如何使用Maven创建具有依赖项的可执行JAR?

我想将我的项目打包在一个可执行的JAR中进行分发。

如何使Maven项目将所有依赖项JAR打包到我的输出JAR中?


#1楼

刘坚认为是对的。 Maven依赖插件允许您扩展所有依赖,然后可以将其视为资源。 这使您可以将它们包括在工件中。 使用Assembly插件会创建一个很难修改的辅助工件-就我而言,我想添加自定义清单条目。 我的pom最终显示为:

<project>
 ...
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
     <execution>
      <id>unpack-dependencies</id>
      <phase>package</phase>
      <goals>
       <goal>unpack-dependencies</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
  ...
  <resources>
   <resource>
    <directory>${basedir}/target/dependency</directory>
    <targetPath>/</targetPath>
   </resource>
  </resources>
 </build>
 ...
</project>

#2楼

您可以使用maven-dependency-plugin,但问题是如何创建可执行JAR。 为此,需要对Matthew Franglen的响应进行以下更改(顺便说一句,从一个干净的目标启动时,使用依赖插件需要更长的构建时间):

<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>${basedir}/target/dependency</directory>
        </resource>
    </resources>
</build>

#3楼

Maven组装插件对我来说很棒。 我花了几个小时使用了maven-dependency-plugin,但无法正常工作。 主要原因是我必须在配置部分中明确定义应包含在文档中所描述的工件项目。 有一个示例,用于您想要使用它的情况: mvn dependency:copy ,其中不包含任何artifactItems,但它不起作用。


#4楼

对我有用的是:

  <plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack-dependencies</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>unpack-dependencies</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.directory}/classes</outputDirectory>
        </configuration>
      </execution>

    </executions>
  </plugin>


  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack-dependencies</id>
        <phase>package</phase>
      </execution>
    </executions>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
          <mainClass>SimpleKeyLogger</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>

我有特殊情况,因为我的依赖关系是系统一:

<dependency>
  ..
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/myjar.jar</systemPath>
</dependency>

我更改了@ user189057提供的代码,并进行了以下更改:1)在“ prepare-package”阶段执行maven-dependency-plugin 2)我将解压后的类直接提取到“ target / classes”


#5楼

我不会像其他人以前已经做过的那样直接回答这个问题,但是我真的很想知道将所有依赖项嵌入项目jar本身是否是一个好主意。

我明白了这一点(易于部署/使用),但这取决于您的项目的用例(并且可能有其他选择(请参见下文))。

如果您完全独立使用它,为什么不这样做。

但是,如果您在其他上下文中使用项目(例如,在Web应用程序中,或放置在其他jar所在的文件夹中),则您的类路径中可能有jar重复项(文件夹中的重复项,jar中的重复项)。 也许不是出价协议,但我通常会避免这种情况。

一个不错的选择:

  • 将您的应用程序部署为.zip / .war:归档文件包含项目的jar和所有相关的jar;
  • 使用动态的类加载器机制(请参见Spring,或者您也可以轻松地自己完成此操作)来获得项目的单个入口点(单个类即可开始-请参见其他答案的Manifest机制),这将(动态)添加到项目当前的classpath以及所有其他需要的jar。

这样,最后只有一个清单和一个“特殊动态类加载器主程序”,您可以使用以下命令启动您的项目:

java -jar ProjectMainJar.jar com.stackoverflow.projectName.MainDynamicClassLoaderClass

#6楼

您也可以使用此插件,它非常好,我用它来包装我的罐子http://sonatype.github.io/jarjar-maven-plugin/


#7楼

应该是这样的:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack-dependencies</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
        </execution>
    </executions>
</plugin>

拆包必须处于“生成资源”阶段,因为如果处于打包阶段,则不会包含在资源中。 尝试清洁包装,您会看到的。


#8楼

我在博客上写了一些不同的方法来做到这一点。

参见带有Apache Maven的可执行Jar (WordPress)

可执行文件罐与Maven示例 (GitHub)

笔记

这些优点和缺点由Stephan提供。


对于手动部署

  • 优点
  • 缺点
    • 依赖性超出了最终的范围。

将依赖项复制到特定目录

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/${project.build.finalName}.lib</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

使Jar可执行并具有Classpath意识

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>
        <mainClass>${fully.qualified.main.class}</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

此时,该jar实际上可以与外部classpath元素一起执行。

$ java -jar target/${project.build.finalName}.jar

制作可部署的档案

jar文件只能在同级...lib/目录中执行。 我们需要创建存档以与目录及其内容一起部署。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <id>antrun-archive</id>
      <phase>package</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <target>
          <property name="final.name" value="${project.build.directory}/${project.build.finalName}"/>
          <property name="archive.includes" value="${project.build.finalName}.${project.packaging} ${project.build.finalName}.lib/*"/>
          <property name="tar.destfile" value="${final.name}.tar"/>
          <zip basedir="${project.build.directory}" destfile="${final.name}.zip" includes="${archive.includes}" />
          <tar basedir="${project.build.directory}" destfile="${tar.destfile}" includes="${archive.includes}" />
          <gzip src="${tar.destfile}" destfile="${tar.destfile}.gz" />
          <bzip2 src="${tar.destfile}" destfile="${tar.destfile}.bz2" />
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>

现在您有了target/${project.build.finalName}.(zip|tar|tar.bz2|tar.gz) ,其中每个都包含jarlib/*


Apache Maven Assembly插件

  • 优点
  • 缺点
    • 不支持类重定位(如果需要类重定位,请使用maven-shade-plugin)。
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <archive>
          <manifest>
            <mainClass>${fully.qualified.main.class}</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </execution>
  </executions>
</plugin>

您具有target/${project.bulid.finalName}-jar-with-dependencies.jar


Apache Maven Shade插件

  • 优点
  • 缺点
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <shadedArtifactAttached>true</shadedArtifactAttached>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>${fully.qualified.main.class}</mainClass>
          </transformer>
        </transformers>
      </configuration>
    </execution>
  </executions>
</plugin>

您具有target/${project.build.finalName}-shaded.jar


onejar-maven-plugin

  • 优点
  • 缺点
    • 自2012年以来未获得积极支持。
<plugin>
  <!--groupId>org.dstovall</groupId--> <!-- not available on the central -->
  <groupId>com.jolira</groupId>
  <artifactId>onejar-maven-plugin</artifactId>
  <executions>
    <execution>
      <configuration>
        <mainClass>${fully.qualified.main.class}</mainClass>
        <attachToBuild>true</attachToBuild>
        <!-- https://code.google.com/p/onejar-maven-plugin/issues/detail?id=8 -->
        <!--classifier>onejar</classifier-->
        <filename>${project.build.finalName}-onejar.${project.packaging}</filename>
      </configuration>
      <goals>
        <goal>one-jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Spring Boot Maven插件

  • 优点
  • 缺点
    • 添加潜在的不必要的Spring和Spring Boot相关类。
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
      <configuration>
        <classifier>spring-boot</classifier>
        <mainClass>${fully.qualified.main.class}</mainClass>
      </configuration>
    </execution>
  </executions>
</plugin>

您具有target/${project.bulid.finalName}-spring-boot.jar


#9楼

我在这里尝试了投票最多的答案,并且能够使jar运行。 但是程序没有正确运行。 我不知道是什么原因。 当我尝试从Eclipse运行时,得到了不同的结果,但是当我从命令行运行jar时,得到了不同的结果(由于特定于程序的运行时错误而崩溃)。

我有与OP类似的要求,只是我的项目对Maven的依赖过多。 幸运的是,唯一对我有用的解决方案是使用Eclipse 。 非常简单,非常简单。 这不是针对OP的解决方案,而是针对具有类似要求但具有许多Maven依赖项的人的解决方案,

1)只需右键单击您的项目文件夹(在Eclipse中),然后选择“ Export

2)然后选择Java - > Runnable Jar

3)系统将要求您选择jar文件的位置

4)最后,选择具有要运行的Main方法的类,然后选择Package dependencies with the Jar file然后单击Finish


#10楼

这是我发现的最好方法:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <archive>
        <manifest>
        <addClasspath>true</addClasspath>
        <mainClass>com.myDomain.etc.MainClassName</mainClass>
        <classpathPrefix>dependency-jars/</classpathPrefix>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <outputDirectory>
               ${project.build.directory}/dependency-jars/
            </outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

使用此配置,所有依赖项都将位于/dependency-jars 。 我的应用程序没有Main类,只有上下文类,但是我的一个依赖项确实有一个Main类( com.myDomain.etc.MainClassName ),该类启动JMX服务器,并接收startstop参数。 因此,我能够像这样启动我的应用程序:

java -jar ./lib/TestApp-1.0-SNAPSHOT.jar start

我等待对您所有人有用。


#11楼

我仔细检查了所有这些响应,以寻找一个包含所有依赖项的胖可执行jar,但它们均无法正常工作。 答案是shade插件,它非常简单明了。

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>2.3</version>
      <executions>
         <!-- Run shade goal on package phase -->
        <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
             <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>path.to.MainClass</mainClass>
             </transformer>
          </transformers>
        </configuration>
          </execution>
      </executions>
    </plugin>

请注意,您的依赖项需要具有一定的编译范围或运行时才能正常运行。

这个例子来自mkyong.com


#12楼

这也可以是一种选择,您将能够构建您的jar文件

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>WordListDriver</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

#13楼

我比较了本文中提到的树形插件。 我生成了2个jar和一个包含所有jar的目录。 我比较了结果,肯定是maven-shade-plugin是最好的。 我面临的挑战是,我需要合并多个spring资源以及jax-rs和JDBC服务。 与maven-assembly-plugin相比,它们全部由shade插件正确合并。 在这种情况下,除非将它们复制到自己的资源文件夹并手动合并一次,否则弹簧将失效。 两个插件都输出正确的依赖关系树。 我有多个作用域,例如测试,提供,编译等,并且两个插件都跳过了测试并提供了该作用域。 他们两个都产生了相同的清单,但是我能够使用他们的转换器将许可证与shade插件合并。 当然,有了Maven-Dependency-plugin,您就不会遇到那些问题,因为罐子没有被提取。 但是,就像其他人指出的那样,您需要携带一个额外的文件才能正常工作。 这是pom.xml的片段

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <includeScope>compile</includeScope>
                        <excludeTransitive>true</excludeTransitive>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.rbccm.itf.cdd.poller.landingzone.LandingZonePoller</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-my-jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <configuration>
                <shadedArtifactAttached>false</shadedArtifactAttached>
                <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.factories</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.tooling</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
                    </transformer>
                </transformers>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

#14楼

对于任何寻求从uber-jar中排除特定依赖项的选项的人,这是一个对我有用的解决方案:

<project...>
<dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>1.6.1</version>
            <scope>provided</scope> <=============
        </dependency>
</dependencies>
<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>...</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

因此,它不是mvn-assembly-plugin的配置,而是依赖项的属性。


#15楼

要从命令行本身创建可执行JAR,只需在项目路径中运行以下命令:

mvn assembly:assembly

#16楼

使用maven-shade-plugin将所有依赖项打包到一个uber-jar中。 通过指定主类,它还可用于构建可执行jar。 在尝试使用maven-assembly和maven-jar之后,我发现此插件最适合我的需求。

我发现此插件特别有用,因为它可以合并特定文件的内容,而不是覆盖它们。 当罐中存在具有相同名称的资源文件并且插件尝试打包所有资源文件时,这是必需的

见下面的例子

      <plugins>
    <!-- This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies. -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                        <!-- signed jars-->
                            <excludes>
                                <exclude>bouncycastle:bcprov-jdk15</exclude>
                            </excludes>
                        </artifactSet>

                         <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <!-- Main class -->
                                <mainClass>com.main.MyMainClass</mainClass>
                            </transformer>
                            <!-- Use resource transformers to prevent file overwrites -->
                            <transformer 
                                 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>properties.properties</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>applicationContext.xml</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/cxf/cxf.extension</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                <resource>META-INF/cxf/bus-extensions.xml</resource>
                            </transformer>
                     </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>

#17楼

为了解决此问题,我们将使用Maven程序集插件,该插件将JAR及其依赖项JAR创建到单个可执行JAR文件中。 只需在pom.xml文件中添加以下插件配置即可。

<build>
   <pluginManagement>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
               <archive>
                  <manifest>
                     <addClasspath>true</addClasspath>
                     <mainClass>com.your.package.MainClass</mainClass>
                  </manifest>
               </archive>
               <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
            </configuration>
            <executions>
               <execution>
                  <id>make-my-jar-with-dependencies</id>
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </pluginManagement>
</build>

完成此操作后,请不要忘记使用以下命令运行MAVEN工具mvn clean compile assembly:single

http://jkoder.com/maven-creating-a-jar-together-with-its-dependency-jars-into-a-single-executable-jar-file/


#18楼

已经有数百万个答案,如果您不需要向应用程序中添加entryPoint,我想添加您不需要<mainClass>例如,API可能不一定必须具有main方法。

Maven插件配置

  <build>
    <finalName>log-enrichment</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

建立

mvn clean compile assembly:single

校验

ll target/
total 35100
drwxrwx--- 1 root vboxsf     4096 Sep 29 16:25 ./
drwxrwx--- 1 root vboxsf     4096 Sep 29 16:25 ../
drwxrwx--- 1 root vboxsf        0 Sep 29 16:08 archive-tmp/
drwxrwx--- 1 root vboxsf        0 Sep 29 16:25 classes/
drwxrwx--- 1 root vboxsf        0 Sep 29 16:25 generated-sources/
drwxrwx--- 1 root vboxsf        0 Sep 29 16:25 generated-test-sources/
-rwxrwx--- 1 root vboxsf 35929841 Sep 29 16:10 log-enrichment-jar-with-dependencies.jar*
drwxrwx--- 1 root vboxsf        0 Sep 29 16:08 maven-status/

#19楼

这是Credit Karma使用的Maven可执行jar插件。 它使用能够从嵌套jar加载类的类加载器创建一个jar罐。 这使您可以在dev和prod中具有相同的类路径,并且仍将所有类保留在单个签名的jar文件中。

https://github.com/creditkarma/maven-exec-jar-plugin

这是一篇博客文章,其中详细介绍了该插件及其制作原因: https//engineering.creditkarma.com/general-engineering/new-executable-jar-plugin-available-apache-maven/


#20楼

您可以在包阶段之前使用dependency-plugin在单独的目录中生成所有依赖项,然后将其包含在清单的类路径中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>theMainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

或者使用${project.build.directory}/classes/lib作为OutputDirectory将所有jar文件集成到主jar中,但是您将需要添加自定义类加载代码来加载jar。


#21楼

添加到pom.xml:

  <dependency>
            <groupId>com.jolira</groupId>
            <artifactId>onejar-maven-plugin</artifactId>
            <version>1.4.4</version>
  </dependency>

<plugin>
       <groupId>com.jolira</groupId>
       <artifactId>onejar-maven-plugin</artifactId>
       <version>1.4.4</version>
       <executions>
              <execution>
                     <goals>
                         <goal>one-jar</goal>
                     </goals>
              </execution>
       </executions>
</plugin>

而已。 下一个mvn软件包还将另外创建一个胖子jar,包括所有依赖项jar。


#22楼

您可以使用maven-shade插件构建如下的uber jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

#23楼

使用onejar插件将其构建为一个可执行jar文件,该文件将所有依赖jar打包在其中。 那解决了我与此类似的问题。 当使用assembly plugin时,它将所有依赖项jar都解压缩到源文件夹中,然后将其重新打包为jar,它已经覆盖了我在代码内部具有相同类名的所有类似实现。 在这里,onejar是一个简单的解决方案。


#24楼

使用maven-assembly-plugin-2.2.1定位共享程序集文件时出现问题?

尝试使用描述符Id配置参数代替描述符/描述符或descriptorRefs / descriptorRef参数。

它们都不满足您的需要:在classpath上查找文件。 当然,您需要添加共享程序集驻留在maven-assembly-plugin的类路径上的包(请参见下文)。 如果您使用的是Maven 2.x(不是Maven 3.x),则可能需要在pluginManagement部分的最上层父pom.xml中添加此依赖项。

请参阅了解更多详情。

类别:org.apache.maven.plugin.assembly.io.DefaultAssemblyReader

例:

        <!-- Use the assembly plugin to create a zip file of all our dependencies. -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorId>assembly-zip-for-wid</descriptorId>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>cz.ness.ct.ip.assemblies</groupId>
                    <artifactId>TEST_SharedAssemblyDescriptor</artifactId>
                    <version>1.0.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </plugin>

#25楼

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

然后用

mvn clean compile assembly:single

编译目标应该在Assembly:single:single之前添加,否则不包括您自己项目中的代码。

在评论中查看更多详细信息。


通常,此目标与自动执行的构建阶段相关。 这样可以确保在执行mvn install或执行部署/发布时构建JAR。

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

#26楼

采纳未答复的答案并重新格式化,我们有:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

接下来,我建议将其作为构建的自然组成部分,而不要明确调用。 要使其成为构建不可或缺的一部分,请将此插件添加到pom.xml并将其绑定到package生命周期事件。 但是,要注意的是,如果将其放入pom.xml中,则需要调用assembly:single目标,而如果从命令行手动执行它,则需要调用“ assembly:assembly”。

<project>
  [...]
  <build>
      <plugins>
          <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                  <archive>
                      <manifest>
                          <addClasspath>true</addClasspath>
                          <mainClass>fully.qualified.MainClass</mainClass>
                      </manifest>
                  </archive>
                  <descriptorRefs>
                      <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
              </configuration>
              <executions>
                  <execution>
                      <id>make-my-jar-with-dependencies</id>
                      <phase>package</phase>
                      <goals>
                          <goal>single</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
      [...]
      </plugins>
    [...]
  </build>
</project>

#27楼

如果您真的想在单个生成的JAR中重新打包其他JAR内容,则可以使用Maven Assembly插件 。 它将解压缩,然后通过<unpack>true</unpack>所有内容重新打包到目录中。 然后,您将获得第二次通过,将其构建为一个大型JAR。

另一个选择是OneJar插件 。 这一步即可完成上述重新打包操作。


#28楼

您可以将以下内容添加到pom.xml中

<build>
<defaultGoal>install</defaultGoal>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>com.mycompany.package.MainClass</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.mycompany.package.MainClass</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-my-jar-with-dependencies</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>

然后,您必须通过控制台切换到pom.xml所在的目录。 然后,您必须执行mvn assembly:single ,然后将有望构建具有依赖性的可执行JAR文件。 您可以在使用cd ./target切换到输出(目标)目录并使用类似于 java -jar mavenproject1-1.0-SNAPSHOT-jar-with-dependencies.jar的命令启动jar时进行检查

我使用Apache Maven 3.0.3对此进行了测试。


#29楼

您可以将maven-shade-pluginmaven-jar-plugin

  • maven-shade-plugin将您的类和所有依赖项打包在一个jar文件中。
  • 配置maven-jar-plugin以指定可执行jar的主类(请参阅“ 设置类路径 ”中的“使Jar可执行”)。

maven-jar-plugin示例POM配置:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.example.MyMainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

最后通过调用创建可执行jar:

mvn clean package shade:shade

#30楼

长期使用maven程序集插件 ,但是我找不到"already added, skipping"问题的解决方案。 现在,我正在使用另一个插件-onejar-maven-plugin 。 下面的示例( mvn package构建jar):

<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.3.0</version>
    <executions>
        <execution>
            <configuration>
                <mainClass>com.company.MainClass</mainClass>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

您需要为该插件添加存储库:

<pluginRepositories>
    <pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
    </pluginRepository>
</pluginRepositories>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值