An Open Source Organization!

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.

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

https://tvd12.com/wp-content/uploads/jdk8_download.png

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

https://tvd12.com/wp-content/uploads/maven-eclipse-new.png

3.2 Click next and we have

https://tvd12.com/wp-content/uploads/maven-eclipse-next1.png

3.3 Click Next and we have

https://tvd12.com/wp-content/uploads/maven-eclipse-next2.png

This step allow you select an archetype you want, please read this guide to know more about Maven Archetype
We will use default choice
3.4 Click next and we have

https://tvd12.com/wp-content/uploads/maven-eclipse-next3.png

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

https://tvd12.com/wp-content/uploads/mven-hello-world-project.png

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

https://tvd12.com/wp-content/uploads/maven-intellij-dialog-1.png

Select Maven on left side, tick Create from archetype, find and select org.apache.maven.archetypes:maven-archetype-quickstart
This step allow you select an archetype you want, please read this guide to know more about Maven Archetype

https://tvd12.com/wp-content/uploads/maven-intellij-dialog-2.png

4.2 Click next and we have

https://tvd12.com/wp-content/uploads/maven-intellij-dialog-3.png

Fill information follow:
GroupId: com.example
ArtifactId: hello-world
4.3 Click Next, Next (double Next) and we have

https://tvd12.com/wp-content/uploads/maven-intellij-dialog-4.png

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

https://tvd12.com/wp-content/uploads/maven-intellij-project.png

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.target>

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.0.5</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:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<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:

https://tvd12.com/wp-content/uploads/project-finnaly.png

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

Share: