How to invoke maven default lifecycle - maven

If i call
mvn clean install
maven knows that clean is a lifecycle and install is a phase of the default lifecycle
if i call
mvn deploy
maven will execute all phases of the default lifecycle sequentially.
Is there a way to call the default lifecycle by giving a lifecyle name (instead of executing the last phsae of the lifecycle)?
EDIT: So the question is: is there a command
mvn lifecyclename
that start execution of the default lifecycle?

There is no command to run a lifecycle based on lifecycle name. So you can't do a mvn Default and expect it to run upto Default:deploy. You will have to mention a task of a cycle like test, package, clean and the life-cycle that owns this task will get active.
It does not make sense to have life-cycle as an argument. It will be confusing. For example running mvn clean is the Clean life-cycle or clean task?
Or, it will be more verbose to type mvn clean will run Clean life cycle; and mvn clean:clean will run Clean life cycle until clean task.
Maven has three life cycle. Executing a task (say task_N) of any of the life cycle will result in executing the whole life-cycle until that task (task_N). The three life cycles are Clean, Default, and Site.
For more details see here Introduction to Maven Life-cycles and task order
You see when you execute say, mvn test these are the things gets executed in that order
validate > initialize > generate-sources > process-sources > generate-resources > process-resources > compile > process-classes > post-process > generate-test-sources > process-test-sources > generate-test-resources > process-test-resources > test-compile > process-test-classes > test
You can't skip any of the default tasks. You may hook plugins that gets gets executed during a task.

There seems to be a misunderstanding. Further reading could clarify that
mvn clean
does not invoke the whole clean lifecycle. In fact there is a clean phase in the clean lifecycle. And the command executes this phase and not the lifecycle.

As far as I know you cannot execute an isolated lifecycle phase.
But you can execute an isolated plugin goal .
mvn deploy:deploy
That won't trigger any plugin execution tied to deploy phase. But you can always add more plugin execution to the command line. So I better go with a profile skipping all plugins executions.
If you want to execute a myplugin:mygoal that was tied to deploy phase then
mvn myplugin:mygoal deploy:deploy
But the configuration of the execution must be in the cli.
But you better skip plugin executions that you do not want. only skip test and integration-test in command line. But you can achieve that with a profile that sets the configuration for the default cycle to skip.
<profiles>
<profile>
<id>skip</id>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
...
And then summon with
mvn deploy -Pskip

Related

How does maven know which life-cycle to run?

a simple maven question here ?
Maven documentation said, there are three built-in life-cycles:
clean build(default) site
but how does maven know when to enter the site life-cycle?
You cannot run maven lifecycle by lifecycle name. You have to invoke phases of lifecycles.
mvn <phase> //e.g., mvn compile
In the site lifecyle, Maven defined 4 phases, in this order:
<phases>
<phase>pre-site</phase>
<phase>site</phase>
<phase>post-site</phase>
<phase>site-deploy</phase>
</phases>
So, if you run mvn site-deploy, Maven knows you are in the site lifecyle. It will execute all the phases of the site lifecyle up to the site-deploy phase.
The order of execution will be: pre-site > site > post-site >site-deploy
You must specify it on the mvn command line.
e.g. You can simply run
mvn site

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

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

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

Maven questions

When I just type mvn command in my machine where pom is located, which phase it is going to run? mvn install or deploy??
When I use mvn eclipse:eclipse in my machine to convert maven project to eclipse project, what phases it is going to execute from default life cycle? is it going to run all the phases again?
When I just type mvn command in my machine where pom is located, which phase it is going to run?
No goal is executed, instead you'll get:
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal [...]
You can specify default goal with the following pom.xml declaration:
<build>
<defaultGoal>install</defaultGoal>
...
</build>
When I use mvn eclipse:eclipse in my machine to convert maven project to eclipse project, what phases it is going to execute from default life cycle?
It's described in the documentation:
Attributes:
Requires a Maven project to be executed.
Invokes the execution of the lifecycle phase generate-resources prior to executing itself.
See also
How do you specify a string of goals as the defaultGoal in maven 2?

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