pom.xml 执行的配置块实际上在哪里?

Where does the configuration block of an execution actually go in pom.xml?

看似很简单的问题,但有些地方不对。

所以给定一个 pom.xml 文件,我想使用 properties-maven-plugin。如果我复制什么是 in the docs,意思是这个代码:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
          <execution>
            <phase>generate-resources</phase>
            <goals>
              <goal>write-project-properties</goal>
            </goals>
            <configuration>
              <outputFile>
                   ${project.baseDir}/app.properties
              </outputFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

然后我 运行 使用 mvn properties:write-project-properties,我得到一个错误:

Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:write-project-properties (default-cli) on project backend: The parameters 'outputFile' for goal org.codehaus.mojo:properties-maven-plugin:1.0.0:write-project-properties are missing or invalid

但是

当我编辑上面的代码并将 configuration 移到 executions 旁边然后 运行 命令时,一切正常(即使 IntelliJ 强调了 configuration带有红色字样 properties 'child' tag should be defined.

的表达式
<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>write-project-properties</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outputFile>
                        ${project.baseDir}/app.properties
                    </outputFile>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

我只是偶然发现 this answer here 解决了这个问题。

为什么会发生这种情况?maven 的文档在哪里可以防止我下次需要时在配置文件上浪费几个小时?

原因如下:

  • 第一个 POM 定义了 Maven 生命周期的执行及其配置。因此,当您调用 mvn clean install 之类的内容时,它的意思是 运行。使用执行中的配置,您只需配置该执行(而不是任何其他执行,就像您从命令行启动的那样)。
  • 第二个 POM 具有插件目标的全局配置。 POM 中的执行无关紧要(你可以删除它),但你的命令行执行将获取此全局配置。

这是否使行为更容易理解?