博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Maven在jar中包含依赖项
阅读量:3584 次
发布时间:2019-05-20

本文共 4737 字,大约阅读时间需要 15 分钟。

本文翻译自:

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楼

参考:


#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中的相关块,它执行此操作:

maven-assembly-plugin
package
single
jar-with-dependencies

#4楼

There's the . 有 。 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 which has a for this purpose and that you could just use on the command line: 使用Maven 2,正确的方法是使用 ,它具有用于此目的的 ,您可以在命令行上使用:

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可执行,只需添加要运行的主类到插件配置:

org.apache.maven.plugins
maven-assembly-plugin
my.package.to.my.MainClass

If you want to create that assembly as part of the normal build process, you should bind the or goal (the assembly goal should ONLY be run from the command line) to a lifecycle phase ( package makes sense), something like this: 如果要将该程序集创建为正常构建过程的一部分,则应该将或目标( assembly目标应仅从命令行运行)绑定到生命周期阶段( package有意义),类似于这个:

org.apache.maven.plugins
maven-assembly-plugin
create-my-bundle
package
single
jar-with-dependencies
...

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 检查此项目:One-JAR

转载地址:http://fmlgj.baihongyu.com/

你可能感兴趣的文章
Java基础入门 arraycopy方法
查看>>
Java基础入门 Math类
查看>>
Java基础入门 Random类
查看>>
Java基础入门 Date类
查看>>
Java基础入门 Calendar类
查看>>
Java基础入门 DateFormat类
查看>>
Java基础入门 Window类及Panel类
查看>>
Java基础入门 AWT事件处理
查看>>
Java基础入门 鼠标事件
查看>>
Java基础入门 键盘事件
查看>>
Java基础入门 GridLayout
查看>>
JavaEE Bean的两种常用作用域 singleton(单例)和prototype(原型)
查看>>
MySQL 数据库索引
查看>>
JavaEE Spring与MyBatis的整合之传统DAO方式整合(教材学习笔记)
查看>>
JavaEE MyBatis与Spring的整合——基于mapper接口方式开发(教材学习笔记)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(五)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(六)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(七)
查看>>
Omap138开发板下以uboot2012.04.01为例分析uboot执行(八)
查看>>
Java发送邮件 注册成功发送邮件
查看>>