【每日学习】【2】【maven】生命周期

82 阅读3分钟

生成第一个maven项目

mvn -B archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
<?xml version="1.0" encoding="UTF-8"?> <!-- xml: xml标识 -->
<!-- project: pom文件根元素 -->
<project xmlns="http://maven.apache.org/POM/4.0.0" 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><!-- modelVersion: 模型版本,根据版本不同模型内容也不同,这里学习4.0.0版本 -->

    <groupId>com.mycompany.app</groupId><!-- 表明创作者身份或组织 -->
    <artifactId>my-app</artifactId><!-- 对外发布的软件名称(唯一) -->
    <version>1.0-SNAPSHOT</version><!-- 软件版本号 -->

    <name>my-app</name><!-- 用于在生成的文档显示该软件的名字 -->

    <url>http://www.example.com</url><!-- 用于在生成的文档显示该软件的主页 -->

    <properties><!-- 相当于声明一些变量,在pom任何地方使用,一般用于声明版本 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 单个变量 -->
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies><!-- 声明软件中使用的依赖的集合 -->
        <dependency><!-- 单个依赖 -->
            <groupId>junit</groupId><!-- 依赖包组织名 -->
            <artifactId>junit</artifactId><!-- 依赖包标识 -->
            <version>4.11</version><!-- 依赖包版本 -->
            <scope>test</scope><!-- 依赖包作用域 -->
        </dependency>
    </dependencies>

    <build><!-- 声明项目的目录结构和管理插件 -->
        <pluginManagement><!-- 为了避免使用默认版本,锁定下面插件的版本 (可以移动到父pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
  • 尝试编译

    mvn compile

    maven-resources-plugin maven-compiler-plugin

  • 测试

    mvn test

    maven-resources-plugin:resources maven-compiler-plugin:compile maven-resources-plugin:testResources maven-compiler-plugin:testCompile maven-surefire-plugin:test

  • 打包

    mvn package

    maven-resources-plugin:resources maven-compiler-plugin:compile maven-resources-plugin:testResources maven-compiler-plugin:testCompile maven-surefire-plugin:test maven-jar-plugin:jar

  • 清理

    mvn clean

    maven-clean-plugin:clean

Phase

可以在尝试中发现在每一个生命周期中包含多个Phase,每个Phase执行一个目标。

Clean Lifecycle

PhaseDescription
pre-cleanexecute processes needed prior to the actual project cleaning
cleanremove all files generated by the previous build
post-cleanexecute processes needed to finalize the project cleaning

Default Lifecycle

PhaseDescription
validatevalidate the project is correct and all necessary information is available.
initializeinitialize build state, e.g. set properties or create directories.
generate-sourcesgenerate any source code for inclusion in compilation.
process-sourcesprocess the source code, for example to filter any values.
generate-resourcesgenerate resources for inclusion in the package.
process-resourcescopy and process the resources into the destination directory, ready for packaging.
compilecompile the source code of the project.
process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sourcesgenerate any test source code for inclusion in compilation.
process-test-sourcesprocess the test source code, for example to filter any values.
generate-test-resourcescreate resources for testing.
process-test-resourcescopy and process the resources into the test destination directory.
test-compilecompile the test source code into the test destination directory
process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes.
testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package.
packagetake the compiled code and package it in its distributable format, such as a JAR.
pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.
verifyrun any checks to verify the package is valid and meets quality criteria.
installinstall the package into the local repository, for use as a dependency in other projects locally.
deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Site Lifecycle

PhaseDescription
pre-siteexecute processes needed prior to the actual project site generation
sitegenerate the project's site documentation
post-siteexecute processes needed to finalize the site generation, and to prepare for site deployment
site-deploydeploy the generated site documentation to the specified web server

Phase与goal

Clean Lifecycle Bindings

Phaseplugin:goal
cleanclean:clean

Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war

Phaseplugin:goal
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packageejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging ear

Phaseplugin:goal
generate-resourcesear:generate-application-xml
process-resourcesresources:resources
packageear:ear
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging maven-plugin

Phaseplugin:goal
generate-resourcesplugin:descriptor
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packagejar:jar and plugin:addPluginArtifactMetadata
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging pom

Phaseplugin:goal
package
installinstall:install
deploydeploy:deploy

Site Lifecycle Bindings

Phaseplugin:goal
sitesite:site
site-deploysite:deploy