Maven questions - maven

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?

Related

Maven plugin removed is still usable

I added to my maven project the a PMD and checkstyle plugins. And when I run them the work perfectly. But when I remove them from the pom.xml I can still run mvn checkstyle:checkstyle or mvn pmd:pmd even though I removed them. Also after removing them I ran mvn clean install. ANy idea of what could happen ?
The commands you execute are plugin goals (plugin:goal) and unlike "mvn install" not a phase.
you can run almost any plugin on a project if maven can find it. The apache maven plugins allow that shortcut notation (pmd:pmd) since maven will try to resolve them in the apache namespace.
Plugins from other sources would need to be run with their full name, for example:
org.codehaus.mojo:versions-maven-plugin:2.5:display-dependency-updates
The plugin itself decides if it can run a goal on its own or if it requires a running reactor and only works within the maven life-cycle (usually because it depends on outputs from other phases)
So in your case: mvn install should not run the pmd plugin anymore if its not in the pom - and install is a phase. mvn pmd:pmd will run it directly with its default config - since pmd:pmd is a plugin goal.
The default plugins per packaging and phase are documented here. These may run if in the pom or not (depending on whats in the project).

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

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

Bind mvn jetty:run to a different phase?

I have a maven webapp, which can be run using jetty. If I call jetty with
mvn jetty:run
it is executed before the install phase. However, I want to run jetty at the very end of the maven lifecycle only. How can I achieve that?
Or to put it in a different way. The run goal of jetty maven plugin is bound by default to a certain maven phase. Can I change that binding?
Update: Just to make sure, I don't want to know how to execute jetty automatically each time a maven phase is executed like pre-integration-test. I just want to bind the jetty run goal to a later phase so that additional maven phases get executed when calling it manually.
That's not possible (using predefined packagings like jar or war). In Maven you run plugin's goal or phase (which starts lifecycle). If you run goal, only this goal is executed. If you run phase, lifecycle runs from the beginning to that phase included. Try to run (after mvn clean) mvn install:install (goal only) and then mvn install (default lifecycle to the install phase included).
You can create own plugins' goals to lifecycle's phases binding by creating own packaging type. Predefined packaging types (jar, war, ear, etc.) have this binding already specified.

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