Creating Simple Java Project Using Apache Maven

SHARE & COMMENT :

Maven is a build and project management tool for java based application development. In previous article(“Apache Maven for Beginners“), we talked about installing and configuring maven on windows operating system. In this article, we shall see how to create a simple Maven project, check the created directories and build the project.

If you have any questions, please post it in the comments section. If you are interested in receiving the future articles on Java topics, please subscribe here.

Maven uses archetype plugins to create projects. To create a simple java application, we’ll use maven-archetype-quickstart plugin from command line. In the example below, We’ll create a maven based java application project in C:\MVNProject folder.

Open the command line window and type the below command, Maven creates a blank java project with all default options.

C:\MVNProject>mvn archetype:generate -DgroupId=net.javabeat -DartifactId=
SampleJavaProject -DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

Note: If you have just installed Maven, and if its your first run, it may take a while, because Maven downloads the most recent artifacts (plugin jars and other files) into your local repository (to your .m2 directory). You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete.

Maven commands

    • mvn : is the Maven command.
    • archetype:create : is called a Maven goal, analogous to an ANT target. It describes a unit of work to be completed in a build. The purpose of this goal is to quickly create(goal) a project from an archetype(plugin)
    • -Dname=value pairs : (DgroupId, DartifactId, DarchetypeArtifactId, DinteractiveMode) These are the arguments which are passed to the goal.They take the form of -D properties, similar to the system property options which you pass to the Java Virtual Machine via the command line.

The output on executing the above command is

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: net.javabeat
[INFO] Parameter: packageName, Value: net.javabeat
[INFO] Parameter: package, Value: net.javabeat
[INFO] Parameter: artifactId, Value: SampleJavaProject
[INFO] Parameter: basedir, Value: C:\MVNProject
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: C:\MVNProject\SampleJavaProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13:03.812s
[INFO] Finished at: Sun Feb 17 08:37:52 IST 2013
[INFO] Final Memory: 8M/24M
[INFO] ------------------------------------------------------------------------

Once the project is created, a directory structure is created as shown below. This directory structure adheres to the standard Maven directory layout.

dir_struct
The details of the basic directories is as follows:

    • SampleJavaProject: This directory contains src folder and pom.xml. Maven Archetype plugin creates this directory, value matching the artifactId SampleJavaProject
    • src/main/java: This directory contains java code files under the package structure (net/javabeat).
    • src/main/test: This directory contains test java code files under the package structure (net/javabeat).

We see that a default pom.xml has been created. POM file generated is:

<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>net.javabeat</groupId>
  <artifactId>SampleJavaProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SampleJavaProject</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>

Maven Archetype plugin also created a sample Java Source file(C:\MVNProject\SampleJavaProject\src\main\java\net\javabeat\App.java) and Java Test file(C:\MVNProject\SampleJavaProject\src\test\java\net\javabeat\AppTest.java).

App.java, is a single 13-line Java class, with a static main function that prints a simple “Hello World!” message.

package net.javabeat;

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

AppTest.java

package net.javabeat;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-) 
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

Build project

Once the project is created with the Maven Archetype plugin as seen above, we can build and package the application. To do so, run mvn install from the directory that contains the pom.xml(in our case it is C:\MVNProject\SampleJavaProject):

C:\MVNProject\SampleJavaProject>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SampleJavaProject 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ SampleJavaProject ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVNProject\SampleJavaProject\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ SampleJavaProject ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\MVNProject\SampleJavaProject\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ SampleJavaProject ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVNProject\SampleJavaProject\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ SampleJavaProject ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\MVNProject\SampleJavaProject\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.5:test (default-test) @ SampleJavaProject ---
[INFO] Surefire report directory: C:\MVNProject\SampleJavaProject\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running net.javabeat.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.453 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.1:jar (default-jar) @ SampleJavaProject ---
[INFO] Building jar: C:\MVNProject\SampleJavaProject\target\SampleJavaProject-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ SampleJavaProject ---
[INFO] Installing C:\MVNProject\SampleJavaProject\target\SampleJavaProject-1.0-SNAPSHOT.jar to C:\Documents and Settings\Manisha\.m2\repository\net\javabeat\SampleJavaProject\1.0-SNAPSHOT\SampleJavaProject-1.0-SNAPSHOT.jar
[INFO] Installing C:\MVNProject\SampleJavaProject\pom.xml to C:\Documents and Settings\Manisha\.m2\repository\net\javabeat\SampleJavaProject\1.0-SNAPSHOT\SampleJavaProject-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 37.375s
[INFO] Finished at: Sun Feb 17 12:17:10 IST 2013
[INFO] Final Memory: 11M/27M
[INFO] ------------------------------------------------------------------------

Executing the mvn install, we have just created, compiled, tested, packaged and installed the simple java project by Maven. We can also clean the target directories by using the clean command as mvn clean install.

Execute the program from command line as follows:

C:\MVNProject\SampleJavaProject\target\classes>java net.javabeat.App
Hello World!

Summary

In this article we saw how to create a simple Java project using Maven archetype plugins. We checked the directory structure, the generated source code.We then did learn about how to build this simple Java project using Maven command.In the next article we shall create a web application project using Maven.

If you have any questions, please post it in the comments section. If you are interested in receiving the future articles on Java topics, please subscribe here.

Comments

comments

About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Trackbacks

  1. [...] Creating Simple Java Project Using Apache Maven [...]

  2. […] the previous post we saw how to create simple Java project using Maven. We will create a simple web application. The […]

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better