简体   繁体   English

在Maven中使用第3方项目作为依赖项

[英]Using 3rd party project as dependency in Maven

I have a project A depending on a project B. I downloaded the project B from a git repo, I ran "mvn package" and "mvn install" in project B so I have it in my ~/.m2/repository directory. 我有一个依赖于项目B的项目A。我从git repo下载了项目B,我在项目B中运行了“ mvn软件包”和“ mvn install”,所以我将它保存在〜/ .m2 / repository目录中。

I tried packaging project A with the "jar-with-dependencies" and it works perfectly, but I don't want a big jar with all the dependencies. 我尝试将项目A与“具有依赖关系的罐子”打包在一起,并且效果很好,但是我不希望所有依赖项都有一个大罐子。

When I put this: 当我把这个:

<dependency>
      <groupId>ch.usi.da</groupId>
      <artifactId>paxos</artifactId>
      <version>trunk</version>
</dependency>

the command "mvn package" works fine but when I try to run it: 命令“ mvn软件包”可以正常工作,但是当我尝试运行它时:

java -cp target/basecast-1.0-SNAPSHOT.jar ar.uba.dc.basecast.App

I got an Exception in thread "main" java.lang.NoClassDefFoundError: ch/usi/da/paxos/Util error. 在线程“主” java.lang.NoClassDefFoundError中得到异常:ch / usi / da / paxos / Util错误。

I'm doing this only with maven, not using Eclipse. 我仅使用maven进行此操作,而不使用Eclipse。 I want to keep it simple and learn what is happening behind the scenes, maybe Eclipse manage the classpath in a better way but I want to do it without it. 我想保持简单,并了解幕后发生的事情,也许Eclipse以更好的方式管理类路径,但我想在没有它的情况下进行操作。

Update: I forgot to mention that this 3rd party project B has a lot of dependencies, so including its .jar file as a "lib" is not my ideal solution. 更新:我忘了提到这个第三方项目B具有很多依赖性,因此将其.jar文件作为“ lib”包含在内并不是我的理想解决方案。 I want to use maven dependencies resolution because everything is installed in my local repository. 我想使用Maven依赖项解析,因为一切都安装在本地存储库中。

If you do not want a fat jar (which can be for many reasons) you must first decide what other mechanism you want to use to provide a way to launch your application. 如果您不想要一个胖子罐(这可能有很多原因),则必须首先确定要使用哪种其他机制来提供启动应用程序的方法。 Typically you may create an executable jar with a Class-Path entry pointing to all the artifacts you use, or create a shell script that can do various sanity tricks and then assemble a classpath variable pointing to all the artifacts you use, or - on MacOS - package up an Application. 通常,您可以使用指向您使用的所有工件的Class-Path条目创建可执行jar,或者创建可以执行各种理智技巧的shell脚本,然后组装指向您使用的所有工件的classpath变量,或者-在MacOS上- -打包一个应用程序。 Advanced deployments may include WebStart. 高级部署可能包括WebStart。

A good solution at your current skill level is to use the appassembler-maven-plugin which has these coordinates: 在您当前的技能水平上,一个好的解决方案是使用具有以下坐标的appassembler-maven-plugin:

            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.10</version>

which with a appropriate configuration section can create the scripts you need (on both Unix and Windows), put the artifacts you use in its correct spot, all as a part of your normal Maven build. 它具有适当的配置部分,可以创建您需要的脚本(在Unix和Windows上),并将您使用的工件放在正确的位置,所有这些均作为常规Maven构建的一部分。

See http://www.mojohaus.org/appassembler/appassembler-maven-plugin/assemble-mojo.html for full instructions. 有关完整说明,请参见http://www.mojohaus.org/appassembler/appassembler-maven-plugin/assemble-mojo.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM