当前位置:网站首页>pom. XML configuration file label: differences between dependencies and dependencymanagement

pom. XML configuration file label: differences between dependencies and dependencymanagement

2022-07-07 20:00:00 Whiteye too white

dependencyManagement Dependency management

Usually in the top-level parent of a project pom Use in dependencyManagement,
Use pom.xml Medium dependencyManagement Element allows all subprojects to reference a dependency without listing the version number . In this way, the dependent version number in the child project will use the parent project pom The dependent version in the file .

When a dependency of a subproject does not specify a version number ,maven Will go up the father and son level , Until you find someone who has dependencyManagement Elements of the project , And then it will use this dependencyManagement The version specified in the element .

The good thing is : If multiple subprojects refer to the same dependency , You can avoid declaring a version number in each used subproject , So when you want to upgrade or switch to another version , Just update in the top-level parent container , It doesn't need to be modified one by one ; In addition, if a sub project requires another version , You just need to be yourself pom Just state the version number in .

Be careful :

  • dependencyManagement It's just a statement of dependency , No introduction , So the child project needs to display the required dependencies for the declaration
  • If you don't declare dependencies in subprojects , Is not inherited from the parent project ; Only the dependency is written in the subproject , And no specific version was specified , Will inherit the item from the parent project , also version and scope Read from parent pom.
  • If the version number is specified in the subproject , Then you will use the... Specified in the subproject jar edition .

dependencies Dependency relationship

Introduce automatic declaration in dependencies All the dependencies in , It is inherited by all subprojects by default . If no dependencies are written in the project , From the parent project
Inherit ( All properties inherit ) Declared in the parent project dependencies Dependencies in .

1. Single module

<!-- Just manage the version number , It won't actually introduce jar-->
<dependencyManagement>  
      <dependencies>  
            <dependency>
            		<!--jar Package identity limit -->
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>  
                <!-- Declaration of version number -->
                <version>3.2.7</version>
            </dependency>  
    </dependencies>  
</dependencyManagement>  
  
<!-- Will actually download the declared dependencies jar package -->
<dependencies>  
       <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-core</artifactId>
                <!-- Do not declare version  label , Will inherit dependencyManagement-->
       </dependency>  
</dependencies>

2. Multi module situation

Parent module pom.xml

<!--parent-module Parent module pom.xml-->
<properties>
    <!-- Unified management jar Package version . Focus on the parent module properties All dependent version numbers are defined in the tag .-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.eclipse.persistence.jpa.version>1.2.6</org.eclipse.persistence.jpa.version>
    <javaee-api.version>1.8</javaee-api.version>
</properties>
 
<dependencyManagement>  
    <!-- Define the version number of the public dependency -->
    <dependencies> 
        <dependency>  
            <groupId>org.eclipse.persistence</groupId>  
            <artifactId>org.eclipse.persistence.jpa</artifactId>  
            <version>${org.eclipse.persistence.jpa.version}</version>  
            <scope>provided</scope>  
        </dependency>  
          
        <dependency>  
            <groupId>javax</groupId>  
            <artifactId>javaee-api</artifactId>  
            <version>${javaee-api.version}</version>  
        </dependency>  
    </dependencies>  
</dependencyManagement> 

Sub module pom.xml

<!--son-module Sub module pom.xml-->
<!-- Inherited parent class -->  
<parent>
    <!-- Declare the identity information of the parent class -->
    <artifactId>parent-module</artifactId>
    <groupId>com.ppd</groupId>  
    <version>0.0.1-SNAPSHOT</version> 
    <!-- Declare the of the parent class pom File path -->
    <relativePath>../parent-module/pom.xml</relativePath>
</parent>  
 
<modelVersion>4.0.0</modelVersion>  
<artifactId>son-module</artifactId>  
<packaging>ejb</packaging>  
  
<!-- Dependency relationship -->  
<dependencies>  
    <dependency>  
        <groupId>javax</groupId>  
        <artifactId>javaee-api</artifactId>
        <!-- If not declared, inherit the parent class version、scope-->
    </dependency>  
      
    <dependency>  
        <groupId>com.fasterxml.jackson.core</groupId>  
        <artifactId>jackson-annotations</artifactId>
        <!-- Declaration does not inherit the parent class version-->
        <version>1.8<version/>
        <!-- Inherited parent class scope-->
    </dependency>  
      
    <dependency>  
        <groupId>org.eclipse.persistence</groupId>  
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <!-- If not declared, inherit the parent class version-->
        <scope>provided</scope>
    </dependency>  
</dependencies> 
原网站

版权声明
本文为[Whiteye too white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071749178046.html