Maven Cheat Sheet



Below is cheatsheet for maven, these are fairly basic things:

A quick reference to Maven commands. Mvn -Dtest= test. Tests only the specified class. Mvn -Dmaven.surefire.debug test. Enables remote debugging of tests on port 5005. Surefire will block on the port until you connect with your debugger. Mvn -Dcargo.wait=true -P int integration-test. Runs CS in Tomcat via cargo. Maven Cheat Sheet Knowing Maven is a must-have skill for any respected Java developer. Unfortunately, memorizing the command line options and phases can be tough.

POM : Project Object Model
A POM contains all the information about a project pertaining to:
-configurations
-dependencies
-packaging
etc…

Lets begin with the beginning steps:
The root element in POM is “project”

Naming a project :

Maven-jaxb2-plugin Cheat Sheet

Maven

Maven Cheat Sheet Pdf

1.) groupId – com.example
2.) artifactId – TaskMaster
“groupId:artifactId” denotes a single project
3.) version – 1.0
so full name is – groupId:artifactId:version

Packaging :
this could have values pom, jar, maven-plugin, ejb, war, ear, rar, par
When no packaging is declared, Maven assumes the artifact is the default: jar.

Hence our first block of POM looks like :

Dependencies One can add all the dependencies like JARs inside the root element project

Maven will try to get the dependency from your local repository (~/.m2/repository), if not found, then it will download from the default Maven central repository – http://repo1.maven.org/maven2/
This entry of default repository is mentioned in Super POM which is inherited by all POMs.

Multiple repositories
In case there is an enterprise repository from which the dependencies should be downloaded, we could add multiple repositories.
There are 2 different ways that you can specify the use of multiple repositories.
1.The first way is to specify in a POM which repositories you want to use, by using repositories tag:

2. The second way could be to make a profile in ~/.m2/settings.xml give repository url and activate it.

See the complete documentation here.

Cheat

Sometimes it may happen that a project can not be downloaded from a repository then we could:

1.Install the dependency locally using the install plugin.

This install plugin will create a POM for you with the given address.

Inheritance
We can define parent POM, its just another pom with packaging type as pom.

Dependencies and plugins are few of the important elements in the parent POM that are inherited by its children.

It can be used like this:

Build Settings
The “build” element: Conceptually it is divided into 2 segments
a.) build for project
b.) build for profiles

Resources

Another feature of build elements is specifying where resources exist within your project. Resources are not (usually) code. They are not compiled, but are items meant to be bundled within your project or used for various other reasons, such as code generation.

Plugins
Beyond the standard coordinate of groupId:artifactId:version, there are elements which configure the plugin or this builds interaction with it.
Plugins are extensions or plugins developed to support additional features in maven. For example, the Apache Tomcat Maven Plugin provides goals to manipulate WAR projects within the Apache Tomcat servlet container. You can run your War Apache Maven project through Apache Maven without deploying your WAR file to an Apache Tomcat instance.
This plugin can be used by the plugin attribute in the POM.

Maven-jaxb2-plugin cheat sheet

This is just a list of few of the important things in POM, the complete reference is here.