pluginManagement:管理Maven插件

杰米粉2021-10-09 10:36

    Maven使用dependencyManagement对依赖进行管理,与之类似地,Maven中还提供了一个名为pluginManagement的元素,它可以帮助用户管理Maven插件。

    插件管理

    pluginManagement元素与dependencyManagement元素的原理十分相似,在pluginManagement元素中可以声明插件及插件配置,但不会发生实际的插件调用行为,只有在POM中配置了真正的plugin元素,且其groupId和artifactId与pluginManagement元素中配置的插件匹配时,pluginManagement元素的配置才会影响到实际的插件行为。

    例如,在App-Data-lib模块中使用pluginManagement元素管理maven-source-plugin插件,并将其插件目标jar-no-fork绑定到default生命周期的verify阶段上,具体配置如下。

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>App-Data-lib</artifactId>
    <parent>
        <groupId>net.biancheng.www</groupId>
        <artifactId>Root</artifactId>
        <version>1.0</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
    <!--添加插件管理-->
    <build>
        <pluginManagement>
            <plugins>
                <!--声明插件-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <!--将 jar-no-fork 目标绑定到 verify 阶段-->
                        <execution>
                            <id>www.biancheng.net</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>
                                    jar-no-fork
                                </goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

    根据以上配置可以看出,使用pluginManagement管理插件很简单,只需要将声明插件的配置添加在pluginManagement元素中即可。

    由于该插件目标是绑定在verify阶段上,因此我们只需要执行verify或以后的阶段(如install)即可调用maven-source-plugin:jar-no-fork目标。

    打开命令行窗口,跳转到App-Data-lib的目录下,执行以下命令,尝试调用maven-source-plugin:jar-no-fork目标。

mvn clean install

    执行结果如下。

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< net.biancheng.www:App-Data-lib >-------------------
[INFO] Building App-Data-lib 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ App-Data-lib ---
[INFO] Deleting D:\eclipse workSpace4\App-Data-lib\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ App-Data-lib ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\eclipse workSpace4\App-Data-lib\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ App-Data-lib ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\eclipse workSpace4\App-Data-lib\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ App-Data-lib ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\eclipse workSpace4\App-Data-lib\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ App-Data-lib ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\eclipse workSpace4\App-Data-lib\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ App-Data-lib ---
[INFO] Surefire report directory: D:\eclipse workSpace4\App-Data-lib\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running net.biancheng.www.App_Data_lib.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ App-Data-lib ---
[INFO] Building jar: D:\eclipse workSpace4\App-Data-lib\target\App-Data-lib-1.0.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ App-Data-lib ---
[INFO] Installing D:\eclipse workSpace4\App-Data-lib\target\App-Data-lib-1.0.jar to D:\myRepository\repository\net\bianc
heng\www\App-Data-lib\1.0\App-Data-lib-1.0.jar
[INFO] Installing D:\eclipse workSpace4\App-Data-lib\pom.xml to D:\myRepository\repository\net\biancheng\www\App-Data-li
b\1.0\App-Data-lib-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.469 s
[INFO] Finished at: 2021-04-16T09:23:26+08:00
[INFO] ------------------------------------------------------------------------

    通过以上执行结果,我们看到maven-source-plugin:jar-no-fork目标并没有被调用。

    在App-Data-lib的POM文件中添加plugin元素声明需要调用的插件。

<project>
    ...
    <!--添加插件管理-->
    <build>
        <pluginManagement>
          ...
        </pluginManagement>
                <!-- 声明使用 maven-source-plugin 插件 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

    与依赖管理相似,由于pluginManagement元素中已经包含了插件的全部信息,所以当使用plugins元素声明插件调用时,只需要声明插件的groupId和artifactId即可。

    再次执行命令mvncleaninstall,maven-source-plugin:jar-no-fork目标被成功调用,如图所示。

pluginManagement:管理Maven插件

    继承插件依赖

    当项目中的多个模块存在相同的插件时,应当将插件配置移动到父模块的pluginManagement元素中。即使各个模块对于同一插件的具体配置不尽相同,也应当在父模块中使用pluginManagement元素对插件版本进行统一声明。

    我们甚至可以将项目中所有插件的版本信息都在父模块的POM中声明,子模块中不再配置任何的版本信息,这样不仅可以统一项目的插件版本,还可以避免出现版本冲突或插件不稳定等问题。

    例如,我们将App-Data-lib中pluginManagement元素中的配置内容移动到父模块Root的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.biancheng.www</groupId>
    <artifactId>Root</artifactId>
    <version>1.0</version>
    <!--定义的父类pom.xml 打包类型使pom -->
    <packaging>pom</packaging>
    <properties>
        <!-- 定义一些属性 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <log4j.version>1.2.17</log4j.version>
        <junit.version>4.9</junit.version>
        <system.version>1.0</system.version>
        <mysql.connector.version>5.1.18</mysql.connector.version>
        <c3p0.version>0.9.1</c3p0.version>
    </properties>
    <!--dependencyManagement 标签用于控制子模块的依赖版本等信息 -->
    <!-- 该标签只用来控制版本,不能将依赖引入 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <!--引用的properties标签中定义的属性 -->
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <!--引用的properties标签中定义的属性 -->
                <version>${junit.version}</version>
                <!-- <scope>test</scope> -->
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <!--引用的properties标签中定义的属性 -->
                <version>${mysql.connector.version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <!--引用的properties标签中定义的属性 -->
                <version>${c3p0.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!--插件依赖-->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <id>www.biancheng.net parent</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>
                                    jar-no-fork
                                </goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

    在App-Data-lib的POM中使用parent元素继承Root,并声明使用maven-source-plugin插件,配置如下。

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>App-Data-lib</artifactId>
    <parent>
        <groupId>net.biancheng.www</groupId>
        <artifactId>Root</artifactId>
        <version>1.0</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 声明使用 maven-source-plugin 插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

    打开命令行窗口,跳转到App-Data-lib的目录下,执行命令

mvn clean install

    执行结果如下图。