maven advanced summary

1. Maven can import packages from each other

From the pom file of the current maven, find the corresponding coordinates, and then import and change the coordinates in other mavens, but thinking of importing the coordinates,Need to use the install in the life cycle, must be installed locally before it can be recognized and compiled successfully in other packages.

<dependency>
          <groupId>org.example</groupId>
          <artifactId>maven_03_pojo</artifactId>
          <version>1.0-SNAPSHOT</version>
      </dependency>

2. Maven's dependencies are transitive

For example : I maven_03_pojoimported it in org.mybatis, then I maven_02_ssmdon’t need to import the coordinates in my , and I can also recognize this package. This is maven’s dependency transfer.

insert image description here

3. Optional dependencies and excluded dependencies:

Optional dependencies: I hide my stuff and don't want others to use it

<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
            <!-- 可选依赖是隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递性-->
            <optional>true</optional>
        </dependency>

Exclude dependencies: I use other people's things, some I don't want to use

<dependency>
    <groupId>org.example</groupId>
    <artifactId>maven_04_dao</artifactId>
    <version>1.0-SNAPSHOT</version>
    <exclusions>
      <exclusion>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions>
  </dependency>

4. Inheritance and Aggregation

4.1 Aggregation

insert image description here
step:

  • 4.1.1. Create a Maven module and set the packaging type to pom
    	<packaging>pom</packaging>
    
  • 4.1.2. Set the submodule name contained in the current aggregation project
    	 <!--设置管理模块的名称-->
    <modules>
        <module>../maven_02_ssm</module>
        <module>../maven_03_pojo</module>
        <module>../maven_04_dao</module>
    </modules>
    

The modules contained in the aggregation project will set the construction order according to the dependencies between the modules when they are built. It has nothing to do with the configuration writing position of the modules in the aggregation project. The projects participating in the aggregation cannot sense upward whether to participate in the aggregation, and can only configure which modules downward. Participate in the aggregation of this project

4.2 Inheritance

insert image description here
step:

  • 4.2.1. Create a Maven module and set the packaging type to pom
    	<packaging>pom</packaging>
    
  • 4.2.2. Configure dependencies in the pom file of the parent project (the child project will inherit the dependencies in the parent project)
  • 4.2.3 Configure optional dependencies in subprojects
    	 <!--设置管理模块的名称  子工程不需要配置版本-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.12</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
  • 4.2.4 Configure the parent project inherited by the current project in the subproject
    	    <parent>
        <groupId>org.example</groupId>
        <artifactId>maven_01_parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../maven_01_parent/pom.xml</relativePath>   这一行不写也能用,帮忙定位位置的
    </parent>
    
  • 4.2.5 Configure the optional dependent coordinates in the parent project in the subproject
    	 <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
    

    When using optional dependencies in the parent project in the sub-project, only the group id and project id need to be provided, and the version is not required. The version is provided by the parent project to avoid version conflicts. Sub-projects can also define dependencies that are not defined in the parent project relation

insert image description here

5. Configuration and use of attributes

  • 5.1 Define properties
    <!--定义属性-->
    <properties>
        <spirng.version>5.2.0.RELEASE</spirng.version>
        <log4j.version>1.2.12</log4j.version>
        <mybatis-spring.version>1.3.0</mybatis-spring.version>
    </properties> 
    
  • 5.2 Reference properties
    <!--定义属性-->
    <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>
    

6. Version management

insert image description here

8. Multi-environment development and skip testing (understand)

  1. a
  2. a

Guess you like

Origin blog.csdn.net/missgrass/article/details/129240488