【问题标题】:Maven ignoring plugin version from pluginManagement in profile of submoduleMaven 忽略子模块配置文件中 pluginManagement 的插件版本
【发布时间】:2017-01-24 21:34:07
【问题描述】:

我在父 POM 的 <pluginManagement> 部分中定义插件版本,并希望在子模块的 <plugins> 部分中使用它们。

这是有效的,除非插件在子模块的配置文件中使用。在这种情况下,来自父 POM 的 <pluginManagement> 部分的版本将被忽略。

mvn -v 的输出:

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_102, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre
Default locale: de_DE, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"

./pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <prerequisites>
        <maven>3.1.0</maven>
    </prerequisites>

    <modules>
        <module>project1</module>
    </modules>

    <groupId>org.example.test</groupId>
    <artifactId>test-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>1.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

./project1/pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <prerequisites>
        <maven>3.1.0</maven>
    </prerequisites>

    <parent>
        <groupId>org.example.test</groupId>
        <artifactId>test-parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>project1</artifactId>

    <profiles>
        <profile>
            <id>p1</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.eirslett</groupId>
                        <artifactId>frontend-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

mvn versions:display-plugin-updates 的输出:

$ mvn versions:display-plugin-updates
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] test-parent
[INFO] project1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-parent 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:display-plugin-updates (default-cli) @ test-parent ---
[INFO]
[INFO] All plugins with a version specified are using the latest versions.
[INFO]
[INFO] Project defines minimum Maven version as: 3.1.0
[INFO] Plugins require minimum Maven version of: 3.1.0
[INFO] Note: the super-pom from Maven 3.3.9 defines some of the plugin
[INFO]       versions and may be influencing the plugins required minimum Maven
[INFO]       version.
[INFO]
[INFO] No plugins require a newer version of Maven than specified by the pom.
[INFO]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project1 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:display-plugin-updates (default-cli) @ project1 ---
[INFO]
[INFO] All plugins with a version specified are using the latest versions.
[INFO]
[WARNING] The following plugins do not have their version specified:
[WARNING]   com.github.eirslett:frontend-maven-plugin ................. 0.0.26
[INFO]
[INFO] Project defines minimum Maven version as: 3.1.0
[INFO] Plugins require minimum Maven version of: 3.1.0
[INFO] Note: the super-pom from Maven 3.3.9 defines some of the plugin
[INFO]       versions and may be influencing the plugins required minimum Maven
[INFO]       version.
[INFO]
[INFO] No plugins require a newer version of Maven than specified by the pom.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] test-parent ........................................ SUCCESS [  0.851 s]
[INFO] project1 ........................................... SUCCESS [  0.314 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.649 s
[INFO] Finished at: 2016-09-16T16:03:04+02:00
[INFO] Final Memory: 13M/247M
[INFO] ------------------------------------------------------------------------

我可以在子模块内复制父 POM 的 &lt;pluginManagement&gt; 部分的信息以使其工作,但出于显而易见的原因,我想避免这种情况。

【问题讨论】:

    标签: maven maven-3 pom.xml versions-maven-plugin


    【解决方案1】:

    Maven没有忽略它,你可以通过执行以下来检查它:

    mvn -pl project1 help:effective-pom -Doutput=noProfilePom.xml
    

    effective-pom 目标将:

    将此构建的有效 POM 显示为 XML,并考虑活动配置文件。

    检查生成的noProfilePom.xml,您将看到在构建project1 模块的pom.xml 时,Maven 将有效运行什么。

    我们可以看到:

    <pluginManagement>
      <plugins>
        ...
        <plugin>
          <groupId>com.github.eirslett</groupId>
          <artifactId>frontend-maven-plugin</artifactId>
          <version>1.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      ...  
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
        ...
    </plugins>
    

    因此,pluginManagement 已正确合并(取自父级),而 plugins 部分未提供它。

    但运行以下:

    mvn -pl project1 -Pp1 help:effective-pom -Doutput=withProfilePom.xml
    

    注意:作为目标执行的一部分,我们还通过-Pp1 激活配置文件。

    作为生成的withProfilePom.xml 的一部分将有:

    <pluginManagement>
      <plugins>
        ...
        <plugin>
          <groupId>com.github.eirslett</groupId>
          <artifactId>frontend-maven-plugin</artifactId>
          <version>1.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <version>1.0</version>
      </plugin>
    
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
        ...
    </plugins>
    

    这次p1 配置文件处于活动状态,并且它已正确注入到其插件声明的plugins 部分,然后从父级的pluginManagement 获取其版本。

    因此:pluginManagement 部分不会被配置文件中声明的plugins 忽略。

    【讨论】:

    • 那么,为什么 OP 在使用配置文件 p1 运行子项目时收到警告“以下插件未指定其版本”?
    • @LittleSanti 实际上可能是 versions-maven-plugin 的问题,而不是 Maven 问题。
    • 看起来你是对的,A_Di_Matteo。非常感谢!
    猜你喜欢
    • 2016-03-02
    • 2013-07-14
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 2018-07-25
    • 2021-06-19
    相关资源
    最近更新 更多