在构建生命周期之外分发/使用 Maven 项目的推荐实践
在很多情况下,我不确定处理 Maven 项目的依赖关系的最佳方法是什么。那是在执行一个 jar 时,这就是 mvn package 的结果。
我尝试过的事情
1) Maven-shade 插件 对于某些用例(例如 hadoop 作业),我发现使用 Maven 阴影插件与打包步骤集成很方便(它构建了一个全包的 uber-jar )。缺点是超级罐子太大了。另外,我无法让 Maven 阴影处理 datanucleus 依赖项,因为它弄乱了一些东西。
2) 将依赖项与 jar 一起分发。
//something like this
$ mvn package dependency:copy-dependencies
$ java -cp target/project.jar:target/dependency com.MyMainClass
生成一个包含所有依赖项以及 jar 的目录。
我想做的是
3)能够分发 jar 并在执行 jar 时处理依赖项。由于 mvn package 将 pom 放在 jars 清单文件夹中,所以所有信息都在那里,对吗? 。现在,我希望有一个单行命令能够运行这个 jar,要求 Maven 管理依赖项。如果有人知道这样的东西是否可以用作 hadoop 作业,那就更棒了。
There are many occasions where I am not sure whats the best to handle dependencies for a maven project. That is while executing a jar thats the result of mvn package.
Things I have tried
1) Maven-shade plugin
For some use cases ( such as hadoop jobs ) I find it convenient to use the Maven shade plugin to integrate with the package step ( it builds an all inclusive uber-jar ). The downside is that the uber-jar is too massive. Also, I cant get the maven shade to work on datanucleus dependencies as it messes up something.
2) distribute the dependencies along with the jar.
//something like this
$ mvn package dependency:copy-dependencies
$ java -cp target/project.jar:target/dependency com.MyMainClass
generates a directory with all the dependencies along with the jar.
What I would like to do is
3) just be able to distribute the jar and handle the dependencies while executing the jar. Since mvn package puts the pom in the jars manifest folder, all the information is there right ? . Now, I would like it if there was a one line command to be able to run this jar asking maven to manage the dependencies. Even more awesome if someone knows if such a thing can be used as a hadoop job.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的第 3 点中的问题没有解决方案。
我的三个选项,在所有情况下,您都可以使用简单的
java -jar target/project.jar
运行应用程序。不需要更多了。1. 带有 jar-with-dependency 描述符的 Maven 程序集插件
(此处缺少 jar 配置中的清单)
2. Maven 依赖项插件
Maven 依赖项插件、正确配置的 jar(类路径)和用于将所有内容打包在一起的 Maven 程序集自定义描述符。
(如果您的项目不使用快照依赖项,您可以简化这一点)
3. Maven One Jar
I think there is no solution for question in your point 3.
My three options, in all cases you can run application with simple
java -jar target/project.jar
. Nothing more is needed.1. Maven Assembly Plugin with jar-with-dependencies descriptor
(manifest in jar configuration is missed here)
2. Maven dependency plugin
Maven dependency plugin, properly jar configured (classpath) and maven assembly custom descriptor for packing all together.
(you can simplify this, if your project is not using snapshot dependencies)
3. Maven One Jar