Amsterdam, Netherlands 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="" xmlns:xsi="" xsi:schemaLocation="">




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.


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


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"  


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.