views:

8121

answers:

6

Hi, Right now, in Maven2, to exclude a single transitive dependency, I have to do something like:

  <dependency>
  <groupId>sample.group</groupId>
  <artifactId>sample-artifactB</artifactId>
  <version>1</version>
   <exclusions>
     <exclusion>
       <groupId>sample.group</groupId>
       <artifactId>sample-artifactAB</artifactId>
     </exclusion>
   </exclusions>
</dependency>

The problem with this approach is that i have to manually exclude every dependency.

Is there a way to use some sort of wildcard to exclude all transitive dependency at once instead of excluding them one by one.

A: 

What is your reason for excluding all transitive dependencies?

If there is a particular artifact (such as commons-logging) which you need to exclude from every dependency, the Version 99 Does Not Exist approach might help.

Esko Luontola
+3  A: 

To my knowledge there isn't. While it sounds convenient, I think that kind of defeats the tenet of dependency management.

While not as concise (and hackish) as Esko's solution, I'd recommend creating your own custom pom for the dependency that has your <exclusions>. For projects that need to use that dependency, set the dependency to your custom pom instead of the typical artifact. While that does not necessarily allow you exclude all transitive dependencies with a single <exclusion>, it does allow you only have to write your dependency once and all of your projects don't need to maintain unnecessary and long exclusion lists.

That's the best I can suggest, at least.

whaley
I would recommend against making your own pom to work around exclusions. This makes your build far less portable and reduces the comprehension.
Brian Fox
Wow, the accepted answer has net -1 votes! :-)
Andrew Swan
Considering the down voter was presumably Brian Fox, who is employed by Sonatype and is a maven committer, I wasn't going to argue a bunch :) He does have a point in his comment.
whaley
A: 

At times one needs to use a latest version of the library say Spring 2.5.6 but some other dependencies include older version e.g. struts2-spring-plugin (2.1.6) includes Spring 2.5.3.

In such scenarios there is a requirement for exclusion or overriding the version.

Vinod Singh
A: 

You can use exclude, but you have to manually list each dependency that you wish to exclude…

<dependency>
  <groupId>sample.group</groupId>
  <artifactId>sample-artifactB</artifactId>
  <version>1</version>
   <exclusions>
     <exclusion>
       <groupId>sample.group</groupId>
       <artifactId>sample-artifactAB</artifactId>
     </exclusion>
   </exclusions>
</dependency>
MikeNereson
+1  A: 

Currently, there's no way to exclude more than one transitive dependency at a time, but there is a feature request for this on the Maven JIRA site:

http://jira.codehaus.org/browse/MNG-2315

Peter
+1  A: 

There is a workaround for this, if you set the scope of a dependency to runtime, transitive dependencies will be excluded. Though be aware this means you need to add in additional processing if you want to package the runtime dependency.

To include the runtime dependency in any packaging, you can use the maven-dependency-plugin's copy goal for a specific artifact.

Rich Seller