Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a maven dependency if in the parent project exists a newer version of it

Tags:

java

maven

I am working with a Maven project where I have spring framework dependency version 3.2.3.RELEASE and I have to import this project into many others but in some of them I am using spring 4.

How can I exclude this dependency (spring 3) only in case that the project that uses mine has the same or newer version of spring and in those who hasn't use mine?

like image 965
Guido Berezovsky Avatar asked Oct 30 '22 05:10

Guido Berezovsky


1 Answers

One thing you can do is to manually exclude unwanted dependencies:

    <dependency>
        <groupId>com.your.project</groupId>
        <artifactId>project-name</artifactId>
        <version>1.0.0</version>
        <exclusions>
            <exclusion>
                <artifactId>org.springframework</artifactId>
                <groupId>spring-core</groupId>
            </exclusion>
        </exclusions>
    </dependency>

But that can get quite verbose.

It may help if you elaborate a bit more on the problem description as it is not clear to me what exactly are you trying to solve or achieve.

For instance if you include a dependency A which has a dependency B in version 1.0 and then you include B in your pom in 1.1 Maven will use only 1.1. I recommend reading up on it a bit here: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies

like image 55
Ondrej Burkert Avatar answered Nov 15 '22 05:11

Ondrej Burkert