Posted by : Unknown Sunday, January 2, 2011

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


- Munish

{ 2 comments... read them below or Comment }

  1. are you trying to create a fat artifact. If yes, try this.

    <![CDATA[
    <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
    <appendAssemblyId>true</appendAssemblyId>
    <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>assembly</goal>
    </goals>
    </execution>
    </executions>
    </plugin>

    ]]>

    ReplyDelete
  2. Is there a way that we can copy the contents of a jar to target without extracting that jar. For example: I have an asset.jar that weigh around 500MB and I need to copy only one folder from it, to my project target. Can this be achieved without unpacking the jar, becoz unpacking is taking a lot of time....

    ReplyDelete

Popular Post

Labels

enums (1) java (2) JAX-RS (1) JPA (1) mysql (1) request 2 (1) RESTful (1) sphinx (1) tomcat (1) web service (2) ws (2)