Difference between Maven source plugin jar and jar-no-fork goal? - maven

Could you give me a detail explanation about the difference between jar and jar-no-fork goal?
I see it from official site, but I can not get a clear idea for this.

My interpretation: the jar goal is meant to be run from the command line (mvn source:jar), the jar-no-fork is meant to be bound to the lifecycle.
If you look at the docs for the jar goal the key phrase is "Invokes the execution of the lifecycle phase generate-sources prior to executing itself." If you configure your POM to run the source:jar goal as part of the lifecycle, Maven will re-run all of the goals bound to generate-sources and its predecessors. If you have many plugin goals bound to the validate or initialize phases all of those will run twice, lengthening the time of your build.
In contrast, jar-no-fork is what you attach to the build lifecycle because it expects to be bound to a phase somewhere after generate-sources and will not run the bound goals again.
I have verified this behavior by running Maven 3 with -X and reviewing the plugin executions that run.

Related

Maven run plugin goal and preceding phases

Usually when we run a plugin goal in maven directly from the command line, the build phases preceding the one the plugin goal is bound to will not be run.
How can we run the plugin goal and all its preceding build phases?
There is no special support for this. But you can call
mvn preceding-phase goal:you-want-to-call
which will be essentially what you want.

Phase name collisions in custom lifecycles in Maven?

AFAIK, you can create a custom build lifecycle in maven.
Also, AFAIK, you cannot tell maven to execute a lifecycle. You can either:
Tell maven to execute a phase: In this case, maven finds in which lifecycle this phase is, and then executes all phases in that lifecycle, up to the phase specified.
Tell maven to execute a goal, by specifying it as mvn plugin_name:goal_name
So, assume that I have created a custom lifecycle. And assume that this custom lifecycle has a phase named install.
When I write mvn install, will maven execute the default lifecycle (which has a phase named install) or my custom lifecycle (which also has a phase named install)?
How will maven determine which lifecycle to follow?
This is not supported (yet), though recently Stephen started a thread about it: http://maven.markmail.org/thread/z57dzgunecgfcrf7

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

When Maven executes phase or goal or lifecycle?

When I was reading about Maven goals, phases on Stack Overflow, I came across two links in which one says:
when you execute maven you can specify a goal or a phase.
– What is the difference/relation between maven goals and phases?
and the other says:
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.
– Executing a specific Maven phase
Which one is right? Or am I not understanding it?
Also can some give me simple examples Maven executing lifecycle/phase/goal.
And also Maven knows that it has to run phase or a goal? E.g. when I say mvn install, is it install phase or goal?
Bombya Bo,
Think of Maven build life cycle like a fancy meal which has sequential phases:
starter (resources)
main (compile)
dessert (package)
coffee (install)
digestif (deploy)
A goal is the actual food being served during that phase.
starter: gazpacho
main: steak; gravy; fries
dessert: tiramisu
coffee: cappuccino
In this analogy:
pom.xml file is the equivalent of today's menu written on the chalk board
calling mvn on the command line is the equivalent of placing your order to the waiter
you can have more than one item of food/goal bound to a single phase (steak+gravy+fries)
If you like the house defaults, then you can just order the first N courses off the fixed menu:
"I'll take the 2 course meal"
$ mvn compile
You'll get everything up to and including the main course (ie gazpacho followed by steak+gravy+fries).
Calling a single goal is the equivalent of ordering a la carte:
"I'll take a Cobb salad plus the 2 course meal"
$ mvn javadoc:javadoc compile
If you want that goal to become a permanent addition to the menu, then add it to the pom file. That brings us back to calling:
$ mvn compile
which results in gazpacho+Cobb, followed by steak+gravy+fries.
A last point about binding a goal to a phase.
By default, each goal will run during a certain phase (Cobb salad usually served as a starter).
But you can override the phase binding, which is like telling the waiter "I'll have a Cobb salad, but bring it at the same time as the main"
Hope this clarifies the intuition behind goals vs. phases.
When you invoke a single plugin, you need to invoke a goal of that plugin. Like
mvn dependency:tree
in this case you are invoking the goal tree of the dependency plugin
On the other hand, you can invoke a phase of maven by simply doing
mvn test
In this case you are invoking not a plugin but a phase. Note that plugin goals can be bound to specific phases as well. The difference is whether you invoke the full phase (and all the preceding phases implicitly) or just a single goal of a single plugin
Here you can find all the phases of the default lifecycle (which can be modified, but i don't think you need to know about that for now), so as you can see, install is a phase

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.

Resources