Sean Connolly
Sean Connolly

Reputation: 5803

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?

Upvotes: 17

Views: 14400

Answers (2)

Niks
Niks

Reputation: 4832

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>

Upvotes: 2

mszalbach
mszalbach

Reputation: 11440

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.

Upvotes: 29

Related Questions