maven commands and concepts

Maven Commands and Concepts

Apache Maven is a build automation tool for Java projects. This tool is based on the Project Object Model (POM). Maven is very useful and powerful in terms of building Java projects, managing dependencies, and can be easily integrated with the source code management system. Maven helps the developer to automate almost all the application development process from creating the project with folder structure, executing code and compiling, testing, packaging and deploying to any environment. Working with maven is very easy if you know the Maven Commands and Concepts.

Basic Maven Concept.

Maven will start a project to compile, build, test, pack and deploy based on pom file. This pom file will be available in the java project root directory. It may have sub pom files that may call as modules.

Maven Concept
Maven Concept

So, the process lifecycle of Maven starts from gathering libraries listed on pom file which will be imported in java source code. Then, Maven will compile, build, test or deploy as per the available goals in the pom file. All the plugin and libraries either can be downloaded from the remote repository and kept in the local maven repository or can be reused the downloaded packages from the local maven repository.

As part of Maven Commands and Concepts, we should know what the Maven lifecycle, Phases, Goals, and plugins are.

Maven Lifecycle

Maven lifecycle is the defined sequence of phases that can be executed in order. Basically, the maven has three built-in lifecycles.

  • default
  • clean
  • site

If we take default or build lifecycle, then it will have all the following phases

Typical Default (Build) phases
Typical Default (Build) phases

In clean lifecycle, it will have the following phases.

Typical Clean phases
Typical Clean phases

Then, for site lifecycle, the following phases are available.

Typical site phases
Typical site phases

Maven Phases

Then, the Maven Phase is a stage of lifecycle which is responsible for the specific sequence of Goals or tasks like compile or test.

Maven Goal

Maven Goals are typically a task. Like in Ant tool, Maven has Goals that are like fundamental execution points which will do tasks to automate the build process.

Maven Plugin

Maven plugin consists of a group of goals or tasks. So, plugins can be developed which will have the package of the sequence of the task that will help automate the build process.

Read this article to install and configure Maven in your machine.

How to Install and Configure Apache Maven on Ubuntu, CentOS, and Windows

Maven Commands

Once you installed the maven on your machine, you can experiment with the following essential command.

maven -version

which will say which version of the maven you have installed.

Java app Project Creation

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=app-name -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.3 -DinteractiveMode=false

Java Web Project Creation

mvn archetype:generate -DgroupId=org.yourcompany.project -DartifactId=application -DarchetypeArtifactId=maven-archetype-webapp

To Create archetype from existing project

mvn archetype:create-from-project

To invoke any induvial phases

We can run any of the phases by mentioning as a parameter in mvn command. Like,

mvn <phase>

Some Important phases are

  • clean – delete target directory
  • validate – validate, if the project is correct
  • compile – compile source code, classes stored in target/classes
  • test – run tests
  • package take the compiled code and package it in its distributable format, e.g. JAR, WAR
  • verify – run any checks to verify the package is valid and meets quality criteria
  • install – install the package into the local repository
  • deploy – copies the final package to the remote repository

Along with the parameter, we have some important options that can be added to mvn command.

-DskipTests=true : this compiles the tests but skips running them

-Dmaven.test.skip=true : this skips compiling the tests and does not run them

-T : number of threads

-rf, --resume-from : this will resume build from the specified project

-pl, --projects : this will make Maven build only specified modules and not the whole project

-am, --also-make : this makes Maven figure out what modules out target depends on and build them too

-o, --offline : this will help us work offline

-X, --debug : this will enable debug output

-P, --activate-profiles : this will help comma-delimited list of profiles to activate

-U, --update-snapshots : this will force a check for updated dependencies on remote repositories

-ff, --fail-fast : this will stop at first failure

Conclusion

Maven is one of the fantastic tools to automate the application development process automation. You can use this Maven Commands and Concepts as the cheat sheet while you are working with maven projects. In our upcoming articles, we will discuss more on automating development lifecycle. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

Leave a Reply