I'm having a tough time seeing the "forest through the trees" on some Maven concepts here. I understand that Maven comes preconfigured with a slew of so-called "Build Lifecycle Phases" that begin with validate
, and test
and end with deploy
.
I am coming to Maven from Ant where you organized major build stages into targets (which can depend on other targets in a linear fashion), and then you decompose your targets into procedural tasks. Maven seems to supports this but in the form of goals.
My question (since I am used to thinking of building in terms of targets and tasks) is: how do these lifecycle phases (package
, verify
, etc.) relate to goals? Does one need to configured which goals to run at which phase, or is this done by Maven automagically somehow?
Or, do the goals come predefined with which phase they belong in?
I'm just not seeing how one orders goals to create a custom build that works for them or their organization.
Thanks in advance for any clarity!
In Maven the "verbs" are goals packaged in Maven plugins which are tied to a phases in a build lifecycle. A Maven lifecycle consists of a sequence of named phases: prepare-resources, compile, package, and install among other. There is phase that captures compilation and a phase that captures packaging.
Maven Lifecycle: Below is a representation of the default Maven lifecycle and its 8 steps: Validate, Compile, Test, Package, Integration test, Verify, Install and Deploy.
A Build Lifecycle is Made Up of Phases validate - validate the project is correct and all necessary information is available. compile - compile the source code of the project. test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
There are three built-in lifecycles: default: the main lifecycle, as it's responsible for project deployment. clean: to clean the project and remove all files generated by the previous build. site: to create the project's site documentation.
Many Maven plugins bind specific goal(mojo) to a specific lifecycle phase.
E.g. look for documentation of maven-jar-plugin:jar
goal. It says:
Binds by default to the lifecycle phase: package.
Also, lifecycle itself is specifying what tools need to be run for each phase. For default packaging types these tools are predefined by Maven and explained here -> http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings.
But this example shows how to create totally custom lifecycle binding -> http://www.sonatype.com/people/2009/08/create-a-customized-build-process-in-maven/. As you can see you can bind any combination of plugin goals to a specific lifecycle phase.
Last, but not least, you can add plugin execution manually through the build/plugins
element of your pom file. If you do that the plugins in specific phases will execute in a FIFO order.
Also, it may help you to see the Effective POM
with all these defaults spelled out. Many IDE's offer this option for Maven projects, but you can also see effective pom from the command line:
mvn help:effective-pom
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With