使用Maven在jar中包含依赖项

问题描述 投票:307回答:13

有没有办法强制maven(2.0.9)在一个jar文件中包含所有依赖项?

我将一个项目构建到一个jar文件中。我希望将依赖项中的类复制到jar中。

更新:我知道我不能在jar文件中包含一个jar文件。我正在寻找一种方法来解压缩指定为依赖项的jar,并将类文件打包到我的jar中。

java maven-2 jar packaging
13个回答
445
投票

您可以使用带有“jar-with-dependencies”描述符的maven-assembly插件来完成此操作。这是我们的一个pom.xml中的相关块,它执行此操作:

  <build>
    <plugins>
      <!-- any other plugins -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

1
投票

将Maven放在一边,你可以将JAR库放在Main Jar中,但是你需要使用自己的类加载器。

检查这个项目:One-JAR link text


0
投票

这篇文章可能有点旧,但我最近也有同样的问题。 John Stauffer提出的第一个解决方案很好,但是我在今年春天工作时遇到了一些问题。我使用的spring的dependency-jars有一些属性文件和xml-schemas声明,它们共享相同的路径和名称。虽然这些jar来自相同的版本,但jar-with-dependencies maven-goal用最后找到的文件覆盖了这些文件。

最后,应用程序无法启动,因为spring jar无法找到正确的属性文件。在这种情况下,Rop提出的解决方案已经解决了我的问题。

此后,Spring-boot项目现在也存在。通过提供一个重载包目标并提供自己的类加载器的maven目标,它有一种非常酷的方式来管理这个问题。见spring-boots Reference Guide


0
投票

Have a look at this answer:

我正在创建一个作为Java JAR文件运行的安装程序,它需要将WAR和JAR文件解压缩到安装目录中的适当位置。依赖插件可以在包阶段中与复制目标一起使用,它将下载Maven存储库中的任何文件(包括WAR文件)并将它们写在您需要的地方。我将输出目录更改为$ {project.build.directory} / classes,然后最终结果是正常的JAR任务包含我的文件就好了。然后我可以提取它们并将它们写入安装目录。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
    <execution>
        <id>getWar</id>
        <phase>package</phase>
        <goals>
            <goal>copy</goal>
        </goals>
        <configuration>
            <artifactItems>
                <artifactItem>
                    <groupId>the.group.I.use</groupId>
                    <artifactId>MyServerServer</artifactId>
                    <version>${env.JAVA_SERVER_REL_VER}</version>
                    <type>war</type>
                    <destFileName>myWar.war</destFileName>
                </artifactItem>
            </artifactItems>
            <outputDirectory>${project.build.directory}/classes</outputDirectory>
        </configuration>
    </execution>
</executions>


0
投票

谢谢我在POM.xml文件中添加了下面的代码片段并解决了Mp问题,并创建了包含所有依赖jar的胖jar文件。

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <descriptorRefs>
                <descriptorRef>dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
</plugins>

126
投票

使用Maven 2,正确的方法是使用Maven2 Assembly Plugin,它有一个pre-defined descriptor file用于此目的,你可以在命令行上使用:

mvn assembly:assembly -DdescriptorId=jar-with-dependencies

如果你想让这个jar可执行,只需添加要运行的主类到插件配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>my.package.to.my.MainClass</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

如果要将该程序集创建为正常构建过程的一部分,则应将singledirectory-single目标(assembly目标应仅从命令行运行)绑定到生命周期阶段(package有意义),如下所示:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>create-my-bundle</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        ...
      </configuration>
    </execution>
  </executions>
</plugin>

调整configuration元素以满足您的需求(例如,使用清单说明的东西)。


29
投票

如果你想做一个可执行的jar文件,他们也需要设置主类。所以完整的配置应该是。

    <plugins>
            <plugin>
                 <artifactId>maven-assembly-plugin</artifactId>
                 <executions>
                     <execution>
                          <phase>package</phase>
                          <goals>
                              <goal>single</goal>
                          </goals>
                      </execution>
                  </executions>
                  <configuration>
                       <!-- ... -->
                       <archive>
                           <manifest>
                                 <mainClass>fully.qualified.MainClass</mainClass>
                           </manifest>
                       </archive>
                       <descriptorRefs>
                           <descriptorRef>jar-with-dependencies</descriptorRef>
                      </descriptorRefs>
                 </configuration>
         </plugin>
   </plugins>

17
投票

shade maven plugin。它可用于打包和重命名依赖项(以省略类路径上的依赖性问题)。


15
投票

您可以使用<classifier>标签使用新创建的jar。

<dependencies>
    <dependency>
        <groupId>your.group.id</groupId>
        <artifactId>your.artifact.id</artifactId>
        <version>1.0</version>
        <type>jar</type>
        <classifier>jar-with-dependencies</classifier>
    </dependency>
</dependencies>

12
投票

如果你(像我一样)不喜欢上面描述的jar-with-dependencies方法,我更喜欢的maven-solution是简单地构建一个WAR项目,即使它只是你正在构建的一个独立的java应用程序:

  1. 制作一个普通的maven jar项目,它将构建你的jar文件(没有依赖项)。
  2. 另外,设置一个maven war-project(只有一个空的src / main / webapp / WEB-INF / web.xml文件,这将避免maven-build中的警告/错误),只有你的jar项目一个依赖,并使你的jar项目成为你的战争项目下的<module>。 (这个war-project只是将所有jar文件依赖项包装到zip文件中的简单技巧。)
  3. 构建war-project以生成war文件。
  4. 在部署步骤中,只需将.war文件重命名为* .zip并解压缩即可。

您现在应该有一个lib-directory(可以将它移动到您想要的位置)与您的jar以及运行应用程序所需的所有依赖项:

java -cp 'path/lib/*' MainClass

(classpath中的通配符适用于Java-6或更高版本)

我认为这在maven中设置更简单(不需要使用程序集插件)并且还可以让您更清楚地了解应用程序结构(您将在普通视图中看到所有相关jar的版本号,以及避免将所有内容堵塞到一个jar文件中)。


10
投票

http://fiji.sc/Uber-JAR提供了对替代方案的出色解释:

构建uber-JAR有三种常用方法:

  1. 无阴影。解压缩所有JAR文件,然后将它们重新打包到单个JAR中。 Pro:使用Java的默认类加载器。 Con:具有相同路径的多个JAR文件中存在的文件(例如,META-INF / services / javax.script.ScriptEngineFactory)将相互覆盖,从而导致错误行为。 工具:Maven Assembly Plugin,Classworlds Uberjar
  2. 阴影。与无阴影相同,但重命名(即“阴影”)所有依赖项的所有包。 Pro:使用Java的默认类加载器。避免一些(不是全部)依赖版本冲突。 Con:具有相同路径的多个JAR文件中存在的文件(例如,META-INF / services / javax.script.ScriptEngineFactory)将相互覆盖,从而导致错误行为。 工具:Maven Shade插件
  3. JAR的JAR。最终的JAR文件包含嵌入的其他JAR文件。 专业版:避免依赖版本冲突。保留所有资源文件。 Con:需要捆绑一个特殊的“bootstrap”类加载器,以使Java能够从包装的JAR文件中加载类。调试类加载器问题变得更加复杂。 工具:Eclipse JAR文件导出器,One-JAR。

4
投票

我对Eclipse Luna和m2eclipse的最终解决方案:Custom Classloader(下载并添加到您的项目中,仅限5个类):http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/plain/org.eclipse.jdt.ui/jar%20in%20jar%20loader/org/eclipse/jdt/internal/jarinjarloader/;这个类加载器非常适合单jar类加载器并且非常快;

<project.mainClass>org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader</project.mainClass> <project.realMainClass>my.Class</project.realMainClass>

在JIJConstants中编辑“Rsrc-Class-Path”到“Class-Path” mvn clean依赖:copy-dependencies包 在lib文件夹中创建了一个带有瘦类加载器的jar的jar

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*</include>
            </includes>
            <targetPath>META-INF/</targetPath>
        </resource>
        <resource>
            <directory>${project.build.directory}/dependency/</directory>
            <includes>
                <include>*.jar</include>
            </includes>
            <targetPath>lib/</targetPath>
        </resource>
    </resources>
<pluginManagement>
        <plugins>

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

                        <manifestEntries>
                            <Rsrc-Main-Class>${project.realMainClass}  </Rsrc-Main-Class>
                            <Class-Path>./</Class-Path>
                        </manifestEntries>

                    </archive>
                </configuration>
            </plugin>
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

3
投票
        <!-- Method 1 -->
        <!-- Copy dependency libraries jar files to a separated LIB folder -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive> 
                <stripVersion>false</stripVersion>
            </configuration>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Add LIB folder to classPath -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>


        <!-- Method 2 -->
        <!-- Package all libraries classes into one runnable jar -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
        </plugin>            
© www.soinside.com 2019 - 2024. All rights reserved.