Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - why can't I override the version of a dependencyManagement imported pom?

Tags:

java

maven

I'm looking for an explanation about this case: I have a project that has a dependencyManagement / dependencies section with:

myproject/pom.xml:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>Brixton.M3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

The spring-cloud-starter-parent pom defines:

<properties>
        <main.basedir>${basedir}/../..</main.basedir>
        <spring-cloud-netflix.version>1.1.0.BUILD-SNAPSHOT</spring-cloud-netflix.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
                <version>${spring-cloud-netflix.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

This way, my project will get spring-cloud-starter-eureka version 1.1.0.BUILD-SNAPSHOT.

I was expecting that if I add in myproject/pom.xml a properties section with another version I would override the default version, however it is ignored, why?

myproject/pom.xml

...
<properties>
        <spring-cloud-netflix.version>1.0.0</spring-cloud-netflix.version>
</properties>
...
like image 939
codependent Avatar asked Dec 01 '15 09:12

codependent


People also ask

How do you override a dependency version in Pom?

Overriding Solved Dependency Versions How do you do this if the wrong dependency is a transitive dependency? By taking advantage of Maven's nearest definition logic, developers can override the version of a dependency by declaring it on the root pom. xml file.

How do I exclude a specific version of a dependency in Maven?

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 happens if you don't specify version in POM xml?

of your question: If you don't specify a version there are various different outcomes: a) you will get an error... b) if you have defined the version in the dependency management of the project's parent's pom, then that version is taken. The parent of the project doesn't have to be an enclosing superproject.

What happens if you don't specify a version in Maven?

Maven won't allow any other either. Build will fail if version is not found.


1 Answers

The property spring-cloud-netflix.version has already been resolved when building the org.springframework.cloud:spring-cloud-starter-eureka artifact, so you can not just override this when declaring the dependency.

To properly specify a certain dependency version, you have to add the version of that dependency in the dependency management section of your pom:

...
<properties>
        <spring-cloud-netflix.version>1.0.0</spring-cloud-netflix.version>
</properties>
...
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>Brixton.M3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
                <version>${spring-cloud-netflix.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

As a best practice for a multi module build you would typically have a shared master pom containing the dependency management settings incl. all version information for all your dependencies.

like image 95
Torsten Avatar answered Oct 10 '22 13:10

Torsten