Introduction
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.
Pros
- 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\.
Maven Commands
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
mvn –version
- Command to create a maven project
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
The above command will create the maven project structure as shown below.
Project Structure
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
- Plugins
- Goals
- 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
mvn clean
- Command to install the packages in the local repository, for use as a dependency in other projects locally
mvn install
- Command to compile the source code
mvn compile
- Command to packs the compile code in a distributed format
mvn package
- Command to run the test against the compiled source code using suitable unit test framework.
mvn test
- Command to copies the final package to the remote repository
mvn deploy:deploy-file -DgroupId=<group-id>\
-DartifactId=<artifact-id> -Dversion=<version> \
-Dpackaging=<type-of-packaging> \
-DrepositoryId=<id-map-on-server-section-of-settings.xml>\
-Durl=<url-of-the-repository-to-deploy> \
-DpomFile=pom.xml -Dfile=<path-to-file>
To release project artifacts outside the immediate development team
- The maven release plugin is executed in 2 stages
- Preparing release
- Performing release
mvn release:prepare
- The plugin will record release information into a new revision of the project’s xmlfile.
mvn release:perform
- 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.
Conclusion
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.
Reference