用于调用Maven插件的Maven插件?

时间:2015-01-07 15:58:00

标签: maven maven-3 maven-plugin

有没有人知道任何可以通用方式调用其他maven插件的maven插件?我正在寻找的是类似于按特定顺序安排插件执行的代理机制。

关键是我必须将一个巨大的遗留项目迁移到拥有大量必须以特定顺序运行的蚂蚁宏的maven,但是使用maven,不可能在同一个阶段中两次调用相同的插件当我需要通过第二个插件的exectuion拦截两个执行时保留执行顺序。

假设如下:我有插件A(native2ascii)和插件B(替换器)。现在我的执行顺序必须只是一个阶段的A,B,A。当然,我可以这样写,但有效的pom看起来像A,A,B。

所以有一个'proxy-plugin'(P)可以简单地调用配置好的插件。这样,您可以在一个阶段配置P(A),P(B),P(A),并且有效的pom将能够保留执行顺序。

到目前为止感谢!


我已经尝试过Wim的提议,但事实证明,执行ID对有效的pom没有影响。替换器插件的两个执行仍然合并并在native2ascii插件之前运行。

这是我的pom:

<build>
  <plugins>
    <plugin>
      <groupId>com.google.code.maven-replacer-plugin</groupId>
      <artifactId>replacer</artifactId>
      <version>1.5.3</version>
      <executions>
        <execution>
          <id>step-1-replacer</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>replace</goal>
          </goals>
          <configuration>
            <file>src/main/order-test/test.txt</file>
            <outputFile>target/test-replaced-1.txt</outputFile>
            <replacements>
              <replacement>
                <token>t</token>
                <value>xyz</value>
              </replacement>
            </replacements>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>native2ascii-maven-plugin</artifactId>
      <version>1.0-beta-1</version>
      <executions>
        <execution>
          <id>step-2-native2ascii</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>native2ascii</goal>
          </goals>
          <configuration>
            <workDir>target</workDir>
            <includes>
              <include>test-replaced-1.txt</include>
            </includes>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>com.google.code.maven-replacer-plugin</groupId>
      <artifactId>replacer</artifactId>
      <version>1.5.3</version>
      <executions>
        <execution>
          <id>step-3-replacer</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>replace</goal>
          </goals>
          <configuration>
            <file>target/test-replaced-1.txt</file>
            <outputFile>target/test-replaced-2.txt</outputFile>
            <replacements>
              <replacement>
                <token>f6</token>
                <value>-</value>
              </replacement>
              <replacement>
                <token>e4</token>
                <value>></value>
              </replacement>
            </replacements>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

以下是有效pom中的样子(两个替换器执行都在native2ascii执行之前运行):

<plugin>
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>replacer</artifactId>
  <version>1.5.3</version>
  <executions>
    <execution>
      <id>step-1-replacer</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>replace</goal>
      </goals>
      <configuration>
        <file>src/main/order-test/test.txt</file>
        <outputFile>target/test-replaced-1.txt</outputFile>
        <replacements>
          <replacement>
            <token>t</token>
            <value>xyz</value>
          </replacement>
        </replacements>
      </configuration>
    </execution>
    <execution>
      <id>step-3-replacer</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>replace</goal>
      </goals>
      <configuration>
        <file>target/test-replaced-1.txt</file>
        <outputFile>target/test-replaced-2.txt</outputFile>
        <replacements>
          <replacement>
            <token>f6</token>
            <value>-</value>
          </replacement>
          <replacement>
            <token>e4</token>
            <value>></value>
          </replacement>
        </replacements>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>native2ascii-maven-plugin</artifactId>
  <version>1.0-beta-1</version>
  <executions>
    <execution>
      <id>step-2-native2ascii</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>native2ascii</goal>
      </goals>
      <configuration>
        <workDir>target</workDir>
        <includes>
          <include>test-replaced-1.txt</include>
        </includes>
      </configuration>
    </execution>
  </executions>
</plugin>

以下是构建的输出:

...
[INFO] --- replacer:1.5.3:replace (step-1-replacer) @ build-order-test ---
[INFO] Replacement run on 1 file.
[INFO] 
[INFO] --- replacer:1.5.3:replace (step-3-replacer) @ build-order-test ---
[INFO] Replacement run on 1 file.
[INFO] 
[INFO] --- native2ascii-maven-plugin:1.0-beta-1:native2ascii (step-2-native2ascii) @ build-order-test ---
[INFO] Includes: [test-replaced-1.txt]
[INFO] Excludes: []
[INFO] Processing /home/shillner/work/workspaces/dev/build-order-test/target/test-replaced-1.txt
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:2)

查看mojo-executor插件,它可用于调用其他插件 - 来自pom.xml以及programmaticaly。

Example

        <plugin>
            <groupId>org.twdata.maven</groupId>
            <artifactId>mojo-executor-maven-plugin</artifactId>
            <version>@project.version@</version>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>execute-mojo</goal>
                    </goals>
                    <configuration>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-dependency-plugin</artifactId>
                            <version>2.0</version>
                        </plugin>
                        <goal>list</goal>
                        <configuration>
                        </configuration>
                    </configuration>
                </execution>
            </executions>
        </plugin>

答案 1 :(得分:-1)

没有官方支持,但我们按字母顺序使用执行ID,这已经好多年了。

示例:

<plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>step-0-copy-resources</id>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <outputDirectory>${basedir}/target</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>step-1-build-assembly</id>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <descriptors>
                                <descriptor>${basedir}/src/main/assembly/descriptor.xml</descriptor>
                            </descriptors>
                            <!-- don't attach assembly to be installed -->
                            <attach>false</attach>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>step-2-build-installer</id>
                        <phase>package</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>${basedir}/BuildInstaller.groovy</source>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>step-3-attach-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>${basedir}/target/project-${project.version}.tar.gz</file>
                                    <type>tar.gz</type>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

注意每个<id/>如何以“step-x”开头,x为数字。