Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven won't exclude during copy-dependencies

Tags:

java

maven

I have a project that uses Netty 4.0.29 and I have another dependency that pulls in netty 3.9.0. I put in an exclusion but it is still roping in 3.9.0 when I run copy-dependencies.

    <dependency>
        <groupId>com.ning</groupId>
        <artifactId>async-http-client</artifactId>
        <version>1.9.31</version>
        <exclusions>
            <exclusion>
                <groupId>io.netty</groupId>
                <artifactId>netty</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

If I run mvn dependency:tree with this exclusion in place, I see that it is indeed excluded:

[INFO] +- com.ning:async-http-client:jar:1.9.31:compile

But when I run mvn clean dependency:copy-dependencies I see the jar 3.9.0 being copied along with the 4.0.29. According to the documentation and Google, this should not copy when there is an exclusion.

[INFO] Copying netty-3.9.0.Final.jar to /Users/udonom1/wk/141/coursecopy-api/target/dependency/netty-3.9.0.Final.jar
[INFO] Copying netty-all-4.0.29.Final.jar to /Users/udonom1/wk/141/coursecopy-api/target/dependency/netty-all-4.0.29.Final.jar

I tried excluding as suggested by the first answer below and that did not work.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>                               <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                        <excludeArtifactIds>io.netty:netty:3.9.0.Final</excludeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>

I also added a dependency as further suggested:

    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.29.Final</version>
    </dependency>

What am I doing wrong?

like image 379
vertuso Avatar asked Sep 24 '15 22:09

vertuso


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.

Is there a way to exclude Maven dependency globally?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.

Can we exclude a class from Maven dependency?

You could change those classes and define them in a different jar/module which should be included as a dependency before the jar that supplies the dependency where your class to be excluded resides (Marker. class). Maven remembers the classpath ordering from version 2.0.

How does Maven exclusion work?

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.


1 Answers

For those who are having the same issue. I used mvn -X and discovered that dependency:tree is omitting two other jars that are referencing netty. I added exclusions for those and I'm good to go. Spent a whole day on this.

like image 87
vertuso Avatar answered Sep 28 '22 20:09

vertuso