美文网首页
Maven 生命周期

Maven 生命周期

作者: 酱油和醋 | 来源:发表于2017-08-29 15:12 被阅读0次

1.生命周期

maven有三个独立的有序的生命周期,分别是cleandefaultsite。其中clean(用以清理项目)分为pre-clean,clean和post clean。
default(or Build 用以构建项目),生命周期阶段如下表:

default生命周期
site(建立项目站点)分为pre-site,site,post-site和site-deploy。

常用的mvn clean deploy:实际执行的阶段为clean生命周期的pre clean, clean以及default生命周期的所有。

2.生命周期和插件

1.maven的生命周期是模板方法,抽象了构建的各个步骤,定义了它们的次序,而具体的实现是通过插件完成的。
2.将生命周期的阶段和插件目标相互绑定,可以完成实际的所需任务。
3.maven有内置的生命周期阶段和插件目标的绑定


clean&site
default jar打包

2.1绑定关系

2.1.1 自定义绑定

通过pom.xml配置绑定关系

<build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <!--在maven的initialize阶段,将Jacoco的runtime agent作为VM的一个参数传给被测程序,用于监控JVM中的调用。-->
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>

                        <configuration>
                            <destFile>${project.build.directory}/coverage-reports/jacoco.exec</destFile>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>

                    <!--在程序的verify阶段,执行report测试的程序。文件的输入为perpare-agent阶段中设置或者默认的jacoco.exec.参数includes和excludes可用来选定report中过滤-->
                    <execution>
                        <id>default-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
</build>

通过executions下每个execution子元素可以用来配置绑定的插件的一个执行任务。id是任务的名称id,通过phase配置绑定的生命周期阶段,goals配置绑定的指定要执行的插件目标。

2.2.2 内置绑定

id为default-prepare-agent,没有明确指明phase,执行

mvn help:describe -Dplugin=org.jacoco:jacoco-maven-plugin -Ddetail

会看到插件jacoco的prepare-agent默认绑定到了initialize阶段(Bound to phase:initialize)

jacoco:prepare-agent
  Description: Prepares a property pointing to the JaCoCo runtime agent that
    can be passed as a VM argument to the application under test. Depending on
    the project packaging type by default a property with the following name is
    ......
    Resulting coverage information is collected during execution and by default
    written to a file when the process terminates.
  Implementation: org.jacoco.maven.AgentMojo
  Language: java
  Bound to phase: initialize

2.2 配置任务

通过配置插件目标的参数,进一步调整插件目标所执行的任务。

2.2.1 pom.xml插件配置

如2.1.1的pom.xml片段,jacoco-maven-plugin插件的report目标和生命周期test阶段绑定,并且通过configuration配置他的dataFile和outputDirectory。
如果在配置时没有明确phase和goals,则表明是全局的配置,如下maven-compiler-plugin的插件目标绑定到compile和testCompile这两个阶段的配置都使用统一的配置。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <verbose>true</verbose>
            <fork>true</fork>
            <compilerVersion>1.8</compilerVersion>
            <encoding>UTF-8</encoding>
        </configuration>
</plugin>

如果如下配置表示,这个模块不需要打包。一个项目可能分client和web,client需要打成jar包上传到仓库中,而war包不需要。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

2.2.2 命令行配置

使用-D参数,并伴随一个参数键=参数值的形式,来配置插件目标的参数。
maven-surefire-plugin提供了一个maven.test.skip参数,当其值为true时,会跳过执行测试,例如 "mvn install -Dmaven.test.skip=true"

2.3 命令行执行插件目标

mvn dependency:tree列出项目的依赖树,帮助分析依赖来源。
dependency是maven-dependency-plugin插件的前缀,tree表示插件目标。
命令行格式:mvn 插件前缀:插件目标
mvn命令行中的插件前缀与groupId和artifactId是一一对应的关系,这种匹配关系存储在仓库元数据中,通过在settings.xml上配置pluginGroups来自定义仓库元数据。

<pluginGroups>
        <pluginGroup>org.apache.maven.plugins</pluginGroup>
</pluginGroups>

maven-metadata.xml

3.插件仓库

在settings.xml文件中可以加入自定义的插件仓库的配置

<pluginRepositories>
    <pluginRepository>
        <id>xxx-release</id>
        <name>xxx release repo for releases artifacts</name>
        <url>
            http://maven.xxx.com/nexus/content/groups/public
        </url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
    <pluginRepository>
        <id>xxx-snapshot</id>
        <name>
            xxx snapshot repository for snapshots artifacts
        </name>
        <url>
            http://maven.xxx.com/nexus/content/groups/public-snapshots
        </url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

相关文章

网友评论

      本文标题:Maven 生命周期

      本文链接:https://www.haomeiwen.com/subject/uymgdxtx.html