Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

Maven's assembly plugin enables the creation of a big jar including all dependencies with descriptorRef jar-with-dependencies.

How can one exclude some of these dependencies? It seems like it does not have such a configuration? Is there another solution?

like image 763
Jérôme Verstrynge Avatar asked Jun 02 '11 02:06

Jérôme Verstrynge


People also ask

How you can exclude dependency in Maven?

You can use Exclude command from the context menu in the Maven dependency diagram to quickly exclude the specified dependency from POM and the respective tool windows. The dependency is also excluded from the Project and Maven tool windows.

Are Maven dependencies included in jar?

Apache Maven Shade Plugin provides the capability to package the artifact in an uber-jar, which consists of all dependencies required to run the project.

Does a jar file contain all dependencies?

Normally, when we package a project into a jarW file, the jar file doesn't contain its dependencies, so the dependency jar files would need to be included in the classpathW in order to execute a class in the project's jar file that uses one of the dependencies.

How do you exclude a jar file while building a application?

To exclude any file from a jar / target directory you can use the <excludes> tag in your pom.


2 Answers

Add the <scope>provided</scope> to the dependencies you don't want included in the jar-with-dependencies, e.g.

    <dependency>       <groupId>storm</groupId>       <artifactId>storm</artifactId>       <version>0.6.1-SNAPSHOT</version>       <scope>provided</scope>     </dependency> 
like image 146
Jeroen Vuurens Avatar answered Oct 01 '22 14:10

Jeroen Vuurens


You should use the excludes option available in dependencySet.
Follow below.:

Example in your pom.xml:

...   <build>     <plugins>       <plugin>         <artifactId>maven-assembly-plugin</artifactId>         <version>2.3</version>         <configuration>           <finalName>../final/${project.artifactId}</finalName>           <archive>             <manifest>               <addClasspath>false</addClasspath>               <mainClass>com.entrerprise.App</mainClass>             </manifest>           </archive>           <descriptors>             <descriptor>src/main/resources/jar-with-deps-with-exclude.xml</descriptor>           </descriptors>           <appendAssemblyId>false</appendAssemblyId>         </configuration>         <executions>           <execution>             <id>make-assembly</id>             <phase>package</phase>             <goals>               <goal>single</goal>             </goals>           </execution>         </executions>       </plugin> ... 

Now in your new file jar-with-deps-with-exclude.xml (where dependencySet lives):

 <?xml version="1.0" encoding="UTF-8"?>     <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">         <id>jar-with-dependencies-and-exclude-classes</id>         <formats>           <format>jar</format>         </formats>         <includeBaseDirectory>false</includeBaseDirectory>         <dependencySets>           <dependencySet>             <outputDirectory>/</outputDirectory>             <useProjectArtifact>false</useProjectArtifact>             <unpack>true</unpack>             <scope>runtime</scope>             <excludes>               <exclude>junit:junit</exclude>               <exclude>commons-cli:commons-cli</exclude>               <exclude>org.apache.maven.wagon:wagon-ssh</exclude>             </excludes>            </dependencySet>         </dependencySets>         <fileSets>           <fileSet>             <outputDirectory>/</outputDirectory>             <directory>${project.build.outputDirectory}</directory>           </fileSet>         </fileSets>       </assembly> 

That's all.

like image 25
Felipe Pereira Avatar answered Oct 01 '22 12:10

Felipe Pereira