簡體   English   中英

如何多次執行Maven插件

[英]How to execute maven Plugins multiple times

我正在使用Properties Maven插件讀取一些Properties,執行此插件后,我將執行另一個插件,此后,我想使用其他配置再次執行此插件

<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

僅通過指定另一個<execution>塊即可完成構建中再次執行Maven插件的操作,而不是通過在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 :這意味着,即使execution-2將綁定到initialize階段,就像execution-1 ,它將在執行后execution-1

在您的情況下,您在讀取構建中的多個屬性文件方面並不是真正的常規,而是在兩者之間以某種方式交錯執行了maven-antrun-plugin插件。 這很麻煩,因為,由於您無法在POM中兩次聲明該插件,因此不可能在同一階段在另一個插件B的兩次執行之間插入插件A的執行。 這是正常的,因為在給定階段的執行是按照POM中的聲明順序執行的,並且插件必須聲明一次。 因此,要么A在POM中位於B之前,然后在不需要的位置執行(因為必須先執行B的執行),或者B在A之前,並且B的兩次執行將是在A之一之前執行,而A也不想要(因為A的執行需要在兩者之間進行)。

解決此問題的一種方法是將執行綁定到不同的階段:例如,確保我們要先運行的B的執行綁定到某個階段,並且確保我們要運行的A的執行綁定到某個階段。這在構建的后期發生,最后B的最后執行也綁定到了以后的階段。 不過,這不是一個干凈的解決方案,因為您最終會濫用某些階段來執行此時不應執行的操作。

更好的解決方案是接受這樣一個事實,即我們真正想要在一次執行中一步完成的事情:即在同一執行中加載屬性,執行操作,加載另一個屬性。 它們是如此緊密地結合在一起,因此也有必要將它們集成在一起。 這並非總是容易做到的-通常,您需要創建自己的Maven插件才能執行此操作,以便所有這些任務都由它封裝。

但是,在這種情況下,您可以重復使用maven-antrun-plugin並在此插件的執行中執行所有操作。 在設置prop屬性之前和之后,Ant具有任務loadproperties可用於加載屬性文件。

<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>

上面的聲明有antcontrib.properties注意事項:插件已更新到1.8的最新版本,並且需要依賴ant-contrib來解析antcontrib.properties文件。 ant需要從ant-contrib的依賴項中排除,因為AntRun插件使用Ant 1.9,但是該依賴項會使插件繼承較舊的版本。 還要注意<exportAntProperties> ,它將使您的構建可以使用在Ant任務中創建的屬性,並使用<target>而不是已棄用的<tasks>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM