Maven: validation phase invoked 3 times - maven

I have written a maven plugin to create documentation from latex.
Now I added some verification and to that end
added another execution, the one with id validate-converters like so:
<executions>
<execution>
<id>process-latex-sources</id>
<goals>
<goal>cfg</goal>
</goals>
</execution>
<execution>
<id>clear-latex-sources</id>
<goals>
<goal>clr</goal>
</goals>
</execution>
<execution>
<id>validate-converters</id>
<goals>
<goal>vrs</goal>
</goals>
<configuration>
<versionsWarnOnly>true</versionsWarnOnly>
</configuration>
</execution>
</executions>
Now the strange thing is, that for mvn site verification is run 3(!!!) times and I cannot figure out why.
I am willing to give you all code or anything to find out...

Related

Google formating plugin -Spotless

i have added a plug in like
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<java>
<googleJavaFormat>
<version>1.7</version>
<style>GOOGLE</style>
</googleJavaFormat>
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
i want me code to be reformated automatically using this plug in. i have old project where i am trying to use this
but it just list down the violation it does not auto reformat it.
After builing it just faild the build wd violation
i tried updating goals apply
<goals>
<goal>apply</goal>
</goals>
but that is also not working
Try using goal apply within process-classes phase:
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.17.4</version>
<configuration>
<java>
<googleJavaFormat>
<version>1.7</version>
<style>GOOGLE</style>
</googleJavaFormat>
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>apply</id>
<phase>process-classes</phase>
<goals>
<goal>apply</goal>
</goals>
</execution>
</executions>
</plugin>

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>

Command line option to disable an execution in maven?

The Spark pom.xml has the following set of executions: in particular I am interested in the "attach-scaladocs" one at the bottom of this snippet:
<execution>
<id>attach-scaladocs</id>
<phase>verify</phase>
<goals>
<goal>doc-jar</goal>
</goals>
</execution>
Here is the larger environment in which the snippet lives:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile-first</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
<execution>
<id>attach-scaladocs</id>
<phase>verify</phase>
<goals>
<goal>doc-jar</goal>
</goals>
</execution>
</executions>
That particular execution is time consuming and unnecessary for my needs. However, it is not in my best interest to modify the pom.xml (by commenting it out - which works fine BTW) due to git merge issues.
Any way to disable that execution via a maven command line switch?

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.

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

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>

Resources