Tuesday, August 14, 2007

Build a JAR of JARs with Maven2

If you want to create a ZIP or JAR made up of other artifacts, one option is using the maven-dependency-plugin. Here's a quick how-to:

1. Explicitly list the artifacts you want included.


<dependencies>
...
<dependency>
<groupId>javax.sip</groupId>
<artifactId>jain-sip-ri</artifactId>
<version>1.2-2007-07-25</version>
</dependency>
...
</dependencies>


2. Configure the plugin as needed

<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-fetch-deps</id>
<phase>generate-sources</phase>
<goals><goal>copy-dependencies</goal></goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<stripVersion>true</stripVersion>
<excludeTransitive>true</excludeTransitive>
<excludeArtifactIds>junit</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

2 comments:

Hero Hiralal said...

And how does the classloader find classes in the included jars. I have tried this approach, but when trying to run the 'main' method from the jar, I get a Class Not Found Exception from the line in 'main' where I make the first reference to a class included in one of the jarred jars.

CuppajavaMattiz said...

Hero, you need to add the classpath to the manifest file in meta-inf