Creating simple java project with maven

Maven Java project can be created in 2 ways:

  1. Through command line
  2. Through an IDE like eclipse, net beans, IDEA etc.

Please check the blog for setting up maven Installing maven in linux

This tutorial will explain how to create a java project using maven through command line.

Creating a project

You will need a directory to put you project contents into. So please create directory and go to there, and run the following command to create a java project, using simple archetype.

$ cd hello-world-app

$ mvn archetype:generate -DartifactId=helloworld-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

The project is created in the name of ‘artifactId’ you have passed.      You can see the project created message as :

[INFO] -----------------------------------------------------------
[INFO] -----------------------------------------------------------
[INFO] Total time: 16.824 s
[INFO] Finished at: 2014-10-30T15:02:38+01:00
[INFO] Final Memory: 18M/162M

What did I just do?

You executed the Maven goal archetype:generate, and passed in various parameters to that goal. The prefix archetype is the plugin that contains the goal. If you are familiar with Ant, you may conceive of this as similar to a task. This goal created a simple project based upon an archetype.

The folder structure will be created, that is the standard directory structure of any maven project:


Importing this project to IDE

To work further on this project, we will import this into any ide. Eclipse is used here to work on it.

So making this project ready to be imported, we will run the following command:

$ mvn eclipse:eclipse

Now this project can be imported to any IDE.

Eclipse IDE it can be done as follows:

Open-> File-> Import -> Maven -> Import existing maven projects

then import. Click Finish.


Please look at the pom.xml. It is simple and it has only one dependency junit. Because our hello world app requires only junit external dependency to do the test.

<project xmlns="" xmlns:xsi=""

If you look at the App.Java, a simple add() method is added to perform addition of 2 numbers.


* Hello world!
public class App
public static void main( String[] args )
System.out.println( "Hello World!" );

public int add(int param1, int param2){
return param1 + param2;

So we write unit test case to cover that as follows in class.

* Unit test for simple App.
public class AppTest
extends TestCase
* Rigourous Test 🙂
public void testAdd()
assertEquals( 3, new App().add(1, 2) );

Build the project

$ mvn package

The command line will print out various actions, and end with the following:

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.616 s
[INFO] Finished at: 2016-10-30T16:05:46+01:00
[INFO] Final Memory: 20M/146M

Unlike the first command executed (archetype:generate) you may notice the second is simply a single word – package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined.

For example, if we execute the compile phase, the phases that actually get executed are:


You may test the newly compiled and packaged JAR with the following command:

$ java -cp target/helloworld-app-1.0-SNAPSHOT.jar

Which will print

 Hello World!

Leave a Reply

Your email address will not be published. Required fields are marked *