Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding Maven dependencies

Tags:

maven

maven-3

I have a query about exclusion of maven dependencies . Consider the following

   <dependency>         <groupId>org.springframework.security</groupId>         <artifactId>spring-security-taglibs</artifactId>         <version>${spring-security.version}</version>         <exclusions>             <exclusion>                 <groupId>org.springframework.security</groupId>                 <artifactId>spring-security-web</artifactId>             </exclusion>         </exclusions>     </dependency>     <dependency>         <groupId>org.springframework.security</groupId>         <artifactId>spring-security-web</artifactId>         <version>3.1.0.RELEASE</version>         <exclusions>             <exclusion>                 <groupId>org.springframework</groupId>                 <artifactId>spring-web</artifactId>             </exclusion>         </exclusions>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-web</artifactId>         <version>3.1.0.RELEASE</version>     </dependency>     <dependency> 

I am trying to achieve a transition from Spring 3.0.6 to 3.1.0 . Spring security 3.1.0 had a dependency on spring-security-web version 3.0.6 which in turn had a dependency on spring-web 3.0.6 . I need to bring it all to 3.1.0 . So I exclude spring-security-web from Spring security , have a separate dependency for spring-security-web 3.1.0 which in turn excludes the spring-web 3.0.6 version and I provide a separate spring-web 3.1.0 version . This work but I feel there would be a much easier approach . I tried putting an exclusion for spring web under Spring security but it didn't work . Please help .

like image 640
Aravind A Avatar asked Feb 02 '12 19:02

Aravind A


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.

How do I exclude a dependency from all?

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.

What is exclusion in dependency?

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.

How do I make Maven dependency optional?

In order to exclude these special dependencies from the main project, we can apply Maven's <optional> tag to them. This forces any user who wants to use those dependencies to declare them explicitly. However, it does not force those dependencies into a project that doesn't need them.


2 Answers

You can utilize the dependency management mechanism.

If you create entries in the <dependencyManagement> section of your pom for spring-security-web and spring-web with the desired 3.1.0 version set the managed version of the artifact will override those specified in the transitive dependency tree.

I'm not sure if that really saves you any code, but it is a cleaner solution IMO.

like image 158
Mike Deck Avatar answered Sep 23 '22 16:09

Mike Deck


Global exclusions look like they're being worked on, but until then...

From the Sonatype maven reference (bottom of the page):

Dependency management in a top-level POM is different from just defining a dependency on a widely shared parent POM. For starters, all dependencies are inherited. If mysql-connector-java were listed as a dependency of the top-level parent project, every single project in the hierarchy would have a reference to this dependency. Instead of adding in unnecessary dependencies, using dependencyManagement allows you to consolidate and centralize the management of dependency versions without adding dependencies which are inherited by all children. In other words, the dependencyManagement element is equivalent to an environment variable which allows you to declare a dependency anywhere below a project without specifying a version number.

As an example:

  <dependencies>     <dependency>       <groupId>commons-httpclient</groupId>       <artifactId>commons-httpclient</artifactId>       <version>3.1</version>     </dependency>     <dependency>       <groupId>org.springframework</groupId>       <artifactId>spring-beans</artifactId>       <version>3.0.5.RELEASE</version>     </dependency>   </dependencies>   <dependencyManagement>     <dependencies>       <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-beans</artifactId>         <exclusions>           <exclusion>             <groupId>commons-logging</groupId>             <artifactId>commons-logging</artifactId>           </exclusion>         </exclusions>       </dependency>       <dependency>         <groupId>commons-httpclient</groupId>         <artifactId>commons-httpclient</artifactId>         <exclusions>           <exclusion>             <groupId>commons-logging</groupId>             <artifactId>commons-logging</artifactId>           </exclusion>         </exclusions>       </dependency>     </dependencies>   </dependencyManagement> 

It doesn't make the code less verbose overall, but it does make it less verbose where it counts. If you still want it less verbose you can follow these tips also from the Sonatype reference.

like image 21
Spencer Kormos Avatar answered Sep 22 '22 16:09

Spencer Kormos