Maven is a project management tool which encompasses a project model, a set of standards, a dependency management system, and logic for executing plugin goals at defined phases in a life cycle. The result is a tool that can be used for building and managing any Java-based project.
- More consistent project structure – All maven projects have a common structure, which makes it easier to understand.
- Maintain a project as light weight project – We don’t need to maintain a jar file inside the project. Instead of this, it directly downloads all the jars while we are running this project.
- Dependency Management – If a project depends on library, we just add a dependency on pom.ml file.
- Better debugging – Maven repositories allow an artifact’s source code to be published alongside the artifact’s JAR file; so that we can easily identify the error.
- It has plugins for IDEs like Eclipse, NetBeans, IntelliJ IDEA.
Steps to install Maven
The following are the steps to install maven:
- Make sure JDK is installed
- Download Apache maven.
- Set environment variable name as MAVEN_HOME and variable value as path of apache maven where it’s should be installed.
- Then, set environmental variable name as M2_HOME and variable value as same as MAVEN_HOME.
- Finally, set system variable path as C:\Program Files\apache-maven-3.3.9\bin\.
To build a Maven project via command line, run the mvn commands. The command should be executed in the directory that contains the relevant Project Object Model (pom) file. The following commands will execute the maven project from CLI.
- Command to view the maven version
- Command to create a maven project
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app
The above command will create the maven project structure as shown below.
Project Object Model (POM)
It is a fundamental unit of work in Maven. It is an XML file that resides in the base directory of the projects as pom.xml. The POM contains the information about the project and various configuration detail used by Maven to build the project(s).
POM also contains the goals and plugins. While executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, and then executes the goal. Some of the configuration that can be specified in the POM are following.
- Project Dependencies
- Project Version
- Build profiles
Before creating a POM, we should first decide the project group id, name (artifact id) and its version as these attributes help in uniquely identifying the project in repository.
The POM Example as follows:
- Command to clean the build life cycle
- Command to install the packages in the local repository, for use as a dependency in other projects locally
- Command to compile the source code
- Command to packs the compile code in a distributed format
- Command to run the test against the compiled source code using suitable unit test framework.
- Command to copies the final package to the remote repository
mvn deploy:deploy-file -DgroupId=<group-id>\
-DartifactId=<artifact-id> -Dversion=<version> \
To release project artifacts outside the immediate development team
- The maven release plugin is executed in 2 stages
- Preparing release
- Performing release
- The plugin will record release information into a new revision of the project’s xmlfile.
- Maven will compile, test and package the versioned project source code into an artifact. The final deliverable will then be released into an appropriate maven repository.
In case of multiple development environment, Maven can set-up to work as per standards in a very short time. As most of the project setups are simple and reusable, Maven makes life of developers easy while creating reports, checks, build and testing automation setups.