Maven Cheat Sheet



  1. Maven Commands Cheat Sheet
  2. Maven Cheat Sheet Pdf

All cheat sheets, round-ups, quick reference cards, quick reference guides and quick reference sheets in one page. Maven (Apache Maven) W. Here is your FREE guide to finding the perfect fit! The Cheeky Cheat Sheet will be auto delivered to your inbox 100% for free when you check out. Use this guide to find your perfect Hey Mavens! Fit, and feel free to email us at hey@heymavens.com with any questions. Maven: mvn command cheat sheet Spring-Boot REST API with CORS App Maven war file deploy to Tomcat Spring-Boot / Spring Security with AngularJS - Part I (Introduction) Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular) Spring-Boot / Spring Security with AngularJS: Part III (Form-based Authentication).

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

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 :

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.

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.

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

Maven is a build automation management tool in java projects. Most of the people working on java related technologies know about the maven usage. I am listing down the list of Maven commands with examples reference which is very helpful for developer. This list can be used by all the Java developers for their daily usage in projects.

Please have a look of my previous article maven installation. For the maven installation, the JDK is required.

Listing down all the maven command tutorial at a high level.

  • How to create a sample maven standalone project
  • Creating a sample web standalone project
  • Clean project
  • Compile project
  • Building web apps
  • Deploy project
  • Run unit and integration tests
  • Ignore test execution
  • Generate java documentation for the project
  • Maven debug
  • dependency tree and download
  • maven profiles
  • Generate site

Creating a maven standalone project

This is the starting step for any Java project to create. This command runs at the command prompt and creates a project with project name i.e. artifact id. This project is located in group “org.cloudhadoop” using groupid,.

After running this command for the first time, this tries to download all the required artifacts downloaded from remote repositories ( from maven .. etc) and copy to your local repository and after that, it creates a project. This project contains the src/main/java and src/test/java folders which contain the HelloWorld java program in the main folder and test class for hell0world program in the test folder.

Maven cheat sheet pdfMaven

Creating a web standalone project

As you know target folder contains all your compiled classes, as well as the jar, war files when you run the project with maven install.

Clean is maven predefined goal, this command deletes all the contents in the target folder.

compile maven project

compile is maven predefined goal, this command compiles all your java classes which include src files as well as text files in your project.

Build web apps

maven package goal is used to build maven applications.

Maven-jaxb2-plugin cheat sheet

As you know any Java project can be packed as jar or war.

The package is a maven predefined goal. By giving this command, first, compile all the java files(using compile goal) and run all your test classes and copy all these files to target folder and create a jar, war file. The final output for this command is jar/war of your project located in the target folder.

Deploy/install project

When we run this command under your project, it will do all the tasks in “mvn package” and create the required jar/war file in target folder.

Maven install goal is used to deploy the project(jar/war) to the local repository. and local repository location is /.m2/repositories/groupid/.
Maven deploy goal is used to deploy the project to the remote repository like the nexus. so that other developers can use this artifact in their module. remote repository location is specified in pom.xml.

Run unit and integration tests

Test goal is used to run only the test classes in your project. The test files are located under src/test/java and copy the result to your target/test-classes, target/reports if any.

Sheet

Ignore test execution

In 4#, with maven package command run, tests are also executed. If you want to skip the unit tests, The we can use this command. You can use this option with install goal also.

Generate java documentation for the project

This will generate java documentation for your project. And the generated java doc report can be found in the target folder

Maven Commands Cheat Sheet

Maven Cheat Sheet

This will includes API documentation for your Java classes in your project.

Debug Maven

This command is used to start the maven goals in debug mode and gives logging information. This command gives more information like what artifact is failing for what reason.

This command can be used when you are not getting any clue for your maven project execution failure.

dependency commands

We have many command to list out depedency tree which tells about direct and indirect depedencies of an maven project.

There is also an command to download all depedencies without doing anything

to download specific artifact depedency, we have to provide group, artifact id and version as given below.

maven profiles

Maven provides the specific profiles based on user/environment and global

Suppose your project wants different settings for Production and Development.

You can define the profiles in pom.xml

You can configure environment properties in each profile, This will be actived with command line option -D.

The same can be activated with following commands

For production

Maven Cheat Sheet Pdf

For running with development configuration

Generate site

The predefined goal in maven is used to generate site documentation in formatted style.

Please let me know if you like my post and share your comments.