Maven project build life cycle

Maven project build life cycle

In a word: The Maven build lifecycle describes how many events a build process goes through

insert image description here

3 phases of the life cycle

clean

cleanup work

insert image description here

default

Core work, such as compiling, testing, packaging, deploying, etc.

insert image description here

site

Generate reports, publish sites, etc.

insert image description here

The lifecycle is executed in phases

In a word: The project construction life cycle is divided into many stages, not completely executed every time, but executed according to the user's requirements [for example, if you execute compile, then it will execute to the stage of complie, if you execute install, it will execute compile->test->package->install】

for example

1. Demo compile

insert image description here

2. Demo install

insert image description here

maven plugin

introduce

1. The plug-in is bound to a certain stage in the life cycle. When the corresponding life cycle is executed, the corresponding plug-in will complete the task/function .

2. There are many maven plug-ins, first look at a picture:

insert image description here

3. Other functions can be customized through plug-ins

4. Documentation: http://maven.apache.org/plugins/index.html

Custom plug-in - application example

Requirements: Add a custom plug-in to pom.xml, which can output the source code of the main program and test program when packaging the maven_D project

complete steps

  1. The current package will only get the jar of the project

insert image description here

  1. Modify D:\java_projects\maven_D\pom.xml, add the maven plug-in and configure it (Note: After adding a custom plug-in, it may become popular, just restart the project.)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         韩顺平Java 工程师
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wyx</groupId>
    <artifactId>maven_D</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <!-- 解读
            1. option 默认是false , 即不隐藏
            2. option 设置为true , 即隐藏-->
            <optional>false</optional>
            <!-- scope 设置为test, 让他在test 范围有效-->
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <!--在build 时,自定义的插件-->
    <build>
        <plugins>
            <plugin>
                <!-- 插件坐标-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <!-- 对主程序输出源码打包-->
                            <goal>jar</goal>
                            <!-- 对测试程序输出源码打包-->
                            <goal>test-jar</goal>
                        </goals>
                        <!-- 在generate-test-resources 阶段执行-->
                        <phase>generate-test-resources</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. Observe the maven project manager

insert image description here

  1. Execute the intall operation of maven-D again, and you will get two new jars, which contain the source code of the main program and the test program respectively. (Note: When testing, you need to ensure that there are java source codes in the src/main/… and src/test/… directories, otherwise the source code jar will not be generated)

insert image description here

  1. Unzip the jar and you can see the source code.

insert image description here

maven plugin-maven build life cycle diagram

insert image description here

diagram

  1. In the maven project construction life cycle, the execution of each stage is completed by the corresponding plug-in

  2. During the execution of each plug-in, there will be output content, such as jar/war/xml/source code

  3. Programmers can use maven default plug-ins, or customize plug-ins to complete custom tasks.

  4. The custom plug-in is introduced successfully, and it can be seen

insert image description here

Guess you like

Origin blog.csdn.net/apple_67445472/article/details/131710654