Monday, March 7, 2016

Maven: lifecycle, phases and goals



A goal is a "Unit of work" in maven, a specific task that may be executed as a standalone goal or along with other goals as part of a larger build. The collection of one or more goals makes a plugin. Examples are test goal in surefire plugin, compile goal in compiler plugin. Goals have default values for its parameters. If you don't pass, it will use default value or prompt for you to enter. The core of Maven has little to do with any specific task involved in your project's build. By itself maven, doesn't know how to compile your code or make a jar file. It delegates all of this work to maven plugins like compiler plugin or jar plugin, which are downloaded on as-needed basis and periodically updated central maven repository. When you download Maven, you are getting the core of Maven, which consists of a very basic shell that knows only how to parse command line, manage a classpath, parse POM file and download the Maven plugins as needed. By keeping the compiler plugin separate from Maven's core and providing for an update mechanism, Maven makes it easier for user to have access to other options in compiler.
A Maven lifecycle is made up of series of phases, each phases are distinguished based on what happens when such phases are executed. For instance, validation, testing, compiling, deployment etc. And each of those phases are bound to one or more goals. A lifecycle by default begins with a phase to validate the basic integrity of the project and ends with a phase that involves deployment of the project. Following diagram shows what phases(but not all phases in the Maven lifecycle) of Maven lifecycle is bound to which goals of various plugins:
To Summarize, when we execute mvn install, Maven executes all the phases up to the install phase, and in the process of stepping through the lifecycle phases, it executes all the goals bound to each phase.

No comments: