Building Project using Plugins, Lifecycle in Maven (Part-3)
How can we build a project in Maven?
It can be done either by executing maven plugins or maven life cycle phases or a combination of both maven plugins and maven life cycle.
All the work in Maven is performed by plugins. Be it Project creation using archetype(refer post1), project packaging, project build etc., Maven created plugins that can carry out these tasks.
Below are the list of tasks for which the maven plugins are already available.
A plugin usage is very simple. By executing the below command(for eg.) in project root folder, we can compile all the sources(main sources and test sources) of your project.
mvn compiler
Command's Significance:
Here 'compiler' is a plugin name. It has 2 goals, 1 for compiling main sources and 1 for compiling test sources. The above command executed both the goals because we did not mention any goals in the command. We can also be more specific while executing plugins by specifying goals in the command like below:
mvn compiler:compile
mvn install:install. This command installs the project's artifact(JAR), POM to the local repository
mvn install:install-file: This command installs an extenal JAR to the local repository
Another way to run the build in Maven is by using lifecycles. Just like any other lifecycle, maven lifecycle also has several phases in its lifecycle and each phase represent a stage in the lifecycle.
clean, default, site are the 3 lifecycles maven supports. Below are the list of phases each lifecycle contains.
A life cycle can always be executed with phase just like below:
mvn deploy
the above command runs deploy phase in the default life cycle (ref above fig.). All the phases before the deploy phase(from validate to install) in the default life cycle gets executed in sequential order.
we can also run 2 phases in a single go like below:
mvn clean deploy
As I mentioned earlier, we can also run plugins and life cycle items together.
mvn clean compiler:compile
In the above command we ran maven clean life cycle phase and compiler:compile goal together. By executing the command, Maven cleans the build directory and compiles the main sources of your project.
It can be done either by executing maven plugins or maven life cycle phases or a combination of both maven plugins and maven life cycle.
Maven Plugin
(available at http://repo.maven.apache.org/maven2/org/apache/maven/plugins/)All the work in Maven is performed by plugins. Be it Project creation using archetype(refer post1), project packaging, project build etc., Maven created plugins that can carry out these tasks.
Below are the list of tasks for which the maven plugins are already available.
- Build Activities: clean, deploy etc.,
- Test activities : compiling and running test cases, code coverage, etc.,
- Web Applications :Deploying and running web applications
- Reporting tasks : changelog, javadoc, project reports etc.,
A plugin usage is very simple. By executing the below command(for eg.) in project root folder, we can compile all the sources(main sources and test sources) of your project.
mvn compiler
Command's Significance:
Here 'compiler' is a plugin name. It has 2 goals, 1 for compiling main sources and 1 for compiling test sources. The above command executed both the goals because we did not mention any goals in the command. We can also be more specific while executing plugins by specifying goals in the command like below:
mvn compiler:compile
(or)
mvn compiler:testCompile
Another example:mvn install:install. This command installs the project's artifact(JAR), POM to the local repository
mvn install:install-file: This command installs an extenal JAR to the local repository
Maven LifeCycle
Another way to run the build in Maven is by using lifecycles. Just like any other lifecycle, maven lifecycle also has several phases in its lifecycle and each phase represent a stage in the lifecycle.
clean, default, site are the 3 lifecycles maven supports. Below are the list of phases each lifecycle contains.
A life cycle can always be executed with phase just like below:
mvn deploy
the above command runs deploy phase in the default life cycle (ref above fig.). All the phases before the deploy phase(from validate to install) in the default life cycle gets executed in sequential order.
we can also run 2 phases in a single go like below:
mvn clean deploy
The above command runs the clean and deploy life cycles phases sequentially. It runs all the previous phases before them.As I mentioned earlier, we can also run plugins and life cycle items together.
mvn clean compiler:compile
In the above command we ran maven clean life cycle phase and compiler:compile goal together. By executing the command, Maven cleans the build directory and compiles the main sources of your project.

Comments
Post a Comment