5

I am working with a Maven project where I have spring framework dependency version 3.2.3.RELEASE and I have to import this project into many others but in some of them I am using spring 4.

How can I exclude this dependency (spring 3) only in case that the project that uses mine has the same or newer version of spring and in those who hasn't use mine?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    See [Maven: how to override the dependency added by a library](http://stackoverflow.com/questions/3937195/maven-how-to-override-the-dependency-added-by-a-library) and [Override parent pom dependency](http://stackoverflow.com/questions/28770922/override-parent-pom-dependency). – Gerold Broser Apr 19 '16 at 14:30

1 Answers1

2

One thing you can do is to manually exclude unwanted dependencies:

    <dependency>
        <groupId>com.your.project</groupId>
        <artifactId>project-name</artifactId>
        <version>1.0.0</version>
        <exclusions>
            <exclusion>
                <artifactId>org.springframework</artifactId>
                <groupId>spring-core</groupId>
            </exclusion>
        </exclusions>
    </dependency>

But that can get quite verbose.

It may help if you elaborate a bit more on the problem description as it is not clear to me what exactly are you trying to solve or achieve.

For instance if you include a dependency A which has a dependency B in version 1.0 and then you include B in your pom in 1.1 Maven will use only 1.1. I recommend reading up on it a bit here: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies

Ondrej Burkert
  • 6,617
  • 2
  • 32
  • 27
  • Ok, you've sum it up, that is really what I am trying to do. But in case I have a proyect B with an older version of spring declared in POM and proyect A with spring 3 or newer version declared in POM. If I add proyect A to B's POM Maven will use the older version of spring, am I right? – Guido Berezovsky Apr 19 '16 at 14:28
  • Yeah. That's how I understand it. I recently needed to do a similar thing for Jackson dependency where parent defined 2.7 but one library (wiremock) needed 2.6 so I defined 2.6 as a dependency right next to wiremock in respective module. – Ondrej Burkert Apr 20 '16 at 05:50