JavaTMP Bootstrap Template Documentation Index

Java Build Automation Tool Apache Maven

Building and Managing Java and Java Spring Projects using the Java Build Tool - Apache Maven.

Why Maven ?

Objectives of Apache Maven

Development Environment Tools

Installing Maven

Installing Eclipse IDE

Installing Apache Tomcat

Setting up Apache tomcat with eclipse IDE

Setting up Maven with Eclipse IDE

Maven Plugins

Maven is a plugin execution framework, all work is done by plugins. This following page lists the core plugins and others. There are the build and the reporting plugins: http://maven.apache.org/plugins/index.html

Apache Maven Clean Plugin

The Clean Plugin is used when you want to remove files generated at build-time in a project’s directory. Read more about clean plugin in the page http://maven.apache.org/plugins/maven-clean-plugin/

Apache Maven Compiler Plugin

Maven Archetype

Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. It is a system that provides a consistent means of generating Maven projects. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates. -To create a new project based on an Archetype, you need to call mvn archetype:generate goal, like the following:

mvn archetype:generate ...

Creating Maven 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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>javatmp</groupId>
  <artifactId>demo1</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>demo1</name>
  <url>http://www.javatmp.com</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
└──demo1
    │   pom.xml
    └───src
        ├───main
        │   └───java
        │       └───com
        │           └───javatmp
        │               └───demo1
        │                       App.java
        └───test
            └───java
                └───com
                    └───javatmp
                        └───demo1
                                AppTest.java

Standard Maven Directory Layout

Importing a Java Maven Project into Eclipse

Creating a New Java Maven Project in Eclipse

Importing a Java Maven Project into Eclipse IDE from GitHub

Introduction to the Maven ‘Project Object Modal’ or 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

POM Packaging Types

Maven Lifecycles

mvn help:describe -Dcmd=clean
mvn help:describe -Dcmd=compile

Default or Build lifecycle

mvn install
mvn clean deploy

A Build Phase is Made Up of Plugin Goals

mvn clean dependency:copy-dependencies package

For more information about maven-dependency-plugin plugin read the page Apache Maven Dependency Plugin

Setting Up Your Project to Use the Build Lifecycle

Maven Plugins

mvn compiler:compile

Maven Plugin Configuration

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.8.1</version>
			<configuration>
                <verbose>true</verbose>
            </configuration>
        </plugin>
    </plugins>
</build>

Maven Javadoc Plugin

mvn javadoc:javadoc

Maven Surefire Plugin

mvn clean compiler:compile compiler:testCompile surefire:test

Or Simply Run the command which will run all the phases’ goals before it in the default lifecycle:

mvn test

Goals with Lifecycles

Site life Cycle

mvn clean install site
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.7.1</version>
</plugin>

Customizing Maven Default Life Cycle with the Maven Compiler Plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.1</version>
	<configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

Customizing Maven Default Life Cycle with the Maven Assembly Plugin

<plugin>
    <!-- NOTE: We don't need a groupId specification because the group is
	org.apache.maven.plugins ...which is assumed by default. -->
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>3.1.1</version>
	<configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

Introduction to Maven Dependencies