Issue
there is another way to change the version of a maven dependency that is onto another dependency? i did that like this and works fine but, i want to know if there's another way more cleaner to do that.
Example:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Dependency that i want to update -->
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.4</version>
</dependency>
Solution
In my experience, the best practice is to do this with dependencyManagement, and only set the version for the dependency (not scope). This will enforce the version of the dependency with minimal effect on the dependency tree.
Adding a direct dependency like you do now, wrongfully signals that your code refers to jackson-annotations apis directly, and may also have side effects on the set of transitive dependencies.
<project>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.4</version>
</dependency>
</dependencies>
</dependencyManagement>
...
Btw, I think that the exclusion in your example is unnecessary. You can verify that by running mvn dependency:tree
.
Answered By - gjoranv
Answer Checked By - Pedro (JavaFixing Volunteer)