使用Maven在jar中包含依赖项

本文讨论了如何使用Maven的不同方法将依赖项包含到一个jar文件中,如maven-assembly-plugin和maven-shade-plugin,以及如何通过自定义classloader将jar库放入主jar中。

本文翻译自:Including dependencies in a jar with Maven

Is there a way to force maven(2.0.9) to include all the dependencies in a single jar file? 有没有办法强制maven(2.0.9)在一个jar文件中包含所有依赖项?

I have a project the builds into a single jar file. 我将一个项目构建到一个jar文件中。 I want the classes from dependencies to be copied into the jar as well. 我希望将依赖项中的类复制到jar中。

Update: I know that I cant just include a jar file in a jar file. 更新:我知道我不能在jar文件中包含一个jar文件。 I'm searching for a way to unpack the jars that are specified as dependencies, and package the class files into my jar. 我正在寻找一种方法来解压缩指定为依赖项的jar,并将类文件打包到我的jar中。


#1楼

参考:https://stackoom.com/question/7Fny/使用Maven在jar中包含依赖项


#2楼

If you (like me) dont particularly like the jar-with-dependencies approach described above, the maven-solution I prefer is to simply build a WAR-project, even if it is only a stand-alone java application you are building: 如果你(像我一样)不喜欢上面描述的jar-with-dependencies方法,我更喜欢的maven-solution是简单地构建一个WAR项目,即使它只是你正在构建的一个独立的java应用程序:

  1. Make a normal maven jar-project, that will build your jar-file (without the dependencies). 制作一个普通的maven jar项目,它将构建你的jar文件(没有依赖项)。

  2. Also, setup a maven war-project (with only an empty src/main/webapp/WEB-INF/web.xml file, which will avoid a warning/error in the maven-build), that only has your jar-project as a dependency, and make your jar-project a <module> under your war-project. 另外,设置一个maven war-project(只有一个空的src / main / webapp / WEB-INF / web.xml文件,这将避免maven-build中的警告/错误),只有你的jar项目一个依赖项,并使你的jar项目成为你的战争项目下的<module> (This war-project is only a simple trick to wrap all your jar-file dependencies into a zip-file.) (这个war-project只是将所有jar文件依赖项包装到zip文件中的简单技巧。)

  3. Build the war-project to produce the war-file. 构建war-project以生成war文件。

  4. In the deployment-step, simply rename your .war-file to *.zip and unzip it. 在部署步骤中,只需将.war文件重命名为* .zip并解压缩即可。

You should now have a lib-directory (which you can move where you want it) with your jar and all the dependencies you need to run your application: 您现在应该有一个lib-directory(可以将它移动到您想要的位置)与您的jar以及运行应用程序所需的所有依赖项:

java -cp 'path/lib/*' MainClass

(The wildcard in classpath works in Java-6 or higher) (classpath中的通配符适用于Java-6或更高版本)

I think this is both simpler to setup in maven (no need to mess around with the assembly plugin) and also gives you a clearer view of the application-structure (you will see the version-numbers of all dependent jars in plain view, and avoid clogging everything into a single jar-file). 我认为这在maven中设置更简单(不需要使用程序集插件)并且还可以让您更清楚地了解应用程序结构(您将在普通视图中看到所有相关jar的版本号,以及避免将所有内容堵塞到一个jar文件中)。


#3楼

You can do this using the maven-assembly plugin with the "jar-with-dependencies" descriptor. 您可以使用带有“jar-with-dependencies”描述符的maven-assembly插件来完成此操作。 Here's the relevant chunk from one of our pom.xml's that does this: 这是我们的一个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>

#4楼

There's the shade maven plugin . 阴影maven插件 It can be used to package and rename dependencies (to omit dependency problems on the classpath). 它可用于打包和重命名依赖项(以省略类路径上的依赖性问题)。


#5楼

With Maven 2, the right way to do this is to use the Maven2 Assembly Plugin which has a pre-defined descriptor file for this purpose and that you could just use on the command line: 使用Maven 2,正确的方法是使用Maven2 Assembly Plugin ,它具有用于此目的的预定义描述符文件 ,您可以在命令行上使用:

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

If you want to make this jar executable, just add the main class to be run to the plugin configuration: 如果你想让这个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>

If you want to create that assembly as part of the normal build process, you should bind the single or directory-single goal (the assembly goal should ONLY be run from the command line) to a lifecycle phase ( package makes sense), something like this: 如果要将该程序集创建为正常构建过程的一部分,则应该将单个目录单个目标( 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>

Adapt the configuration element to suit your needs (for example with the manifest stuff as spoken). 调整configuration元素以满足您的需求(例如,使用清单说明的内容)。


#6楼

Putting Maven aside, you can put JAR libraries inside the Main Jar but you will need to use your own classloader. 将Maven放在一边,你可以将JAR库放在Main Jar中,但是你需要使用自己的类加载器。

Check this project: One-JAR link text 检查此项目:One-JAR 链接文本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值