Reputation: 198
I am working on a maven project and i need to exclude all transitive dependencies. I have seen the "exclude"tag that is used inside "dependency"tag. But i have a lot of dependencies and writing this tag in every dependency is a huge task. So there is any method where i can exclude all transitive dependencies in a much easier way?
Upvotes: 0
Views: 3822
Reputation: 299218
Ever since Maven 3.2.1, you can use wildcards in dependency exclusions.
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.1.0</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
You will still have to insert the wildcard exclusion into every single dependency though.
Here's an example pom with a Groovy script (executed through the Groovy Maven Plugin) that excludes all transitive dependencies:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.test</groupId>
<artifactId>non-transitive-deps</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>exclude-transitive-deps</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def exclusion = new org.apache.maven.model.Exclusion();
exclusion.groupId='*'
exclusion.artifactId='*'
project.dependencies.each{
d -> d.addExclusion(exclusion)
}
</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
</dependencies>
</project>
Output for mvn dependency:tree
:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ non-transitive-deps ---
[INFO] org.test:non-transitive-deps:jar:1.0-SNAPSHOT
[INFO] \- org.springframework:spring-orm:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
[INFO] | \- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- org.springframework:spring-jdbc:jar:4.3.7.RELEASE:compile
[INFO] \- org.springframework:spring-tx:jar:4.3.7.RELEASE:compile
Output for mvn validate dependency:tree
:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ non-transitive-deps ---
[INFO] org.test:non-transitive-deps:jar:1.0-SNAPSHOT
[INFO] \- org.springframework:spring-orm:jar:4.3.7.RELEASE:compile
So while this groovy script solves your problem, it only does so when a Maven lifecycle phase is executed (not when you trigger a plugin goal directly). validate
is the earliest stage in the lifecycle, long before dependency resolution takes place.
And no, I am not aware of any less verbose solution.
Upvotes: 3