简体   繁体   中英

download compatible dependency in maven

Supppose I use spring 4.0 in my pom file and I want to use junit. In that case how can I declare the junit dependency to download the which compatible with the given spring version with out specify any specific junit version. This can be any dependency where I take junit as a example.

In Maven there are two possible matching things. First the dependency management of the artefact itself. spring-core:4.2.0 defines that it depends on commons-codec:1:10. So there is nothing to do for you when you want to also use commons-codec since its already in your classpath. It did not define JUnit so it did not depend on it and should be compatible with all versions.

The second thing is a BOM pom . This is used to package a lot of dependencies together and let the user select the once he needs but the bom defines the versions for you already (and so they should be compatible). Especially spring has some of this bom poms in the repo.

For example spring-framework-bom which packaged everything related to spring which you could use in your app but you will define which parts you need and do not bothering yourself with the version numbers of the sub dependencies.

For example I want to use spring 4.2 and need webmvc the jdbc stuff and something for spring tests. So will define this:

<dependencymanagement>
    <dependencies>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-framework-bom</artifactid>
            <type>pom</type>
            <version>4.2.0.RELEASE</version>
            <scope>import</scope>
        </dependency>
    <dependencies>
</dependencymanagement>
<dependencies>
    <dependency>
        <groupid>org.springframework</groupid>
        <artifactid>spring-webmvc</artifactid>
    </dependency>
    <dependency>
        <groupid>org.springframework</groupid>
        <artifactid>spring-jdbc</artifactid>
    </dependency>
    <dependency>
        <groupid>org.springframework</groupid>
        <artifactid>spring-test</artifactid>
        <scope>test</scope>
    </dependency>
</dependencies>

This question doesn't really make sense. If spring 4.0 depends on JUnit (you don't say which spring module you're referring to), JUnit (or any other dependency) will automatically be included in your dependencies, since it will be inherited from the spring 4.0 dependency you have already declared, and the version will match whatever version the Spring module declares in its POM.

However, it seems likely that your particular Spring 4.0 dependency does not depend on JUnit, so in which case you may just pick which ever version is suitable for your requirements.

You can view the dependencies which are currently included in your project (explicit and inherited) by running the following command:

mvn dependency:list

This will trace down the dependency tree and show you all the dependencies which are currently included in your project.

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