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

39,496

Solution 1

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

Solution 2

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>

Solution 3

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.

Share:
39,496
Jérôme Verstrynge
Author by

Jérôme Verstrynge

You can contact me via my LinkedIn profile.

Updated on October 15, 2020

Comments

  • Jérôme Verstrynge
    Jérôme Verstrynge over 3 years

    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?

  • zengr
    zengr over 8 years
    This gives an error: "Unrecognised tag: 'dependencySets'" for maven3
  • Ravi
    Ravi about 7 years
    This example assumes that I know where dependencySets goes in the pom.xml. A full example would have been helpful.
  • Andrew T Finnell
    Andrew T Finnell over 5 years
    The issue here (after 7 yaers) is that this only goes in the assembly definition file, which is not used with 'jar-with-dependencies.'
  • Shuai Liu
    Shuai Liu about 5 years
    Better answer than Raghuram's
  • wrschneider
    wrschneider almost 4 years
    This is the simplest solution and is consistent with WAR targets as well
  • Guangliang
    Guangliang over 3 years
    I tried this with javafx-controls and javafx-fxml libraries in two different projects. It worked beautifully in the first project. But on the second one, it failed with the compiling many errors such as 'package javafx.beans doesn't exist'. Don't know why.
  • wrschneider
    wrschneider about 2 years
    Funny to see how I upvoted this answer and now I am having a problem with provided JARs being included in the output JAR
  • WesternGun
    WesternGun almost 2 years
    Note that the final jar would be like app-0.0.1-SNAPSHOT-<descriptor-id>.jar, just as the original jar is app-0.0.1-SNAPSHOT-jar-with-dependencies.jar. If the jar name matters, change the descriptor id to match the original.