Maven is a build automation tool used primarily for Java projects. Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories such as the Maven 2 Central Repository, and stores them in a local cache. This local cache of downloaded artifacts can also be updated with artifacts created by local projects. Public repositories can also be updated. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages.
Need of Maven
Most of the real-time projects require a lot of dependencies (jars) for the proper execution. Some dependencies must be mandatorily available during compile-time while others are needed at run-time. Managing these jars availability at different stages of the project execution is one of the major issues.
What is a build process?
“Build” is the terminology that refers to the below set of activities:
- Compilation of source code into binary code.
- Packaging binary code into jar/war/ear.
- Running Test Scripts.
- Deploying to production environment.
- Creating the release notes/documentation.
Need for Build tool?
- Build tools are required to manage the dependencies present in the different project modules
- Building applications can be automated and made simpler and faster as well.
- Build tools have the ability to recognize the changes that have occurred in the project from a given time stamp and will rebuild only the changed files selectively.
Installation of Maven
In Eclipse IDE – Oxygen version. Maven need not be explicitly installed but only for versions older than Eclipse Luna, download m2e plugin of Eclipse based on the version.
- Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made.
- We use archetypes to try and get our users up and running as quickly as possible by providing a sample project that demonstrates many of the features of Maven, while introducing new users to the best practices employed by Maven.
- Archetypes are found large in number and every archetype is associated with a unique number.
- Archetypes are available in repositories along with their meta-data.
- To create a new project based on an Archetype, you need to call mvn archetype:generate goal, like the following
- Using IDE – By selecting the archetype from the existing list of archetypes displayed in the IDE
Maven Repositories classified as
Local Repository : Local repository is a folder that is located in the machine of the developer where the dependencies downloaded by Maven are stored. Local repository is not created manually whereas, it gets created by default when Maven download happens for the very first time.We can tend to work offline when the local repository contains all the dependencies that are required for a particular project. By this way, local repository helps minimizing the network usage.
Remote Repository : As the name implies, remote repository is the one that resides remote to the developer’s machine. Remote repository can be accessed by Maven only when the machine is connected to the Internet/Intranet. Maven will contact remote repository when dependencies are missing in the local repository. Contacting remote repository needs some special configuration details to be enabled either in pom.xml.
Central Repository : Central Repository is the one that is provided and managed by the Maven community and it usually contains the commonly used libraries for project builds. Central repository is located remote to the developer’s machine.Access to Central repository requires Internet connection.
Adding dependencies in POM.xml
- Visit the Maven Repository site.
- Type the dependency that your project requires in the Search field.
- Click on the version of the artifact that your project requires.
- Copy paste the code into your POM.xml file inside dependencies tag.
validate the project is correct and all necessary information is available
compile the source code of the project
test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
take the compiled code and package it in its distributable format, such as a JAR.
run any checks on results of integration tests to ensure quality criteria are met
install the package into the local repository, for use as a dependency in other projects locally
done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
Maven Site life-cycle
execute processes needed prior to the actual project site generation
generate the project’s site documentation
execute processes needed to finalize the site generation, and to prepare for site deployment
deploy the generated site documentation to the specified web server
By default, all the basic necessary plugins for the build process are bounded to the build life-cycle phases. Maven is – at its heart – a plugin execution framework; all work is done by plugins.Build plugins will be executed during the build and they should be configured in the <build/> element from the POM. Reporting plugins will be executed during the site generation and they should be configured in the <reporting/> element from the POM.
Below is list of the plugins :