简体   繁体   中英

How to prevent from overriding parent pom dependency version in child pom

I have parent pom with such config:

<dependencyManagement>  
    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.9</version>
        </dependency>
    </dependencies>
</dependencyManagement>

And my child pom:

<dependencies>
    <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
    </dependency>
</dependencies>

I want to use 4.3.5 version in my classpath, because at the moment I am getting error, parent version should not be modified:

java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients

Any ideas how to prevent from overriding that 4.2.9 version?

As you know that your parent pom dependency is include in the child pom then don't need to write dependency in the child pom.xml . To include the parent pom dependency in child use

<dependency>
            <groupId>${defined groupId of parent}</groupId>
            <artifactId>${artifact defined for parent }</artifactId>
            <version>${version defined for parent}</version>
        </dependency>

In your parent Pom file for the specified Dependency add scope as Provided

<dependencyManagement>  
    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.2.9</version>
            <scope>provided</scope> 
        </dependency>
    </dependencies>
</dependencyManagement>

But As @Deltharis commented better to use the dependency only in the parent POM

I changed parent version to desired and removed dependency from child (the simple and the best solution). Thanks goues to @Deltharis for the comments!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM