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

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>

Another alternative is to switch to the much more feature rich maven-shade-plugin which can do this without any external assembly files or marking as "provided" (which may not be what you want as it forces users to have that dependency in their pom). Here is an example:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <artifactSet>
            <excludes>
                <exclude><group-id>:<artifact-id>:<classifier></exclude>
                <exclude>org.apache.logging.log4j:log4j-core</exclude>
            </excludes>
        </artifactSet>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

https://maven.apache.org/plugins/maven-shade-plugin/index.html

Note that this wont make a jar-with-dependencies but rather an original-jar... and then just the regular output jar. This plugin even lets you filter out specific classes.


This example indicates one way to do this:

 <dependencySets>
    <dependencySet>
      ....
      <excludes>
        <exclude>commons-lang:commons-lang</exclude>
        <exclude>log4j:log4j</exclude>
      </excludes>
    </dependencySet>
    ....
  </dependencySets>

Essentially we would use the excludes option available in dependencySet.

See also: https://maven.apache.org/plugins/maven-assembly-plugin/assembly-component.html


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.