Centurion, South Africa info@code2bits.com On Request

How to Create a Simple Java Project using Maven

This post contains a step-by-step guide on how to create a simple java application making use of maven. The Archetype Plugin is used to create a simple java project from an existing template / archetype. The guide also illustrates how to build and then run the application using maven commands.

Getting Started

The following list defines the technologies and libraries I used to implement the sample code:

Multiple Posts

Apache Maven is a software project management and comprehension tool. It is one of the basic tools every Java Developer should know. This post forms part of a series of articles about maven and step-by-step guides on how to use maven to build your java applications.

Code Example

The code example can be downloaded from Github from the button below. After you have downloaded the code onto your machine, you can either work through the guide step-by-step to understand the example or jump to the “Compile & Run The Application” section to get the example up and running.

Download Code

Step 1: Use the Maven Archetype Plugin

The archetype plugin allows the user to create a maven project from an existing template called an archetype. The generate goal generates a new project in a directory corresponding to the artifactId. A list of the different archetypes can be found here.

$ mvn archetype:generate -DgroupId=com.code2bits.maven.example -DartifactId=simplejavaproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Step 2: View The Project Object Model (POM)

The Project Object Model (POM) is an XML file that contains information about the project and configuration details. The pom.xml file is used by Maven to build the 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>com.code2bits.maven.example</groupId>
    <artifactId>simplejavaproject</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>simplejavaproject</name>
    <url>http://maven.apache.org</url>

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

Depending on the Java version on your machine, you might want to compile the project to a different version than the one installed. The maven compiler plugin can be configured to use a different java version. You can add the following properties to the pom.xml file.

<project>
    [...]
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    [...]
</project>

Step 3: Edit the default App class

The default App class that is generated only prints the famous words Hello World!. To make this project slightly more exciting, I added code that will print Hello World if no arguments are passed to the application. If you pass in arguments from the command line, it will print a Hello message with the arguments appended.

public class App {
    public static void main( String[] args ) {

        if (args.length == 0) {
            System.out.println("Hello World!");
        } else {
            System.out.print("Hello ");
            for (String arg : args) {
                System.out.print(arg + "! \n");
            }
        }
    }
}

Step 4: Compile and Run the Application

Use the following command to compile the Java application making use of maven.

Build Application

$ mvn clean install

After you have successfully built the Java application, the compiled artifact can be found in the target directory. There are several ways to run the Java Application.

Run Application

$ java -cp target/simplejavaproject-1.0-SNAPSHOT.jar com.code2bits.maven.example.App 

Run Application With Single Argument

$ java -cp target/simplejavaproject-1.0-SNAPSHOT.jar com.code2bits.maven.example.App Code2Bits

Example

The exec maven plugin provides 2 goals to help execute system and Java programs. The exec:java goal executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.

Run Application using Maven Plugin

$ mvn exec:java -Dexec.mainClass="com.code2bits.maven.example.App"  

Run Application using Maven Plugin With Single Argument

$ mvn exec:java -Dexec.mainClass="com.code2bits.maven.example.App" -Dexec.args="Code2Bits"  

Summary

Congratulations! You have successfully created a simple Java Application using maven. You have also used maven to compile the application and also to run the application. Follow me on any of the different social media platforms and feel free to leave comments.