简体   繁体   中英

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.

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.

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:

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.

I'm doing this only with maven, not using 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.

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. I want to use maven dependencies resolution because everything is installed in my local repository.

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. Advanced deployments may include WebStart.

A good solution at your current skill level is to use the appassembler-maven-plugin which has these coordinates:

            <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.

See http://www.mojohaus.org/appassembler/appassembler-maven-plugin/assemble-mojo.html for full instructions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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