Bind mvn jetty:run to a different phase? - maven

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.

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

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

Can I get the phase/plugin:goal used in the maven command?

We use maven for our build process and are now modifying it to use grunt to build the client side files. We have maven kick off the grunt task, but similar to maven, there are some things that we want to do only if a certain phase is run. For example, if the deploy phase is run on my maven project, I want to tell grunt to also deploy the js artifact I am creating. Otherwise, if something like compile was run in the maven project, I don't want to deploy my js artifact through grunt.
I can pass properties just fine into grunt, but I don't know how to get a property that can tell me what phase was run. Is it possible to get the phase or the plugin:goal that was run? Or even the command that was run?
The way I have seen other Maven plugins handle this is to use different mojos, bound to the desired phases. For your example, you'd write a deploy mojo and bind it to the 'deploy' phase.
The maven-resources-plugin does this. It has both Resources and TestResources mojos. One is bound to the process-resources phase, the other to process-test-resources. So, if I was to run mvn compile the Resources mojo would execute, but the TestResources mojo would not, as process-test-resources occurs after the compile phase.
Source code for resource plugin

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.

Resources