Building Java Projects with Maven
Create the directory structure
In a project directory of your choosing, create the following subdirectory
structure;
└── src
└── main
└── java
└── hello
Within the src/main/java/hello directory, you can create any Java classes
you want. To maintain consistency with the rest of this guide, create these
two classes: [Link] and [Link] .
src/main/java/hello/[Link]
package hello;
public class HelloWorld {
public static void main(String[] args) {
Greeter greeter = new Greeter();
[Link]([Link]());
}
}
src/main/java/hello/[Link]
package hello;
public class Greeter {
public String sayHello() {
return "Hello world!";
}
}
Download XML Notepad
Define a simple Maven build
Maven projects are defined with an XML file named [Link]. Among other
things, this file gives the project’s name, version, and dependencies that it
has on external libraries.
Create a file named [Link] at the root of the project (i.e. put it next to
the src folder) and give it the following contents:
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<properties>
<[Link]>1.8</[Link]>
<[Link]>1.8</[Link]>
</properties>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="[Link]
ourceTransformer">
<mainClass>[Link]</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Build Java code
mvn compile
mvn package
To execute the JAR file run:
java -jar target/[Link]
mvn install
Declare Dependencies
First, change [Link] to look like this:
src/main/java/hello/[Link]
package hello;
import [Link];
public class HelloWorld {
public static void main(String[] args) {
LocalTime currentTime = new LocalTime();
[Link]("The current local time is: " +
currentTime);
Greeter greeter = new Greeter();
[Link]([Link]());
}
}
Now run mvn compile command again. Now build will fail because you’ve not declared
Joda Time as a compile dependency in the build. You can fix that by adding
the following lines to [Link] (within the <project> element):
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
Now if you run mvn compile or mvn package , Maven should resolve the Joda
Time dependency from the Maven Central repository and the build will be
successful.
Write a Test
First add JUnit as a dependency to your [Link], in the test scope:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Then create a test case like this, create new file and folders as given below:
src/test/java/hello/[Link]
package hello;
import static [Link];
import static [Link].*;
import [Link];
public class GreeterTest {
private Greeter greeter = new Greeter();
@Test
public void greeterSaysHello() {
assertThat([Link](), containsString("Hello"));
}
Now execute again this command
mvn test
next time while running again, give this command to delete target folder created.
mvn clean test