Maven - determine order of different plugin goals in the same phase - maven

The following snippet is an excerpt of the configuration of the maven-cargo plugin, but the question is independent from that specific plugin.
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy</goal>
<goal>start</goal>
</goals>
</execution>
</executions>
This configuration (lets simply call it plugin A) will wait till pre-integration-test phase, then fire its goals deploy and start (in that order).
Say I have another plugin B which is relevant in the same phase. What are my options to
execute plugin B's goals before (after) A? (someStuff - > deploy -> start)
execute plugin B's goals in-between plugin A's goals (deploy -> someStuff -> start)
I figure that the answer to (1) is here, linking the order of the goals to the order of the plugin definition in the POM. But I have no idea about (2).

You are right about (1). If two plugins are to be executed on the same phase, then they will be executed in the order they are declared in pom.xml.
I'm not 100% sure about the (2), but I think it's impossible without some hacks, like using exec-maven-plugin, for example:
<!-- deploy -->
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- do something -->
<plugin>
<groupId>some_other_plugin</groupId>
<artifactId>some_other_plugin</artifactId>
<executions>
<execution>
<id>someStuff</id>
<phase>pre-integration-test</phase>
<goals>
<goal>some_goal</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- start -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<commandlineArgs>org.codehaus.cargo:cargo-maven2-plugin:start -Dparam=value</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>

Related

Maven plugin crossed execution order

I'm writing some integration tests using failsafe plugin.
I want to execute the following:
1) Start Docker (goal start in phase pre-integration-test)
2) Start Spring (goal start in phase pre-integration-test)
3) Tests (phase integration-test)
4) Stop Spring (goal stop in post-integration-test)
5) Stop Docker (goal stop in post-integration-test)
Why? I want to start Docker BEFORE Spring so all databases are ready when Spring boots and also I want to stop Docker AFTER Spring to avoid a lot of errors in Spring due to database connection losses.
I have the following pom.xml:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.20.1</version>
<executions>
<execution>
<id>run-docker-containers</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
...
</configuration>
</execution>
<execution>
<id>stop-docker-containers</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
...
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
But with this pom.xml I'm getting this order:
1) Start Docker
2) Start Spring
3) Tests
4) Stop Docker
5) Stop Spring
This is, Docker is stopped BEFORE Spring and I don't want that.
I know that the execution order in Maven is given by the order in pom.xml, but in this case I need to have crossed goals.
Any advice?
Thanks.
Execution order in Maven is based on two components:
First, the lifecycle phase to which the execution is bound
For executions bound to the same phase, the ordering in the POM takes precedence.
Maven doesn't allow the same plugin to be configured twice, so it's not possible to move the Docker Stop execution below Spring. The only way I know to fix things like this is to introduce a third phase into the mix. It looks a bit weird to bind 'stop' goals to the verify phase, and I'd definitely add comments to explain why this was done for posterity - but it works.
This plugin config does the following (assuming you use the maven-failsafe-plugin for integration tests; if you use a different plugin follow the same principles with that plugin). Maven lifecycle phase in parentheses:
1) Docker Start (pre-integration-test)
2) Spring Start (pre-integration-test; Spring plugin config after Docker)
3) Execute tests (integration-test)
4) Spring Stop (post-integration-test)
5) Docker Stop (verify)
6) Verify test results (verify; Failsafe plugin config after Docker)
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.20.1</version>
<executions>
<execution>
<id>run-docker-containers</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-docker-containers</id>
<phase>verify</phase> <!-- important: note phase -->
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.maven.failsafe.plugin}</version>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>failsafe-verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

Maven Config Plugins running twice

I have the following pom config. I added the cobertura plugin, and now pmd, cpd, findbugs, and test are running twice.
I understand that is because of my "phases" config, but I don't understand how can I achieve the following:
What I want is before I commit to the repo, build my app and check for pmd errors, findbugs errors, check my tests, and check my cobertura.
How can I achieve this? I am used to run "mvn clean package" before commit. It's that ok?
Here's my config:
...
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<configuration>
<linkXref>false</linkXref>
<rulesets>
<!-- Custom Ruleset -->
<ruleset>codequality/pmd.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>pmd</goal>
<goal>cpd</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<check>
<haltOnFailure>true</haltOnFailure>
<branchRate>70</branchRate>
<lineRate>70</lineRate>
<totalBranchRate>70</totalBranchRate>
<totalLineRate>70</totalLineRate>
<packageLineRate>70</packageLineRate>
<packageBranchRate>70</packageBranchRate>
</check>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any maven plugin has a default execution phase. In this case, the plugins that you apply are executed in the verify phase (pmd-plugin). But you are defining it to run also in the compile phase. Removes the phase tag and lets it run in the verification phase.
<execution>
<!--<phase>compile</phase>-->
<goals>
<goal>...</goal>
...
</goals>
</execution>
Finally, the good practice to validate your project before a commit is to run:
mvn clean verify

why list the goals of a plugin without binding to a phase?

Please consider this pom excerpt taken from jacoco example ( http://www.eclemma.org/jacoco/trunk/doc/examples/build/pom-it.xml)
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5-SNAPSHOT</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<!-- implmentation is needed only for Maven 2 -->
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- implmentation is needed only for Maven 2 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>default-integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Now I know that you can bind a plugin's goal to a maven phase, that is run that goal when maven executes a specific phase.
What is the point of just listing the integration-test goal for the maven failsafe plugin without binding it to something?
The same as for jacoco report and others goal? I don't think you can force the plugin to execute just those listed goals right?
Many thanks
The point is that a plugin can define default life cycle phases where the appropriate goal is bound to (many plugins do this). In this cases you don't need to specify the life cycle phase within the pom file explicitly.
For example the maven-failsafe-plugin has a goal integration-test. This goal has a default binding to the integration-test life cycle phase. Here an excerpt from the documentation:
Description:
Run integration tests using Surefire. Attributes:
Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope: test.
The goal is thread-safe and supports parallel builds.
Binds by default to the lifecycle phase: integration-test.
That's the reason why you don't need to give a life cylce phase in the configuration like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<id>default-integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
The same is meant for the jacoco maven plugin.
If I understand correctly, M2E will execute the plugin goal during workspace full or incremental builds.
The following article may shed some light:
http://eclipse.org/m2e/documentation/m2e-execution-not-covered.html
You are correct that, on command line, you would not be able to specify the goal, since it is a plugin goal and not tied to a phase. This could be an oddball usage specifically for M2E integration.

How to have exec-maven-plugin:exec run before 'site' phase?

I have this script I need to run before site phase to fix an issue (http://jira.codehaus.org/browse/MSITE-640).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3</version>
<inherited>false</inherited>
<executions>
<execution>
<id>workaround-MSITE-640</id>
<!--it should be 'site' but maven doesn't pick up there-->
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<failWithEmptyArgument>false</failWithEmptyArgument>
<workingDirectory>${project.basedir}</workingDirectory>
<executable>./workaround-MSITE-640.sh</executable>
<arguments>
<argument>${settings.localRepository}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
But I can't seem to run it on that phase. I tried to put it under reporting -> plugins but it can't have configuration there.
It is idempotent, so no problem in running it more than once, but would be nice if I could tie it to the correct phase.
Thanks

How do include JUnit test cases as a part of Maven build?

I have existing JUnit4 test cases which I run from Eclipse and as a part of Maven. Now I am looking to perform stress tests by leveraging them. I noticed Maven is not packaging them as a part of the build. How do I go about it?
This will attach the tests (and their sources) in a separate jar:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
<execution>
<id>attach-test-sources</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
Afterwards you can use them as a dependency by using the type test-jar.

Resources