Maven run plugin goal and preceding phases - maven

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.

Related

Create custom Maven script to run other maven scripts in given order

In gradle I can define task that runs other tasks in particular order, is it possible to do the same in maven?
For example, instead of running
mvn clean spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"
or
mvn clean spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=prod"
I want to create single scripts like
mvn server-run:dev or mvn server-run:prod respectively
In Maven, goals are executed in the order of build phases and then in the order the goals are defined in pom.xml.
You could attach different execution goals to same build phase and place them in certain order in pom.xml. You will need to add "execution" nodes to spring-boot plugin. Follow to further reading for inspiration: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Execute maven goal in command line with phase as parameter

I want to run a maven plugin goal using command line. This plugin is not defined in my pom and for this reason can not specify the phase with configuration. Even so, maven is able to find it in the repo and execute the goal properly. The problem is that these goals are executed in the order they appear in the command line, is there a way to link this goal to a specific phase with a command line parameter? I'm looking for something like:
mvn org.something:plugin-name:1.0.0:plugin-goal -phase=test package
This would execute all the phases before package and in the test phase, my goal would be executed? Is it possible?
No.
The way to solve this problem is to add the plugin to the POM and specify the execution in the phase test.

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

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

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.

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.

Resources