【问题标题】:What determines the Maven plugins versions in the <build> section of pom.xml?什么决定了 pom.xml 的 <build> 部分中的 Maven 插件版本?
【发布时间】:2016-07-08 01:23:19
【问题描述】:

我将 Eclipse Kepler 与 M2E 结合使用。我创建了一个空的 Eclipse 项目,直接转换为 Maven 项目。 pom.xml 被清理:没有依赖或构建部分,没有父 POM。 Maven settings.xml 是默认的:不包含任何信息。

在使用 M2E 查阅 Effective POM 时,会出现很多插件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>testTCO</groupId>
  <artifactId>testTCO</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\src\main\java</sourceDirectory>
    <scriptSourceDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\src\test\java</testSourceDirectory>
    <outputDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\target\classes</outputDirectory>
    <testOutputDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\target</directory>
    <finalName>testTCO-0.0.1-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.0</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>C:\stuff\eclipse-jee-kepler_64\workspace\wosp\testTCO\target\site</outputDirectory>
  </reporting>
</project>

例如插件 maven-resources-plugin 的 2.5 版本已加载。为什么是这个版本而不是更新的版本?

我知道我可以通过在 pom.xml 中指定插件和所需版本来覆盖它,但我想知道这些默认版本值是如何加载的。

超级 POM 包含了有效 POM 的一部分内容,但大多数插件似乎都是凭空而来的。

【问题讨论】:

    标签: eclipse maven pom.xml m2e maven-resources-plugin


    【解决方案1】:

    这是Super POMMaven default bindings 的组合。

    Super POM 是所有 POM 隐式继承的特殊 POM。它定义了所有 Maven 项目将共享的各种默认属性。它还用于修复最常用插件的插件版本。对于 Maven 3.3.9,这个 POM 只定义了那些版本。

    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.3</version>
    </plugin>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.2-beta-5</version>
    </plugin>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.8</version>
    </plugin>
    <plugin>
      <artifactId>maven-release-plugin</artifactId>
      <version>2.3.2</version>
    </plugin>
    

    但请注意这里的评论:

    <!-- NOTE: These plugins will be removed from future versions of the super POM -->
    <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
    

    链接到 Maven 问题 MNG-4453。如今,插件版本是在给定生命周期的默认绑定中定义的,并且可能会删除当前存在于 Super POM 中的内容。

    这些绑定可以在META-INF/plexus/default-bindings.xml 文件中找到。作为参考,这是 jar 包装的样子:

    <phases>
      <process-resources>
        org.apache.maven.plugins:maven-resources-plugin:2.6:resources
      </process-resources>
      <compile>
        org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile
      </compile>
      <process-test-resources>
        org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
      </process-test-resources>
      <test-compile>
        org.apache.maven.plugins:maven-compiler-plugin:2.5.1:testCompile
      </test-compile>
      <test>
        org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
      </test>
      <package>
        org.apache.maven.plugins:maven-jar-plugin:2.4:jar
      </package>
      <install>
        org.apache.maven.plugins:maven-install-plugin:2.4:install
      </install>
      <deploy>
        org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
      </deploy>
    </phases>
    

    插件的默认版本绑定到打包的特定阶段,您的构建将从这些阶段继承。

    【讨论】:

      【解决方案2】:

      super pom defines 的某些部分(文件夹结构)但life cycle binding 定义了其他部分,例如绑定到适当的生命周期阶段。 The life cycle phases itself are defined here.

      插件的版本取决于您使用的 Maven 版本,因为 生命周期绑定 正在更新。如果您想确定正在使用哪个版本的插件,最佳做法是在您的 pom 中通过 pluginManagement 定义您正在使用的所有插件,这应该在公司 pom 文件中完成以避免重复。

      【讨论】:

        猜你喜欢
        • 2014-10-28
        • 2014-04-13
        • 1970-01-01
        • 1970-01-01
        • 2019-05-12
        • 2022-01-17
        • 2020-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多