In this post we are going to see the definition and usage of some important terms in Apache Maven framework. Before going into the depth of maven it is very important to understand the essence of these terms. So , let’s begin
1. What is Maven : – There is a misconception among many people about What exactly Maven is ? and What it do ? . Some people say that , it is a build tool used to generate artifacts in the form of jar, war and ear file from your source code , other group of developers say that it is a Project Management Tool that has a Project Object Model (pom file) , a standard life cycle , executing different goals at different phase in the life cycle , do the dependency management etc . Actually both of them are correct , Maven is much more than a traditional build tool . How ?? We will see that in just a minute.
Maven strictly believes in the policy of “Configuration over Convention” that says your framework should be in the running state even with the default configuration. So, how maven implement this , when you create any new maven project maven assumes that your source code will be in
and resources for the same will be present in
, test classes will be in
and when you build this project it will generate you a nice JAR file for you by default until and unless you have customized the packaging type of it. This is not the only example of how maven uses “Configuration over Convention” , there are other things where the same rule is also applied like in core plugins , generating web sites , packaging distribution.
Now you might be worrying what the heck is this goal, phase and dependency management , core plugins etc don’t worry we are going to see all those very soon.
2. What is Maven Phase : – In simple terms Maven “Phase” is an intermediate step of a process and that process in Maven terminology known as “Build LifeCycle” . A lifecycle can have multiple phases in its execution. If we look at the default lifecycle of maven it contains multiple phases in it and each of them will execute sequentially that means next phase in the lifecycle will be executed only when previous phase to it is completed. Below are the maven phases that come under default build lifecycle.
- validate – validate the project is correct and all necessary information is available.
- compile – compile the source code of the project.
- package – take the compiled code and package it in its distributable format, such as a JAR.
- install – install the package into the local repository, for use as a dependency in other projects locally.
- deploy – done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
This is only a very small of list of maven phases , for more list you can always refer Maven Specs here.
You can always call invoke a maven phase directly in just 2 magical words and when you invoke any phase, all the phases that comes before the invoked phase will be invoked automatically:
mvn NAME_OF_PHASE (where name of phase can be any phase) e.g mvn compile – it will call all the phases that exists before compile ,in this case it is validate. mvn install – it will invoke first validate then compile then package and then in the last install.
Now let’s add some more fun to it , As per maven spec each and every phase can have multiple goals associated with it.
3. What is Maven Goal : – In simple terms you can say that maven goal is a subtask or more precisely a specific task of a phase not necessary because it is quite possible that a maven phase can have zero or more than 1 goals associated with it. It’s always in your hand to declare and bind these goals with the phases. Now it is also possible that same goal can be bound to multiple phases and vice-versa. Now if you are thinking that is it possible to execute any goal without binding it with any phase , then Maven also provides you flexibility of doing this , you can execute any goal by direct invocation of it outside of the build lifecycle. A goal can also be clubbed with multiple phases until and unless it makes sense . For example
mvn PHASE_1 GOAL_1 PHASE_2 PHASE_3
In the above case first your PHASE_1 will be executed , then your GOAL_1 will be executed and in the last PHASE_2 and PHASE_3 respectively. You can chain upto any level you want.
More importantly if any phase do not have any goal bound to it than that phase will not execute , generally almost each and every phase binds to at least one goal. For ex: In case of packaging type jar
compile phase is bind to compiler:compile
package phase is bind to jar:jar
And lot more.
Yes, you are thinking right packaging type also impact which all goals will be associated with the phase . Now in case of packaging type pom it only binds install and deploy phase to the goals and others remain skipped.
So, till now we have seen the 3 most important concepts in Maven Framework , in the next post we are going to see some more terminologies like dependencies , plugins etc.
Please share your thoughts and comments on the same or correct if you found any mistake in the article.