How to Exclude Certain Jar Dependencies when Using Maven for Jar Assembly

Jamie Jackson picture Jamie Jackson · Mar 20, 2013 · Viewed 20.5k times · Source

There are a handful of very similar-seeming questions on StackOverflow, but I'm having trouble finding anything that works.

For now, I'm trying to remove log4j from the built Jar.

Although I'm new to Maven, and I'm sure I've got this all wrong, here's what I'm trying.

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.icf.onecpd</groupId>
    <artifactId>pdfgen</artifactId>
    <version>1.1</version>
    <packaging>jar</packaging>

    <name>PDF Generator</name>
    <url></url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.2.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                        </configuration>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <scm>
        <url>https://svn.tms.icfi.com/svn/HUD/onecpd/income_calculator_pdf_generator</url>
    </scm>
</project>

assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <dependencySets>
        <dependencySet>
            <excludes>
                <exclude>log4j:log4j:*</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

assembly:single log:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building PDF Generator 1.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (default-cli) @ pdfgen ---
[INFO] Reading assembly descriptor: assembly.xml
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] Building jar: /home/jamie/workspace_ruby2/onecpd_pdfgen/target/pdfgen-1.1-jar-with-dependencies.jar
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.368s
[INFO] Finished at: Wed Mar 20 13:45:33 EDT 2013
[INFO] Final Memory: 8M/158M
[INFO] ------------------------------------------------------------------------

However, the resulting Jar ends up with the /org/apache/log4j/ classes included.

How do I get rid of them?

Thanks!

Answer

1959bob picture 1959bob · Mar 20, 2013

You can try declaring the dependency as a provided scope, e.g.

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>provided</scope>
    </dependency>