Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Maven - Creating a Jar From Contents of Another Jar File

DZone's Guide to

Maven - Creating a Jar From Contents of Another Jar File

· Java Zone
Free Resource

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

Recently I came across a situation where I had to create a jar file with similar contents as of some other available jar. As using maven we cannot copy one jar file within another and also I didn't want to reinvent the wheel again so here is how I achieved the same.

Let's say there is an artifact 'sandeep-ejb-client' (some client ejb jar which has some dependency on some other artifacts say log4j and junit)
What I am interested is in creating a new jar file that has all the ejb client classes without including dependencies of 'sandeep-ejb-client'.

Here is the POM file to achieve the same:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.test.gognamunish</groupId>
<artifactId>Test</artifactId>
<version>1_0_0</version>
<name>Test JAR</name>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-fetch-deps</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<stripVersion>true</stripVersion>
<excludeTransitive>true</excludeTransitive>
<excludeArtifactIds>log4j,junit</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.ejb.sandeepkadyan</groupId>
<artifactId>sandeep-ejb-client</artifactId>
<version>1_0_0</version>
</dependency>
</dependencies>

</project>

That's All, Simple? If anyone has got better approach please feel free to discuss !!!

From http://gognamunish.blogspot.com/2011/01/maven-creating-jar-from-contents-of.html

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}