views:

1446

answers:

2

Hi,

I would like to create an execution order in my plugin which surrounds a maven plugin with a before and after execution of another maven plugin. All 3 executions are part of the deploy phase.

Here is an example of what I want to do:

  • phase:deploy
  • url:get: execution-before
  • dependency:unpack
  • url:get: execution-after

Note: url:get is my own custo mojo and just executes an http get using commons httpClient.

I would usually attach the after plugin execution in the next phase but unfortunately deploy is the last phase of the jar lifecycle.

Thank you in advance,

Kostas


Note: The following plugins segment from my pom file creates the following execution order which is not expected:

  • phase:deploy
  • url:get: execution-before
  • url:get: execution-after
  • dependency:unpack

Plugin segment:

        <plugin>
            <groupId>com.blabla.stpadmin</groupId>
            <artifactId>maven-url-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>stop-stpadmin-service</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>get</goal>
                    </goals>
                    <configuration>
  ...
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
  ...
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.blabla.stpadmin</groupId>
            <artifactId>maven-url-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <executions>
                <execution>
                    <id>start-stpadmin-service</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>get</goal>
                    </goals>
                    <configuration>
  ...
                    </configuration>
                </execution>
            </executions>
        </plugin>
+3  A: 

You can bind the execution of each plugin to the same phase and they will be executed in the order you specify. Note they will be executed after the deploy goal has run, so you might want to bind them to the previous phase (install)

Update: to ensure the execution-before and execution-after goals are executed around the dependency plugin execution, you'll need to ensure they are defined in separate plugins. Otherwise the two configurations will be merged and executed sequentially.

If the two executions need to be defined in the same plugin, you can do this by defining a custom lifecycle and invoking that lifecycle before your Mojo is executed via the execute annotation. In this answer I described how to create a custom lifecycle and force it to be invoked before a plugin is run. If you configure the execute-after goal to invoke the dependency-plugin you'll get the execution order you want (you could even invoke the execute-before goal in that lifecycle as well).

The example below will execute the three plugins in order during the deploy phase:

  <plugin>
    <groupId>custom.url.plugin</groupId>
    <artifactId>maven-url-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <phase>deploy</phase>
        <goals>
          <goal>execution-before</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.1</version>
    <executions>
      <execution>
        <phase>deploy</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>custom.url.plugin</groupId>
    <!--specify final execution in a different plugin to 
           avoid the configurations being merged-->
    <artifactId>maven-url-2-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <phase>deploy</phase>
        <goals>
          <goal>execution-after</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
Rich Seller
Hi Rich, Thanks for the reply.I 've tested this plugin structure you list but I wasn't able to get the desired execution order.I am expecting to get: * phase:deploy * url:get: execution-before * dependency:unpack * url:get: execution-afterBut I got: * phase:deploy * url:get: execution-before * url:get: execution-after * dependency:unpack
Kostas
ah yes, my mistake is that the configuration for "maven-url-plugin" will be merged and then executed sequentially. If you are able to split the executions into two discrete plugins it will work.
Rich Seller
I've updated my answer with a couple of options to address this
Rich Seller
I created a plugin with custom lyfecycle which forks url:get execution before and then dependency:unpack. Parameter passing from the top plugin to the forked one is not ideal but it works. However I wish maven had an easy way to control the execution order of goals.
Kostas
A: 

Please note that in fact execution order is NOT preserved, and this is an egregiously awful bug detailed here: http://jira.codehaus.org/browse/MNG-2258

Laird Nelson