20 December, 2017
Welcome to Maven Tutorials. The objective of these tutorials is to provide in depth understand of Maven.
In addition to free Maven Tutorials, we will cover common interview questions, issues and how to’s of Maven.
Maven is a build automation tool used primarily for Java projects. The word maven means 'accumulator of knowledge' in Yiddish. Maven addresses two aspects of building software: First, it describes how software is built, and second, it describes its dependencies
Maven, different from other build tools, knows the software development life cycle. It knows that testing need to be executed after the code is compiled and not before. Test code and other third party libraries used only for testing are only required during testing and thus these should not be part of the final product and Maven leaves them out when packaging the application. Maven knows from where to download any third party libraries and their dependencies. Any third party libraries used needs to be also available at runtime. Maven provides several options how this can be achieved. As you can see from the pom file listed above, we did not tell Maven any of this. All we added were specific things required by our project, such as using a splash-screen.
Apache Maven is an advanced build tool to support the developer at the whole process of a software project. Typical tasks of a build tool are the compilation of source code, running the tests and packaging the result into JAR_ files. In additional to these typical build capabilities, Maven can also perform related activities, e.g., create web sites, upload build results or generate reports.
Maven allows the developer to automate the process of the creation of the initial folder structure for the Java application, performing the compilation and testing and the packaging and deployment of the final product. It is implemented in Java which makes it platform-independent. Java is also the best work environment for Maven.
Desired to gain proficiency on Maven? Explore the blog post on Maven Training to become a pro in Maven.
To install Maven on your own system (computer), go to the Maven download page and follow the instructions there. In summary, what you need to do is:
JAVA_HOME environment variable to point to a valid Java SDK (e.g. Java 8).
-Download and unzip Maven.
M2_HOME environment variable to point to the directory you unzipped Maven to.
M2 environment variable to point to
%M2_HOME%\bin on Windows,
$M2_HOME/bin on unix).
M2 to the
PATH environment variable (
%M2% on Windows,
$M2 on unix).
-Open a command prompt and type '
mvn -version' (without quotes) and press enter.
After typing in the
mvn -version command you should be able to see Maven execute, and the version number of Maven written out to the command prompt.
Note: Maven uses Java when executing, so you need Java installed too (and the
JAVA_HOME environment variable set as explained above). Maven 3.0.5 needs a Java version 1.5 or later. I use Maven 3.3.3 with Java 8 (u45).
Maven is centered around the concept of POM files (Project Object Model). A POM file is an XML representation of project resources like source code, test code, dependencies (external JARs used) etc. The POM contains references to all of these resources. The POM file should be located in the root directory of the project it belongs to.
When you execute a Maven command you give Maven a POM file to execute the commands on. Maven will then execute the command on the resources described in the POM.
Build Life Cycles, Phases and Goals
The build process in Maven is split up into build life cycles, phases and goals. A build life cycle consists of a sequence of build phases, and each build phase consists of a sequence of goals. When you run Maven you pass a command to Maven. This command is the name of a build life cycle, phase or goal. If a life cycle is requested executed, all build phases in that life cycle are executed. If a build phase is requested executed, all build phases before it in the pre-defined sequence of build phases are executed too.
Dependencies and Repositories
One of the first goals Maven executes is to check the dependencies needed by your project. Dependencies are external JAR files (Java libraries) that your project uses. If the dependencies are not found in the local Maven repository, Maven downloads them from a central Maven repository and puts them in your local repository. The local repository is just a directory on your computer's hard disk. You can specify where the local repository should be located if you want to (I do). You can also specify which remote repository to use for downloading dependencies. All this will be explained in more detail later in this tutorial.
Learn more about Maven Interview Questions in this blog post.
Build plugins are used to insert extra goals into a build phase. If youed to perform a set of actions for your project which are not covered by the standard Maven build phases and goals, you can add a plugin to the POM file. Maven has some standard plugins you can use, and you can also implement your own in Java if you need to.
Build profiles are used if you need to build your project in different ways. For instance, you may need to build your project for your local computer, for development and test. And you may need to build it for deployment on your production environment. These two builds may be different. To enable different builds you can add different build profiles to your POM files. When executing Maven you can tell which build profile to use.
-Better Dependency Management: Here no need to worry about transitive dependencies. Maven excels in this as it is popular among many users and also easily accessible and easy to use without any discomfort. When the user depend on one of thousands or hundreds of projects, then here comes the role of Maven to make to work on multiple project easily. Maven has high degree of domination and support.
-More powerful builds: Maven’s default plugins and life cycle allow a project to perform common build actions without touching a build configuration file. One can also clean the files and form a class files from the project. They also help in generating Javadoc for the project and also help to analyze any discrepancies that happen in the project.
-Once the maven is being purchased, then n number of person can use it and later it can be reused also. It also gives you the confidence that you have adopted the best practice in the entire industry.
-When using Maven, it is simple to create an element and combine it to a single project.
-Maven projects are easily maintainable as they have scanty wonder and go on the same and single process.
-Study achieves are not safe always.
-Learning maven is long and time consuming in comparison with ANT.
-If in a project Maven is not used properly then there will be chances for more duplication.