Richard
Richard

Reputation: 6126

Importing maven dependencies

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

  1. Is there a way to import all 3 of those with 1 import statement? They all have the same groupId and those 3 are the only ones in that groupId. So can I just leave out the artifactId and just import everything in the groupId?
  2. Springboot seems to have imported these jackson libraries but older versions (2.6.5). When I put those dependencies (2.7.3) will that override the springboot dependency?

Upvotes: 1

Views: 699

Answers (1)

jaydez
jaydez

Reputation: 153

1. Smaller Important Statements for the same groupId

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...

2. Resolving Dependency Conflicts

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

Related Questions