如何多次执行maven Plugins

How to execute maven Plugins multiple times

我正在使用 Properties Maven 插件读取一些属性,在执行这个插件后我执行另一个插件,之后我想用不同的配置再次执行这个插件

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>src/Args.properties</file>
                </files>
            </configuration>
        </execution>
  </executions>
</plugin>

执行 maven-antrun-plugin 然后用配置 "src/Args2.properties" 调用这个插件,因为在上一个插件中我声明了一个新属性,它在文件 "Args2.properties

中使用
<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                             classpathref="maven.plugin.classpath"/>

                    <if>
                        <equals arg1="${serviceType}" arg2="none"/>
                        <then>
                            <property name="prop" value="x"/>
                        </then>
                        <else>
                           <property name="prop"value="y"/>
                        </else>
                    </if>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

和Args2.properties:

prop2=${prop}/anotherValue

我希望在 Antrun 插件中为 prop 赋值后,稍后从文件 Args2 中读取 prop2

在构建中再次执行 Maven 插件只需通过指定另一个 <execution> 块来完成,并且 not 通过在 POM 中指定插件两次来完成:

<plugin>
  <groupId>my.groupid</groupId>
  <artifactId>my-plugin</artifactId>
  <version>version</version>
  <executions>
    <execution>
      <id>execution-1</id>
      <phase>initialize</phase>
      <!-- goals and configuration for this execution -->
    </execution>
    <execution>
      <id>execution-2</id>
      <phase>test</phase>
      <!-- goals and configuration for this execution -->
    </execution>
  </executions>
</plugin>

例如,上面的配置将定义插件的两次执行,其中第一次绑定到 initialize 阶段,而另一个绑定到 test 阶段。当 2 个执行被绑定到同一个阶段时,它们将按照它们在 POM 中的声明顺序执行:这意味着即使 execution-2 会被绑定到 initialize 阶段,就像 execution-1,会在execution-1.

之后执行

在你的情况下,你有一些不是真正传统的读取构建中的多个属性文件,而是以某种方式在两者之间交错执行 maven-antrun-plugin 插件。这很麻烦,因为您不能在 POM 中两次声明插件,所以不可能在同一阶段在另一个插件 B 的两次执行之间插入插件 A 的执行。这是正常的,因为给定阶段的执行是按照 POM 中的声明顺序执行的,插件必须声明一次;所以要么 A 在 POM 中的 B 之前,并且它会在之前执行,这是不想要的(因为 B 的一次执行需要在 运行 之前),或者 B 在 A 之前,并且 2 次执行B 的执行将在 A 的执行之前执行,这也不是我们想要的(因为 A 的执行需要在两者之间发生)。

解决这个问题的一种方法是将执行绑定到不同的阶段:例如确保我们想要 运行 首先执行的 B 被绑定到某个阶段,并且我们想要 运行 之后的 A 的执行被绑定到构建中稍后发生的阶段,最后,B 的最后一次执行也绑定到后面的阶段。但这不是一个干净的解决方案,因为您最终会滥用某些阶段来做此时不应该做的事情。

一个更好的解决方案是接受这样一个事实,即我们真正想在单步执行中完成的事情:即加载一个属性,做一些事情,加载另一个属性,在同一个执行中。它们是如此紧密地联系在一起,因此将它们整合在一起也很有意义。这并不总是很容易完成——通常,您需要创建自己的 Maven 插件来执行此操作,以便所有这些任务都由它封装。

但是,在这种特定情况下,您可以重用 maven-antrun-plugin 并在执行此插件的过程中执行所有操作。 Ant 有一个任务 loadproperties 可用于加载属性文件,在设置 prop 属性.

之前和之后
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.8</version>
  <executions>
    <execution>
      <id>load-properties</id>
      <phase>initialize</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <target>
          <loadproperties srcFile="${project.basedir}/src/Args.properties" />
          <taskdef resource="net/sf/antcontrib/antcontrib.properties"
            classpathref="maven.plugin.classpath" />
          <if>
            <equals arg1="${serviceType}" arg2="none" />
            <then>
              <property name="prop" value="x" />
            </then>
            <else>
              <property name="prop" value="y" />
            </else>
          </if>
          <loadproperties srcFile="${project.basedir}/src/Args2.properties" />
        </target>
        <exportAntProperties>true</exportAntProperties>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>ant-contrib</groupId>
      <artifactId>ant-contrib</artifactId>
      <version>1.0b3</version>
      <exclusions>
        <exclusion>
          <groupId>ant</groupId>
          <artifactId>ant</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</plugin>

上面声明的几个注意事项:插件已更新到 1.8 的最新版本并且需要对 ant-contrib 的依赖才能解析 antcontrib.properties 文件。 ant 需要从对 ant-contrib 的依赖中排除,因为 AntRun 插件使用 Ant 1.9,但该依赖会使插件继承旧版本。另请注意 <exportAntProperties> which will enable your build to use the properties created in the Ant task, and the use of <target> instead of the deprecated <tasks>.