Maven cheatsheet

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”

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
.... 
</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 :

...
<groupId>com.example</groupId>
<artifactId>TaskMaster</artifactId>
<packaging>war</packaging>
<version>1.0</version>
...

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


<project> …...
...
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>
    ...
  </dependencies>
  </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:

<project>
...
  <repositories>
    <repository>
      <id>my-repo1</id>
      <name>your custom repo</name>
      <url>http://jarsm2.dyndns.dk</url>
    </repository>
  </repositories>
...
</project>

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

<settings>
 ...
 <profiles>
   ...
   <profile>
     <id>myprofile</id>
     <repositories>
       <repository>
         <id>my-repo2</id>
         <name>your custom repo</name>
         <url>http://jarsm2.dyndns.dk</url>
       </repository>
     </repositories>
   </profile>
   ...
 </profiles>

 <activeProfiles>
   <activeProfile>myprofile</activeProfile>
 </activeProfiles>
 ...
</settings>

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.

mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar

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.

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <packaging>pom</packaging>
</project>

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:

  <parent>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>my-parent</artifactId>
    <version>2.0</version>
    <relativePath>../my-parent</relativePath>
  </parent>

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

 ...
  <!-- "Project Build" contains more elements than just the BaseBuild set -->
  <build>...</build>

  <profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
      <build>...</build>
    </profile>
  </profiles>
...
</project>

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.

 <build>
    ...
    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
...

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.

<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<configuration>
					<username>admin</username>
					<password>admin</password>
					<server>myserver</server>
					<path>/TaskMaster</path>
       			 </configuration>
			</plugin>

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

Advertisement

2 thoughts on “Maven cheatsheet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.