Create Maven Project

1. Introduction

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. Maven's features include:
  1. Simple project setup that follows best practices.
  2. Consistent usage across all projects.
  3. Dependency management including automatic updating.
  4. A large and growing repository of libraries.
  5. Extensible, with the ability to easily write plugins in Java or scripting languages.
  6. Instant access to new features with little or no extra configuration.
  7. Model-based builds − Maven is able to build any number of projects into predefined output types such as jar, war, metadata.
  8. Coherent site of project information − Using the same metadata as per the build process, maven is able to generate a website and a PDF including complete documentation.
  9. Release management and distribution publication − Without additional configuration, maven will integrate with your source control system such as CVS and manages the release of a project.
  10. Backward Compatibility − You can easily port the multiple modules of a project into Maven 3 from older versions of Maven. It can support the older versions also.
  11. Automatic parent versioning − No need to specify the parent in the sub module for maintenance.
  12. Parallel builds − It analyzes the project dependency graph and enables you to build schedule modules in parallel. Using this, you can achieve the performance improvements
  13. of 20-50%.
  14. Better Error and Integrity Reporting − Maven improved error reporting, and it provides you with a link to the Maven wiki page where you will get full description of the error.
  15. Execute shell commands from Maven's pom.xml

2.Prerequisite​

We will use Java 8, so if you haven’t installed it yet, please download and install it. Download jdk 8 at here
You need select a version for your OS, example me it’s Windows x64. Also, you need setup JAVA_HOME path variable

3.Create a maven project on Eclipse​

If don’t have Eclipse please download and install Eclipse for JavaEE version
3.1 Open Eclipse and select File->New->Other, type text filter to “maven” and select Maven Project
3.2 Click next and we have
3.3 Click Next and we have
This step allow you select an archetype you want, if you don't want we will use default choice
3.4 Click next and we have
Fill information follow:
Group Id: com.example
Artifact Id: hello-world
Package: com.example.hello_world
3.5 Click finish and we have a project in eclipse Package Explore

4.Create a maven project on Intellij​

If you don’t have Intellij IDE, download here and install it
4.1 Open Intellij IDE and click New->Project, we have the dialog
Select Maven on left side, check to Create from archetype, find and select an archetype that you want, in this case we will select org.apache.maven.archetypes:maven-archetype-quickstart Please read this guide to know how to add an archetype
4.2 Click next and we have
Fill information follow:
GroupId: com.example
ArtifactId: hello-world
4.3 Click Next, Next (double Next) and we have
You can change Project name and Project location to fit your purpose
4.4 Click Next (select New Window if the New Project popup open) and we have

5.Config maven project​

5.1 Config pom.xml
Open pom.xml file, we currently have:
<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>com.example</groupId>
  <artifactId>hello-world</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hello-world</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
By default maven use Java 5, to use Java 8 we need setup for it. In properties tag add 2 properties:
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.tar
In plugins tag add the plugin:
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.8.1</version>
   <configuration>
      <source>${maven.compiler.source}</source>
      <target>${maven.compiler.target}</target>
   </configuration>
</plugin>
And now we have the pom.xml:
<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>com.example</groupId>
    <artifactId>hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hello-world</name>
    <url>http://maven.apache.org</url>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
On Eclipse, right click to project and select Maven->Update Project or Alt+F5, On Intellij right click to project and select Maven->Re-import and now we have the hello-world project with Java 8
5.2 Config unitest dependency.
The junit version 3.8.1 is too old, and we want use TestNG and jacoco to check coding coverage, we will replace junit dependency with test-util dependency
<dependency>
        <groupId>com.tvd12</groupId>
        <artifactId>test-util</artifactId>
        <version>1.1.0</version>
</dependency>
Add jacoco dependency:
<plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
        <executions>
            <execution>
                <id>prepare-agent</id>
                <goals>
                    <goal>prepare-agent</goal>
                    </goals>
                </execution>
            <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
            </execution>
        </executions>
</plugin>
And now we have pom.xml
<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>com.example</groupId>
    <artifactId>hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hello-world</name>
    <url>http://maven.apache.org</url>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.tvd12</groupId>
            <artifactId>test-util</artifactId>
            <version>1.0.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.4</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.2</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>src/test/resources/AllTests.tng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
We get errors at AppTest.java too, don’t worry, change it’s content to:
package com.example.hello_world;

import org.testng.annotations.Test;

public class AppTest {
    
    @Test
    public void test() {
        System.out.println("I'm a test");
    }
    
}
To build project we need create AllTest.tng.xml in src/test/resources folder with content:


<suite name="AllTests">
    <test name="All">
        <packages>
            <package name="com.example.hello_world"></package>
        </packages>
    </test>
</suite>
Finnaly, we have hello-world project with structure:
Build project with goals: clean install. Cheer! We done

6.Conclusion​

Use maven to manage project’s build is a good choice, we done need care about library import, and project export, only need add dependencies and build, everything is simple