Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Add jar-with-dependencies as a dependency

Question..
I'd like to add a dependency on a Maven jar packaged with it's dependencies.

Details..
I have a multi-module Maven project in which one of the module depends on native libraries and the like. As part of it's build, it packages up it's dependencies into a jar-with-dependencies as shown here:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>${mainClass}</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

All good, I get two jars when I build:

seaniscool-0.0.1-SNAPSHOT.jar
seaniscool-0.0.1-SNAPSHOT-jar-with-dependencies.jar

However, I'd like to use this artifact in another module of the same project. If I simply add the module as a dependency, I get the jar without the native libraries included.

I could duplicate the build configuration to include the native libraries in the 2nd module also, it's not very extensive, but would prefer not to.

Any ideas how I can add the jar-with-dependencies as a dependency and thus depend on the libraries included?

Some thoughts..
I know I can build a separate jar with test classes that Maven can reference:

In 1st module:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

In 2nd module:

<dependency>
    <groupId>my.group.id</groupId>
    <artifactId>my.artifact.id</artifactId>
    <version>my.version</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>

Is this concept transferrable here perhaps?

like image 564
Sean Connolly Avatar asked May 26 '13 15:05

Sean Connolly


People also ask

How do I add a dependency in Maven?

Add a Java Maven Dependency to the Utility ProjectRight-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

Do JARs contain dependencies?

Most JARs, no matter whether they contain an executable program or a library, require other libraries to run. Those dependencies are usually contained in JARs as well. So how can you make sure that your JAR has all required JARs in its CLASSPATH?


2 Answers

You can do this with a maven classifier. Classfiers are used so a maven module can build multiple artefacts from the same source. Examples are jdk1.6 or 1.7 version or even the source and javadoc jars maven can build.

So try this:

<dependency>
  <groupId>yourID</groupId>
  <artifactId>seaniscool</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <classifier>jar-with-dependencies</classifier>
</dependency>

If you want to rename your classfier to a better name like withNative or complete or anything else have a look at the maven shade plugin which can also build jars with dependencies but allows some more control.

like image 95
mszalbach Avatar answered Sep 20 '22 15:09

mszalbach


Just a side note to @msczalbach's answer

Actually, even with standarad maven-jar-plugin you can give any suffix to generated jar. Just use the configuration.

E.g:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <descriptorRefs>
            <descriptorRef>self-contained</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>
like image 28
Niks Avatar answered Sep 20 '22 15:09

Niks