简体   繁体   中英

Maven Plugin for calling Maven Plugins?

does anyone know any maven plugin that can call other maven plugins in a generic way? What I am searching for is anything like a proxy mechanism for scheduling plugin executions in a specific order.

The key is that I have to migrate a huge legacy project to maven which has plenty of ant macros that have to run in a specific order but with maven it is not possible to invoke the same plugin twice in one and the same phase with preserving the execution order when I need to intercept the two executions by the exectuion of a second plugin.

Assume the following: I have plugin A (native2ascii) and plugin B (replacer). Now my execution order must be A,B,A in just one phase. Sure, I can write it like that but the effective pom would look like A,A,B.

So it would be nice to have a 'proxy-plugin' (P) which would simply invoke the configured plugins. This way you could configure P(A),P(B),P(A) in one phase and the effective pom would be able to preserve the execution order.

Thanks so far!


I've tried out Wim's proposal like in the following but it turned out that the execution ids have no impact on the effective pom. The two executions of the replacer plugin are still combined and run before the native2ascii plugin.

Here is my 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>

And here is how it looks like in the effective pom (both replacer executions are run before the native2ascii execution):

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

And here is the output of the build:

...
[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] ------------------------------------------------------------------------

Take a look at mojo-executor plugin, it can be used to invoke other plugins - from pom.xml and also 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>

There is no official support, but we are using execution ids in alphabetical order and this works fine for many years already.

Example:

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

Notice how each <id/> starts with "step-x" with xa number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM