Build Simple Java Projects with Maven

  1. Prerequisite
  2. Build Java project with Maven
  3. Add Dependencies
  4. Add Junit Test

Prerequisite

  • IDE (Integrated development environment)
  • JDK 8 or later
  • Maven

Build Java project with Maven

1. Create Directories *nix systems:

mkdir -p src/main/java/hello

└── src
    └── main
        └── java
            └── hello

2. Create Java Class

HelloWorld.java Greeter.java

package hello;

public class HelloWorld {
  public static void main(String[] args) {
    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}
  • src/main/java/hello/HelloWorld.java
package hello;

public class Greeter {
  public String sayHello() {
    return "Hello world!";
  }
}
  • src/main/java/hello/Greeter.java

3. Add pom.xml for Maven Build

pom.xml file should be same level of src

└── pom.xml
└── src
    └── main
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

This is simple pom.xml file

  • <modelVersion>. POM Model version (always 4.0.0).
  • <groupId>. Group and Organization name.
  • <artifactId>. jar or war file name
  • <version>. Project version
  • <packaging>. choose package as jar or war

4. Build Java code

  1. compile
$ mvn compile 

2. package

$ mvn package

The purpose of package is java file compile and test. jar or war will be created in target directory.

3. execute

$ java -jar target/gs-maven-0.1.0.jar

Hello world!

Declare Dependencies

Add native Java Libraries

src/main/java/hello/HelloWorld.java

package hello;

import org.joda.time.LocalTime;

public class HelloWorld {
  public static void main(String[] args) {
    LocalTime currentTime = new LocalTime();
    System.out.println("The current local time is: " + currentTime);
    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}

if you run $ mvn compile directly, you will get Error message

Add dependencies in pom.xml

<dependencies>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
			<version>2.9.2</version>
		</dependency>
</dependencies>
  • <groupId> – The group or organization that the dependency belongs to.
  • <artifactId> – The library that is required.
  • <version> – The specific version of the library that is required.

repackage and execute the code.

$ mvn package
$ java -jar target/gs-maven-0.1.0.jar

The current local time is: 12:09:18.910
Hello world!
  • You can see the time

Add JUnit dependency

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>

Write Test

$ mkdir -p test/java/hello

src/test/java/hello/GreeterTest.java

package hello;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;

import org.junit.Test;

public class GreeterTest {

  private Greeter greeter = new Greeter();

  @Test
  public void greeterSaysHello() {
    assertThat(greeter.sayHello(), containsString("Hello"));
  }
}

Basic configuration: surefire plugin compiles and executes all test files in src/test/java.

$ mvn test

Test Result

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running hello.GreeterTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.134 s
[INFO] Finished at: 2020-09-04T12:29:05+09:00
[INFO] ------------------------------------------------------------------------

More detail: https://spring.io/guides/gs/maven/#scratch

Leave a Reply

Your email address will not be published.

ANOTE.DEV