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?
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.
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.
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.
To exclude any file from a jar / target directory you can use the <excludes> tag in your pom.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With