第六章 Maven-profile及插件

一、profile概述

profile让你能够在特定场景下使用与基本配置不同的配置构建项目。你不需要创建多个单独的POM文件,只需在单个POM文件中包含不同的profile配置,这些profile配置在特定场景下将覆盖pom中的基本配置。

例如,项目中需要构建开发版本、测试版本以及正式版本,这些版本可以通过在pom文件中添加不同构建profile构建。执行maven时指定不同的构建profile就可以。

二、profile示例

<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>

  <groupId>com.qikegu.demo</groupId>
  <artifactId>mybatis-demo</artifactId>
  <version>1.0.0</version>

  <profiles>
      <profile>
          <id>test</id>
          <activation>...</activation>
          <build>...</build>
          <modules>...</modules>
          <repositories>...</repositories>
          <pluginRepositories>...</pluginRepositories>
          <dependencies>...</dependencies>
          <reporting>...</reporting>
          <dependencyManagement>...</dependencyManagement>
          <distributionManagement>...</distributionManagement>
      </profile>
  </profiles>

</project>


profile中的元素将覆盖POM中同名元素的值。
<activation>设置触发profile生效的条件
也可在maven命令行中指定profile:-P profile-name

更多细节参考官方文档:http://maven.apache.org/pom.html#Profiles

三、Maven插件概述

Maven实际上是一个插件执行框架,Maven中的所有任务都是由插件完成的。

Maven插件是构建目标的集合,也称为MOJO (Maven Old Java Object)。可以把插件理解为一个类,而构建目标是类中的方法。构建阶段包含一系列的构建目标,可以理解为按顺序调用各个插件中的构建目标(方法),然后一系列的构建阶段组成一个构建生命周期。

构建目标可以绑定到多个构建阶段,也可以不绑定,就像类的方法可以被调用,也可以不被调用。

四、Maven插件示例

<build>
    <plugins>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven.failsafe.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
这是一个故障安全插件的简单配置,负责运行集成测试。可以看到,插件有两个主要目标:

# 1.integration-test: 运行集成测试
# 2.verify: 验证所有通过的集成测试

五、列出特点插件所有目标

# 1.我们可以使用下面的命令列出一个特定插件的所有目标:
mvn <PLUGIN>:help

# 2.例如,要列出故障安全插件中的所有目标:
mvn failsafe:help

# 3.查看输出:
...
Maven Failsafe MOJO in maven-failsafe-plugin.

This plugin has 3 goals:

failsafe:help
  Display help information on maven-failsafe-plugin.
  Call mvn failsafe:help -Ddetail=true -Dgoal=<goal-name> to display parameter
  details.

failsafe:integration-test
  Run integration tests using Surefire.

failsafe:verify
  Verify integration tests ran using Surefire.
...

可以看到插件包含3个目标。

# 4.要执行一个特定的构建目标,不执行它的整个阶段(和前面的阶段),可以使用以下命令:
mvn [plugin-name]:[goal-name]

# 5.例如,可以使用maven-compiler-plugin的编译目标来编译Java项目,运行以下命令:
mvn compiler:compile

六、插件类型

Maven提供了2种类型的插件:

# 1.构建类插件 – 在构建过程中执行,pom中<build/>元素中配置。
# 2.报告类插件 – 在文档生成过程中执行,pom中<reporting/>元素中配置。

下面是一些maven内置的常用插件:
clean
compiler
surefire
jar
war
javadoc
antrun

七、自定义插件

如果maven内置插件不能满足项目需求,也可以开发自定义插件。

开发自定义插件可参考官网:https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
posted @ 2021-08-12 18:04  年少纵马且长歌  阅读(466)  评论(0编辑  收藏  举报