Reputation: 6126
I have 2 questions related to this topic. I want to import the following dependencies into my project:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
Here are my 2 questions
Upvotes: 1
Views: 699
Reputation: 153
As far as I know there is no way to import all three artifacts with one import statement, even though they use the same groupId. However you're doing the right thing by using a variable to make sure the versions are all the same. This helps if you are seeing conflicts in version numbers for different dependencies, which leads to the next answer...
Not exactly. Both dependencies will be included in your class path, which could cause conflicts later on (or problems where functions from the newer versions can't be resolved because Java is finding the old one first). There are two methods of dealing with this. One is to use the Maven Dependency plugin to search for conflicts that might occur if you do not know for sure. And then once you understand the conflict (like in the case of Springboot and your software), you can exclude the specific dependency from being brought in by Springboot, thereby causing your version to be the one used by every other dependency. You can do that by adding exclusions when you declare the Springboot dependency:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Edit: As khmarbaise mentioned, according to Maven's documentation on the matter, the dependency that is "nearest" to your project (i.e. declared in your pom instead of the pom of a dependency), will be used first by your program, so you shouldn't have to exclude the artifacts. However, I haven't found this to always work in practice (or maybe something else is going on). Exclusion is useful in the other case where you are creating a shaded Jar with all the dependencies inside and you don't want to include multiple versions of the same one.
Upvotes: 4