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>
...
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.
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.
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.
Maven won't allow any other either. Build will fail if version is not found.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With