[Excerpt] DependencyManagement and Dependencies in Maven

Here is a difference between declaring dependencyManagement and dependencies in the root node of the parent project

dependencyManagement

Maven uses the dependencyManagement element to provide a way to manage dependency version numbers. The dependencyManagement element is usually seen in the top-most parent POM of an organization or project. Using the dependencyManagement element
in pom.xml allows all subprojects to reference a dependency without explicitly listing the version number. Maven will walk up the parent-child hierarchy until it finds a project with a dependencyManagement element, then it will use the version number specified in the dependencyManagement element.

 

For example in the parent project:

Xml code   Favorite code
  1. <dependencyManagement>  
  2. <dependencies>  
  3. <dependency>  
  4. <groupId>mysql</groupId>  
  5. <artifactId>mysql-connector-java</artifactId>  
  6. <version>5.1.2</version>  
  7. </dependency>  
  8. ...  
  9. <dependencies>  
  10. </dependencyManagement>  

Then you can add mysql-connector in the subproject without specifying the version number, for example:

 

Xml code   Favorite code
  1. <dependencies>  
  2. <dependency>  
  3. <groupId>mysql</groupId>  
  4. <artifactId>mysql-connector-java</artifactId>  
  5. </dependency>  
  6. </dependencies>  

The advantage of this is that if there are multiple subprojects that reference the same dependency, you can avoid declaring a version number in each subproject used, so that when you want to upgrade or switch to another version, you only need to be at the top level Updates in the parent container do not need to be modified one by one sub-project; in addition, if a sub-project needs another version, just declare the version.

 

DependencyManagement only declares dependencies and does not implement import, so subprojects need to explicitly declare the dependencies they need to use.

dependencies

Compared with dependencyManagement, all dependencies declared in dependencies are automatically imported and inherited by default by all sub-projects.

 

classifier

If you are publishing the same code, but need to generate two separate artifacts for technical reasons, you will use a classifier. For example, if you wanted to build two separate artifacts into JARs, one using the Java 1.4 compiler and the other using the Java 6 compiler, you could use the classifier
to generate two separate JAR artifacts with the same groupId :artifactId:version combination. If your project uses the native extension library, you can use the classifier to generate one artifact for each target platform. Classifiers are often used to package source code, JavaDoc or binary collections of artifacts.

 

 

 

Can also be referenced in child pom

  <dependencyManagement>

        <dependencies>

            <dependency>

                <!-- Import dependency management from Spring Boot -->

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-dependencies</artifactId>

                <version>1.1.6.RELEASE</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

        </dependencies>

    </dependencyManagement>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326513881&siteId=291194637