简体   繁体   English

Maven将具有依赖项的jar复制到另一个项目中

[英]Maven Copy jar with dependencies into another project

I have two maven projects, the second project extends some classes of first project. 我有两个Maven项目,第二个项目扩展了第一个项目的某些类。 I want to create the jar file with all dependencies of first project and include it to another project as dependency. 我想创建具有第一个项目的所有依赖项的jar文件,并将其作为依赖项包含到另一个项目中。 I am searching this since long time, is it possible to do it? 很久以来我一直在搜索此文件,可以这样做吗?

I am new to maven, any help on this would be highly appreciated. 我是Maven的新手,对此将提供任何帮助。

Thanks 谢谢

If you just want to add the dependencies to another project you add the second project dependency to your new project and the first one will be inherited and automatically included. 如果仅要将依赖项添加到另一个项目,则将第二个项目依赖项添加到新项目中,第一个依赖项将被继承并自动包含在内。 This is what is called a transitive dependency. 这就是所谓的传递依赖。 Read more about it in the free book Maven: The Complete Reference . 在免费书籍《 Maven:完整参考》中了解有关此内容的更多信息。

If I understand you right, you want to create a uber-jar containing all dependencies, right ? 如果我理解正确,那么您想创建一个包含所有依赖项的uber-jar,对吗?

Please refer to this question How can I create an executable JAR with dependencies using Maven? 请参考此问题。 如何使用Maven创建具有依赖项的可执行JAR?

In the second project's POM file, specify the first projects maven co-ordinates (groupId,artifactId,version,packaging) under the 'dependency' section. 在第二个项目的POM文件中,在“依赖项”部分下指定第一个项目的Maven坐标(groupId,artifactId,版本,包装)。 It will transitively acquire all the dependent artifacts. 它将可传递地获取所有相关的工件。

Although it is possible in Maven to generate a standalone jar with all its dependencies. 尽管在Maven中可以生成具有所有依赖项的独立jar。 For that purpose, you can use the maven-shade-plugin. 为此,您可以使用maven-shade-plugin。 ( Reference ) 参考

There are two ways you are create a fat jar. 有两种创建胖子的方法。 You include the jar itself in the jar of dependent. 您将罐子本身包含在依赖罐中。 You will not have much control in this case and the maven assembly plugin would do the work. 在这种情况下,您将没有太多控制权,而maven程序集插件将可以完成工作。 Alternatively, you can unzip the jar and zip everything together to create the new jar. 或者,您可以解压缩jar并将所有内容压缩在一起以创建新的jar。 You have to decide which one best suits you. 您必须决定哪个最适合您。 If you have multiple versions of same class, then include the whole jar in the new jar would help, but if the versions are coherent, it's best to create a jar by unzipping and zipping everything. 如果您有多个相同类的版本,则将整个jar包含在新的jar中会有所帮助,但是如果版本是一致的,则最好通过解压缩和压缩所有内容来创建一个jar。 For the second procedure, I recommend using the maven shade plugin to create uber jar. 对于第二步,我建议使用maven shade插件创建uber jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Maven 项目中创建具有依赖项的 jar - create jar with dependencies in maven project 使用Maven将jar文件从一个项目复制到另一个项目 - Copy jar file from one project to another using maven 如何将Maven依赖项(jar文件)从一个Java项目复制到另一个动态Web项目的WEB-INF / lib文件夹? - How do you copy Maven dependencies (jar files) from one Java project to another Dynamic web project's WEB-INF/lib folder? Maven - 复制项目的所有依赖项和传递依赖项 - Maven - copy all the dependencies of a project and and transitive dependencies 如何导出将在另一个Java项目中使用的jar以及jar使用的整个maven /外部依赖关系? - How do I export a jar that will be used in another Java project, along with the entire maven/external dependencies that the jar uses? Maven在添加依赖项时复制JAR - Maven to copy JAR when adding dependencies 具有依赖关系的Maven依赖插件复制jar - Maven dependency plugin copy jar with dependencies Maven打包项目作为具有依赖关系的可执行JAR - Maven packaging project as executuble JAR with dependencies 在jar类型的Maven项目中建立依赖关系 - building dependencies in a jar type maven project Maven项目与另一个Maven项目的依赖关系 - Maven project to another maven projects dependencies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM