Friday, June 28, 2013

Exclude transitive dependency while building(maven) a WAR file.

Sometime you want to copy ONLY the dependency jar in WEB-INF/lib directory of the WAR file.
Because service hosting product already provided the transitive dependencies.
You can achieve this requirement using the proper exclusions with the dependency.

Let's say your project has following dependency,

<dependency>
            <groupId>org.wso2.carbon</groupId>
            <artifactId>org.wso2.carbon.registry.common</artifactId>
            <version>4.2.0-SNAPSHOT</version>      
</dependency>

1. First run the mvn dependency:tree command to get the dependency tree of your project. After that copy the output  to some text file and search above jar name(org.wso2.carbon.registry.common).

Let's say you have found following sub tree in the dependency tree of your project .

+- org.wso2.carbon:org.wso2.carbon.registry.common:jar:4.2.0-SNAPSHOT:provided
  +- commons-beanutils.wso2:commons-beanutils:jar:1.8.0.wso2v1:provided
  \- org.wso2.carbon:org.wso2.carbon.feature.mgt.services:jar:4.2.0-SNAPSHOT:provided
     +- org.wso2.carbon:org.wso2.carbon.feature.mgt.core:jar:4.2.0-SNAPSHOT:provided
     +- org.eclipse.equinox:org.eclipse.equinox.p2.artifact.repository:jar:1.1.100.v20110519:provided
     |  +- org.eclipse.equinox:org.eclipse.equinox.p2.director:jar:2.1.0.v20110504-1715:provided
     |  +- org.eclipse.equinox:org.eclipse.equinox.p2.metadata.repository:jar:1.2.0.v20110511-1359:provided
     |  \- org.eclipse.equinox:org.eclipse.equinox.simpleconfigurator:jar:1.0.301.v20120828-033635:provided
     +- org.eclipse.equinox:org.eclipse.equinox.p2.core:jar:2.1.0.v20110502-1955:provided
     +- org.eclipse.equinox:org.eclipse.equinox.p2.engine:jar:2.1.0.v20110511:provided
     +- org.eclipse.equinox:org.eclipse.equinox.p2.metadata:jar:2.1.0.v20110510:provided
     +- org.eclipse.equinox:org.eclipse.equinox.p2.repository:jar:2.1.0.v20110601:provided
     \- org.eclipse.equinox:org.eclipse.equinox.common:jar:3.6.100.v20120522-1841:provided

2. Now, if you want to prevent packaging its transitive dependency you should update the above dependency as follow.

<dependency>
            <groupId>org.wso2.carbon</groupId>
            <artifactId>org.wso2.carbon.registry.common</artifactId>
            <version>4.2.0-SNAPSHOT</version> 
           <exclusions>
                  <exclusion>
                      <groupId>commons-beanutils.wso2</groupId>
                      <artifactId>commons-beanutils</artifactId>
                  </exclusion>

          </exclusions>     
</dependency>

OR , you can  define new dependency with the scope as provided.

<dependency>
            <groupId>commons-beanutils.wso2</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.0.wso2v1</version> 
            <scope>provided</scope>               
</dependency>