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 -DgroupId=com.mksiva.app -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 :

...eclipsespace/hello-world-app/helloworld-app
[INFO] -----------------------------------------------------------
[INFO] BUILD SUCCESS
[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:

selection_003

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.

selection_004selection_005

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="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.mksiva.app</groupId>
<artifactId>helloworld-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloworld-app</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>

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

package com.mksiva.app;

/**
* 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 AppTest.java 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:

app/helloworld-app/target/helloworld-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[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:

validate
generate-sources
process-sources
generate-resources
process-resources
compile

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

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

Which will print

 Hello World!

Leave a Reply

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