【问题标题】:How to create a .jar out of multiple jars using Maven Plugin如何使用 Maven 插件从多个 jar 中创建一个 .jar
【发布时间】:2016-11-03 16:20:36
【问题描述】:

我有一个项目,其中有多个 .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

现在,我希望能够在 pom.xmlpom.xml 中将 sampleModule1 用作 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 来添加它。我找到了this,它展示了如何将多个文件包含在一个文件中的示例。但是,在示例中它们不是罐子,而是类和其他。现在在这里,我试图实现相同的目标,但只使用 jars。

【问题讨论】:

  • 你的项目结构是什么?这些 jar 文件在 pom.xml 文件之外吗?
  • 谢谢提醒。它们与 pom.xml 位于同一目录中。
  • 为什么不将它们作为依赖项添加到 pom.xml 中?这就是maven的意思!
  • 同一个项目下的10个.jar,你确定吗?我的意思是,据我所知,在添加依赖项时,您可以为每个工件 ID 添加一个 .jar。我可以只使用相同的 artifact-id 并继续添加所有 jar 吗?
  • 请张贴您的结构和实际 jar 文件的屏幕截图,以帮助我们更好地理解。谢谢!

标签: maven jar maven-plugin maven-shade-plugin


【解决方案1】:

有两种方法可以解决您的问题!如果你只是想将文件添加到你的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 中。如需complete syntax reference,请查看此处。

另一种方式,也许是最方便的方式是使用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 部分允许您通过提及他们的groupidartifactid 来从本地复制中引用要包含的jar

这将包括class 文件、manifest 等内容以及提到的工件(来自您的本地复制品)的文件夹结构)到您的 jar 中。

如果你想在你的 jar 中包含其他任意文件,你可以使用插件的 filter 标签,它允许你直接指定文件和文件模式。如需complete syntax reference,请查看此处。

P.S.:如果要排除某些文件,可以将 include 标签替换为 exclude 标签 ;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多