Building and Managing Java Project with Maven

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.

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:

  1. Make sure JDK is installed
  2. Download Apache maven.
  3. Set environment variable name as MAVEN_HOME and variable value as path of apache maven where it’s should be installed.
  4. Then, set environmental variable name as M2_HOME and variable value as same as MAVEN_HOME.
  5. 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.

Salesforce Maven

Project Structure

Salesforce Maven

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:

Salesforce Maven

  • 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
    1. Preparing release
    2. 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

http://books.sonatype.com/mvnref-book/pdf/mvnref-pdf.pdf

https://www.tutorialspoint.com/maven/maven_tutorial.pdf

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Release Process

Release Process is a structured process with planning, scheduling, and controlling the project in each stage or environments with testing and deployments. It makes deployment stable and smooth.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.