如何使用Maven Plugin从多个jar中创建一个.jar



我有一个项目,其中有多个.jar文件。要将其作为依赖项添加,我需要将所有这些jar变成一个大jar。直到:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>mainProjects</groupId>
      <artifactId>mainProjects.master</artifactId>
      <relativePath>../pom.xml</relativePath>
      <version>1.0.0-SNAPSHOT</version>
   </parent>
   <groupId>mainProjects</groupId>
   <artifactId>sampleModule1</artifactId>
   <name>sampleModule1</name>
   <version>1.0.0.qualifier</version>
   <build>
    <plugins>
     <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <artifactSet>
                    <includes>
                        <include>Sample1.jar</include>
                        <include>Sample2.jar</include>
                     </includes>
                    </artifactSet>
                    <finalName>${project.artifactId}-${project.version}-final</finalName>
                    <outputDirectory>${project.artifactId}/build</outputDirectory>
                    <shadedArtifactAttached>false</shadedArtifactAttached>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>
</build>
</project>

这将创建最终的jar,但是,其中没有来自其他jar (sample1.jar和sample2.jar)的内容。我已经查看了插件的文档,但是他们所做的一切都是通过类文件,而不是jar来完成的。所以我不知道从现在开始该怎么做。

任何想法吗?

更新:

为了说清楚,我在这里分享我的项目结构:

 +- mainProjects
 |   +- pom.xml
 |   +- mainProject1
 |   |  +- pom.xml
 |   |  +- src
 |   +- mainProject2 
 |   |  +- pom.xml
 |   |  +- src   
 |   +- group1
 |   |  +- pom.xml  
 |   |  +- sampleModule1
 |   |  | +- pom.xml  
 |   |  | +- build
 |   |  | +- sample1.jar
 |   |  | +- sample2.jar
 |   |  | +- sample3.jar
 |   |  | +- sample4.jar
 |   |  | +- sample5.jar
 |   |  | +- sample6.jar
 |   |  +- sampleModule2
 |   |  | +- pom.xml
 |   |  | +- src

现在,我希望能够使用sampleModule1作为mainProject1pom.xml作为一个jar的依赖项,如下所示:

      <dependency>
         <groupId>group1</groupId>
         <artifactId>sampleModule1</artifactId>
         <version>1.0.0.qualifier</version>
         <scope>system</scope>
         <systemPath>sampleModule1/build/${project.artifactId}-${project.version}-final.jar</systemPath>
   </dependency>

要实现这一点,我需要将所有jar编译成一个jar,这样我就可以通过使用一个systemPath来添加它。我发现这是一个如何将多个文件包含到一个文件中的例子。然而,在这个示例中,它们不是jar,而是类和其他类。现在,在这里,我试图实现相同的,但只有jar .

有两种方法可以解决您的问题!如果您只想将文件添加到jar中,可以使用resources标记来添加它们

<build>
    <resources>
        <resource>
            <filtering>false</filtering>
            <directory>lib</directory>
            <includes>
                <include>*.jar</include>
            </includes>
        </resource>
    </resources>
</build>

现在将lib文件夹中的所有jar文件放入主jar中。根文件夹是您从中调用相应pom.xml的文件夹。这样,您就可以向jar中添加任意文件。要获得完整的语法参考,请查看此处。

另一种方法,也许是最方便的方法是使用maven-shade-plugin,它允许您将几乎所有内容复制到最终的jar中。

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-shade-plugin</artifactId>
   <version>2.4.3</version>
   <executions>
      <execution>
         <phase>package</phase>
         <goals>
            <goal>shade</goal>
         </goals>
         <configuration>
            <artifactSet>
               <includes>
                  <include>groupid:artifactid_a</include>
                  <include>groupid:artifactid_b</include>
               </includes>
            </artifactSet>
            <filters>
               <filter>
                  <artifact>*:*</artifact>
                  <includes>
                     <include>resources/*.png</include>
                  </includes>
               </filter>
            </filters>
         </configuration>
      </execution>
   </executions>
</plugin>

artifactSet部分允许您从本地副本中引用jar,只需提及它们的groupidartifactid

这将包括class文件、manifest文件等内容,以及上述工件(来自您的本地副本)的文件夹结构。

如果你想包含其他任意文件到你的jar中,你可以使用插件的filter标签,它允许你直接指定文件和文件模式。要获得完整的语法参考,请查看此处。

注::如果你想排除某些文件,你可以用exclude标签代替include标签;-)

最新更新