Maven advanced notes

learning target

  • Understand the meaning of module development
  • Ability to quickly build projects using Aggregate Engineering
  • Ability to use inheritance to simplify project configuration
  • Able to configure production, development, and testing environments according to needs, and switch operations between each environment

1. Sub-module development and design

1. The significance of sub-module development

Question import

What are the benefits of modular development to engineering?

Module splitting principle

Purpose: The project's scalability has become stronger, making it easier for other projects to reference the same functions.

Insert image description here

  • Split the original module into several sub-modules according to functions to facilitate mutual calls and interface sharing between modules.

Insert image description here

2. Module development (module splitting)

Question import

On what basis should a complete project be divided into modules?

2.1 Create Maven module

Insert image description here

2.2 Writing module code

Precautions:

  1. Modular development requires first designing the module functions and then coding. The project will not be developed first and then split
2.3 Install the module to the local warehouse through the maven command (install command)
mvn install

Precautions:

  1. Internal development within the team requires publishing module functions to a repository that can be shared within the team (private server)

2. Dependency management

  • Dependency management refers to the jars required to run the current project. A project can set multiple dependencies.

  • Format:

<!--设置当前项目所依赖的所有jar-->
<dependencies>
    <!--设置具体的依赖-->
    <dependency>
        <!--依赖所属群组id-->
        <groupId>org.springframework</groupId>
        <!--依赖所属项目id-->
        <artifactId>spring-webmvc</artifactId>
        <!--依赖版本号-->
        <version>5.2.10.RELEASE</version>
    </dependency>
</dependencies>

1. Dependency transfer

Question import

A depends on B, and B depends on C. Does A depend on C?

  • Dependencies are transitive
    • Direct dependencies: dependencies established through dependency configuration in the current project
    • Indirect dependency: If the resource being resourced depends on other resources, the current project indirectly depends on other resources.
    • Special priority: When different versions of the same resource are configured at the same level, the one configured later overwrites the one configured first.

Insert image description here

2. Optional dependencies

Question import

A depends on B, and B depends on C. If A doesn't want to depend on C, can it be done?

  • Optional dependencies refer to hiding the currently dependent resources from the outside - opaque
<dependency>
    <groupId>com.itheima</groupId>
    <artifactId>maven_03_pojo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--可选依赖是隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递性-->
    <optional>false</optional>
</dependency>

3. Eliminate dependencies

Question import

A depends on B, and B depends on C. If A doesn't want to depend on C, can it be done?

  • Excluding dependencies means actively disconnecting dependent resources. Excluded resources do not need to specify versions————No need
  • To exclude dependent resources, only specify GA, no need to specify V
<dependency>
    <groupId>com.itheima</groupId>
    <artifactId>maven_04_dao</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--排除依赖是隐藏当前资源对应的依赖关系-->
    <exclusions>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

4 The difference between optional dependencies and excluded dependencies

Insert image description here

3. Aggregation and inheritance

1. Polymer Engineering

Question import

What is aggregation?

  • Aggregation: The process of organizing multiple modules into a whole and building the project at the same time is called aggregation

  • Aggregation project: usually an "empty" project with no business functions (there is only one pom file)

  • Function: Multiple projects can be grouped using aggregation projects. By building the aggregation project, the included modules can be constructed simultaneously.

    • When a module in the project is updated (changed), it must be ensured that the modules associated with the updated module in the project are updated simultaneously. At this time, aggregation projects can be used to solve the problem of synchronized building of batch modules.

Insert image description here

2. Polymer engineering development

Question import

What are the packaging methods for projects?

2.1 Create a Maven module and set the packaging type to pom
<packaging>pom</packaging>

Precautions:

  1. Each maven project has a corresponding packaging method, the default is jar, and the web project packaging method is war
2.2 Set the name of the submodule included in the current aggregation project
<modules>
    <module>../maven_ssm</module>
    <module>../maven_pojo</module>
    <module>../maven_dao</module>
</modules>

Precautions:

  1. When building the modules included in the aggregation project, the build order will be set according to the dependencies between modules, regardless of the writing position of the module configuration in the aggregation project.
  2. Projects participating in aggregation cannot sense upward whether to participate in aggregation, and can only configure downward which modules participate in the aggregation of this project.

3. Inheritance relationship

Question import

What is inheritance?

  • concept:
    • Inheritance describes the relationship between two projects. It is similar to inheritance in Java. Subprojects can inherit configuration information in the parent project, which is common in inheritance of dependencies.
  • effect:
    • Simplify configuration
    • Reduce version conflicts

Insert image description here

4. Development of inheritance relationships

4.1 Create a Maven module and set the packaging type to pom
<packaging>pom</packaging>

Precautions:

  1. It is recommended that the parent project packaging method be set to pom
4.2 Configure dependencies in the pom file of the parent project (the sub-project will inherit the dependencies in the parent project)
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    ……
</dependencies>
4.3 Configure optional dependencies in subprojects
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        ……
    </dependencies>
</dependencyManagement>
4.3 Configure the parent project inherited by the current project in the sub-project
<!--定义该工程的父工程-->
<parent>
    <groupId>com.itheima</groupId>
    <artifactId>maven_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--填写父工程的pom文件,根据实际情况填写-->
    <relativePath>../maven_parent/pom.xml</relativePath>
</parent>
4.5 Configure in the sub-project to use the coordinates of the optional dependencies in the parent project
<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
</dependencies>

Precautions:

  1. When using optional dependencies in the parent project in a sub-project, only the group ID and project ID need to be provided, and there is no need to provide the version. The version is provided by the parent project to avoid version conflicts.
  2. Subprojects can also define dependencies that are not defined in the parent project.

5. The difference between aggregation and inheritance

Question import

What is the role of aggregation and inheritance?

  • effect
    • Aggregation for quickly building projects
    • Inheritance for quick configuration
  • Same point:
    • The packaging method of pom.xml files for aggregation and inheritance is pom, and the two relationships can be made into the same pom file.
    • Aggregation and inheritance are both design modules and have no actual module content.
  • difference:
    • Aggregation is to configure relationships in the current module. Aggregation can sense which modules participate in aggregation.
    • Inheritance is to configure relationships in sub-modules. The parent module cannot sense which sub-modules inherit it.

4. Attribute management

1. Properties

Question import

What are the benefits of defining properties?

Insert image description here

1.1 Attribute configuration and usage
①: Define attributes
<!--定义自定义属性-->
<properties>
    <spring.version>5.2.10.RELEASE</spring.version>
    <junit.version>4.12</junit.version>
</properties>
②: Reference attribute
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
1.2 Resource file reference attributes
①: Define attributes
<!--定义自定义属性-->
<properties>
    <spring.version>5.2.10.RELEASE</spring.version>
    <junit.version>4.12</junit.version>
    <jdbc.url>jdbc:mysql://127.0.0.1:3306/ssm_db</jdbc.url>
</properties>
②: Reference attributes in the configuration file
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
jdbc.username=root
jdbc.password=root
③: Turn on the filter for the resource file directory loading attribute
<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>
④: When configuring maven to build a war package, ignore the web.xml check
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.3</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
</plugin>
1.3 Other attributes (understand)
  • Property list
    • Custom attributes (commonly used)
    • Built-in properties
    • Setting property
    • Java system properties
    • environment variable properties

Insert image description here

2. Version management

Question import

What types of versions can the project development be divided into?

2.1 Project version
  • SNAPSHOT (snapshot version)
    • The version temporarily output during the project development process is called a snapshot version
    • Snapshot versions will be continuously updated as development progresses
  • RELEASE (release version)
    • After the project reaches the milestone, a more stable version will be released to the outside of the team. The component files corresponding to this version are stable.
    • Even if the function is subsequently developed, the content of the current release version will not be changed. This version is called a release version.

Insert image description here

2.2 Release version
  • alpha version
  • beta version
  • Digital only version

5. Multi-environment configuration and application

1. Multi-environment configuration function

Question import

What are the benefits of multi-environment configuration?

  • Maven provides settings for configuring multiple environments to help developers quickly switch environments during use.

Insert image description here

2. Multi-environment configuration steps

2.1 Define multiple environments
<!--定义多环境-->
<profiles>
    <!--定义具体的环境:生产环境-->
    <profile>
        <!--定义环境对应的唯一名称-->
        <id>env_dep</id>
        <!--定义环境中专用的属性值-->
        <properties>
            <jdbc.url>jdbc:mysql://127.0.0.1:3306/ssm_db</jdbc.url>
        </properties>
        <!--设置默认启动-->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!--定义具体的环境:开发环境-->
    <profile>
        <id>env_pro</id>
        ……
    </profile>
</profiles>
2.2 Using multiple environments (build process)
【命令】:
mvn 指令 –P 环境定义id

【范例】:
mvn install –P pro_env

2. Skip the test (understand)

Question import

Will skipping tests affect the project's build process?

2.1 Application scenarios
  • The function is being updated and has not been developed yet.
  • Quick packaging
  • ……
2.2 Skip test command
  • mvn install –D skipTests

Precautions:

  1. The executed project build instructions must include the test life cycle, otherwise it will have no effect. For example, the compile life cycle is executed without going through the test life cycle.
2.3 Fine-grained control of skip testing
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
    <configuration>
        <skipTests>true</skipTests>
        <!--设置跳过测试-->
        <includes>
            <!--包含指定的测试用例-->
            <include>**/User*Test.java</include>
        </includes>
        <excludes>
            <!--排除指定的测试用例-->
            <exclude>**/User*TestCase.java</exclude>
        </excludes>
    </configuration>
</plugin>

6. Private server

1. Private server introduction

Question import

What is the difference between the private server here and the national server, trial server, European server, etc. that we usually hear about?

1.1 Introduction
  • Team development status analysis

  • The private server is an independent server used to solve the problem of resource sharing and resource synchronization within the team.

  • Nexus

    • A maven private server product from Sonatype
    • Download address: https://help.sonatype.com/repomanager3/download

Insert image description here

1.2 Nexus installation and startup
  • Start the server (command line startup)

    • nexus.exe /run nexus
  • Access the server (default port: 8081)

    • http://localhost:8081
  • Modify basic configuration information

    • The nexus-default.properties file in the etc directory under the installation path saves nexus basic configuration information, such as the default access port.
  • Modify server running configuration information

    • The nexus.vmoptions file in the bin directory under the installation path saves the configuration information corresponding to the nexus server startup, such as the default memory space occupied.
1.3 Analysis of private server resource operation process

Insert image description here

2. Private server warehouse classification

Question import

What types of private server warehouses are there?

Insert image description here

3. Resource uploading and downloading

Question import

Does uploading resources to a private server require identity authentication? Where to set authentication information?
Insert image description here

3.1 Download dependencies from private servers

[Step 1] Configure in the <mirrors> tag in maven's settings.xml. At this time, you need to comment out the aliyun configuration.

<mirror>
    <id>nexus-heima</id>
    <mirrorOf>*</mirrorOf>
    <url>http://localhost:8081/repository/maven-public/</url>
</mirror>

[Step 2] Set in nexus to allow anonymous downloads. If not allowed, dependencies will not be downloaded from the private server.

Insert image description here

If there is no corresponding jar in the private server, it will be downloaded from the central warehouse, which is very slow. You can configure the private server to download dependencies from Alibaba Cloud.

Insert image description here

3.2 Upload dependencies to the private server

[Step 1] Configure the permissions of the local warehouse to access the private server (configured in the servers tag of maven's settings.xml)

<server>
  <!--id任意,多个server的id不重复就行,后面会用到-->
  <id>heima-nexus</id>
  <username>admin</username>
  <password>123456</password><!--填写自己nexus设定的登录秘密-->
</server>

[Step 2] Configure the storage location for the current project to access private server upload resources (configured in the project's pom.xml file)

<distributionManagement>
    <repository>
      	<!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码-->
        <id>heima-nexus</id>
      	<!--如果jar的版本是release版本,那么就上传到这个仓库,根据自己情况修改-->
        <url>http://localhost:8081/repository/heima-releases/</url>
    </repository>
    <snapshotRepository>
      	<!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码-->
        <id>heima-nexus</id>
      	<!--如果jar的版本是snapshot版本,那么就上传到这个仓库,根据自己情况修改-->
        <url>http://localhost:8081/repository/heima-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

Note: It must correspond to the <id>heima-nexus</id> defined in the server in maven's settings.xml

[Step 3] Publish resources to private server command

mvn deploy

对应的用户名和密码-->
        <id>heima-nexus</id>
      	<!--如果jar的版本是release版本,那么就上传到这个仓库,根据自己情况修改-->
        <url>http://localhost:8081/repository/heima-releases/</url>
    </repository>
    <snapshotRepository>
      	<!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码-->
        <id>heima-nexus</id>
      	<!--如果jar的版本是snapshot版本,那么就上传到这个仓库,根据自己情况修改-->
        <url>http://localhost:8081/repository/heima-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

Guess you like

Origin blog.csdn.net/weixin_45417754/article/details/126304746