Software DevelopmentLearn How to Create a Java Project using Maven Archetype

Learn How to Create a Java Project using Maven Archetype

In the last article, we discussed about the use of various Maven Plug-ins. In this article, we are going to discuss and explore the archetype plugins which is used to create project structures. Here, we are going to create a simple JAVA project by using maven-archetype-quickstart plugin. In the following example, we will be creating a maven based JAVA project under C:\MVN_JAVA folder using this folder as the project’s workspace.

Step 1: – Open a window’s command line and change directory to C:\MVN_JAVA

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\MVN_JAVA

C:\MVN_JAVA>

Step 2: – Execute the following Maven command to create a project.

mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 -DgroupId=com.eduonix -DartifactId=project -Dversion=1.0-SNAPSHOT -Dpackage=com.eduonix.project

After executing above command on the console, you can observe that Maven has started processing the instructions and has created the complete JAVA application project structure with Maven build as shown below.

C:\MVN_JAVA>mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 -DgroupId=com.eduonix -DartifactId=project -Dversion=1.0-SNAPSHOT -Dpackage=com.eduonix.project
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] Archetype repository not defined. Using the one from [org.apache.maven.archetypes:maven-archetype-quickstart:1.1] found in catalog remote
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\MVN_JAVA
[INFO] Parameter: package, Value: com.eduonix.project
[INFO] Parameter: groupId, Value: com.eduonix
[INFO] Parameter: artifactId, Value: project
[INFO] Parameter: packageName, Value: com.eduonix.project
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\MVN_JAVA\project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.040 s
[INFO] Finished at: 2017-09-23T17:59:13-04:00
[INFO] Final Memory: 15M/147M
[INFO] ------------------------------------------------------------------------

C:\MVN_JAVA>

Step 3: – After above Maven build is successful, you can navigate to C:\MVN_JAVA directory where you will observe that the plugin has created a complete JAVA project structure with the Maven build, named as “project”. You will observe a standard directory layout as shown below.
MVN_Java_DirectoryDescription of Folders created by plugin

S No

Folder name

1

project

It is a folder created by the plugin and contains src folder and pom.xml file.

2

src/main/java

It is folder created by the plugin and contains java code files under the package structure (i.e. com/eduonix/project). Here, we had provide plugin option as -Dpackage=com.eduonix.project.

3

src/main/test

It is folder created by plugin that contains test java code files under the package structure (com/eduonix/project). Here, we had provide plugin option as -Dpackage=com.eduonix.project.

4.

com.eduonix.project

It is created as a result of plugin option as

-Dpackage=com.eduonix.project.

Not only these folders are created but, maven-archetype-quickstart plugin has also created the following files as well.
1. A sample Java Source file: – It is the source JAVA file which is named as App.java that contains a main method that is capable of printing the “Hello World!” string on console when executed as shown below.
Path: – C:\MVN_JAVA\project\src\main\java\com\eduonix\project

package com.eduonix.project;

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

2. A sample Java Source Junit Test file: – It is the source JAVA Junit Test file which has name as AppTest.java that contains the default Test case, suite, etc. as shown below.
Path: – C:\MVN_JAVA\project\src\test\java\com\eduonix\project

package com.eduonix.project;

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 );
    }
}

3. The pom.xml file: – The following are the default contents for the pom.xml file which is created by the archetype plugin

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.eduonix</groupId>
  <artifactId>project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

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

Source code for Maven JAVA project structure using the Maven build procedure

Conclusion: –
In this article, we discussed about the use of maven-archetype-quickstart plugin along with a suitable example to create JAVA project structure using the Maven build procedure.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -