Is there a way to simplify this maven command for sonar? - maven

I'm running the following sonar command with maven:
mvn clean compile sonar:sonar
I'd like to just run:
mvn sonar:sonar
(ie have the sonar task trigger the clean and compile steps)
Is there a way to express this as a dependency in maven?

Quick Answer: No - not by declaring dependencies nor by having the plugin be in charge of the lifecycles and their phases to be run prior to the plugin's goal.
By mvn clean compile sonar:sonar you are instructing Maven to first run the entire clean-lifecycle with all its phases - then continoue with the default lifecycle to the phase compile and finally to call the goal sonar of the plugin sonar - i personally think the command is relativly short for what is going on and must not be simplified any further (what would be the actual benefit of a shorter command executing the same phases/ goals in the background?).
You can however bind plugins to certain build phases so that if the phase is run (say the phase process-classes right after phase compile in the default lifecycle) the plugin's goal will be executed automaticially.
This would abstract the need of explicitly calling sonar:sonar and allow for example to just call mvn clean process-classes and have sonar:sonar beeing executed in the process-classes phase of the default lifecycle.
Now to come back to your question one could generally ask if a Maven plugin can take controll of the lifecycles and phases beeing executed prior to its own goal which is as faar as i know not possible with "standard Maven techniques" (I however dont know of the possibilities if you write your own plugin).

Related

How to completely skip unit tests and integration tests during maven release prepare and perform with maven release plugin

I have set up some automation to perform maven semantic versioining using maven release plugin. As such i am using the below :-
mvn -B clean release:clean -DpreparationGoals=clean release:prepare -Darguments="-DskipTests" -Darguments="-DskipITs" -Darguments="-Dmaven.javadoc.skip=true" release:perform -Dusername=$GIT_USERNAME -Dpassword=$GIT_PASSWORD
Everything works great except for the fact that during perform phase, i still see the TESTS being executed in our jenkins logs.
I am wondering what would be the best way to skip any kind of tests during this maven release prepare and perform without having to make any changes in existing pom.xml as i would like to enforce that through maven arguments passed as CLI arguments so that we don't have to update 1000 of poms using this automation
Any help or suggestions here would be greatly appreciated as always.
You can use the arguments property to do this. Quoting can be very important.
Example:
mvn release:prepare ... -Darguments="-Dmaven.test.skip=true -DsomethingElse=whatever"
Here, I pass the maven.test.skip property (defined by the maven-surefire-plugin's test goal) through to the execution of mvn run by the prepare goal of the maven-release-plugin.

Execute one Maven plugin goal as the last

I would like to run one Maven plugin goal (jacoco:dump) at every Maven build of my project, no matter the phase of the build, and it should be the last goal executed.
One idea I would implement is link the jacoco:dump goal to the compile phase, in order to execute it at every build, but then it would be not possible to execute the dump as the last goal of the last phase.
Execution order
validate -> compile -> (any other phase and plugin goal execution) -> jacoco:dump
So if I build with mvn verify, jacoco:dump could be executed after the verify phase; if I build with mvn compile the jacoco:dump could be executed after the compile phase etc.
Is there any way to solve this problem with Maven?
Thank you
One (hacky) approach would be to pass the phase as a property each time. Something like:
<execution>
<id>jacoco</id>
<phase>${phase}</phase>
<goals>
<goal>dump</goal>
</goals>
</execution>
The call with mvn compile -Dphase=compile or mvn test -Dphase=test etc. Probably not the most elegant solution but pretty easy to implement.
The solution is simple but effective. We can run the plugin goal directly from command line without configuring it into the project pom.
As stated in the official documentation, when running multiple goals from command line they are executed in the given order, so I only have to put the jacoco plugin at the tail of the command:
mvn compile org.jacoco:jacoco-maven-plugin:0.7.7.201606060606:dump sonar:sonar
The example above first compiles, then executes jacoco:dump, then executes sonar:sonar

How can I atomically run mvn tests (without rebuilding source code)?

I want to run a maven project lifecycle starting, and ending, with the unit tests.
How can I skip recompilation and re-resolution of dependencies and only run the test phase?
If you start maven by calling a phase it will execute all lifecycle phases up to the one you are calling. For example, when calling
mvn test
all the phases before the test lifecycle phase will be execute too: the project will be validated, sources and resources will be generated and processed, sources will be compiled, the same will happen to test sources and resources and finally unit tests will be run.
But you can also call the plugin goal that is bound to a lifecycle phase. In the case of the test phase the bound goal is surefire's test mojo. So you could call
mvn surefire:test
and no other lifecycle phase will be executed.
You can find the goals bound to each phase depending on the package type here.
You can run :
mvn surefire:test
Build your own lifecycle, or run the tests with something besides Maven (Ant, Gradle, your IDE, command-line JUnit runner, ...). That's the kind of restriction you live with when you're using Maven.

Executing a specific Maven phase

Is there any way to execute a specific phase in a maven build. For example, if I only want to run those plug-ins that execute in the pre-integration-phase, does Maven provide a means to do that?
e.g.
mvn pre-integration-phase
You can't call the life-cycle-phase itself but you can call the goals of the plugins which are bound to the life-cycle-phases.
mvn compile:testCompile
mvn failsafe:integration-test
but usually this shouldn't be needed...
No. You'd have to run the plugins manually.

Maven-assembly-plugin & incremental build

It seems that the assembly plugin (or jar/war plugin) are just dumb.
They just can't figure out whenever there is just nothing to do.
A basic "Makefile" won't recompile a target if all its dependencies are older than the target.
For maven, it seems that the packaging is done "all the time" !
if I do "mvn package" and then "mvn integration-test", Maven will process the packages again and again.
Since I build some fat-standalone jars : it takes a while !
Is it also the way is works for you, or is there something broken in my configuration.
Thanks in advance for your help,
Raphaƫl
In Maven exists a Life-Cycle which is run through every time you call a phase.
mvn integration-test
means to run all phases which are before integration-test (incl. integration-test itself) which includes in your case the package phase. Furthermore you shouldn't call integration-test cause the post-integration-test will not run in this case. You should call mvn verify instead.
The result from the above is you should simply call mvn integration-test and the package phase will run automatcially. Maven is not Make.

Resources