Create a Simple Maven Project using Command Line

In this tutorial, we learn how to create a simple standalone Java maven project using the Maven Archetype plugin.

Note that we are creating a simple Maven project from the command line using Maven.

Note that this is not a web application. If you want to create a Maven web application then check out Create a Simple Maven Web Application using Command-Line.

You can also create Maven projects in Eclipse IDE. Check out below articles:

Pre-requisites 

- Make sure that you have installed Java 8+.

- Make sure that you have installed Maven and configured an environment path ( if not check out How to install Maven on Windows 10)

1. Creating a Maven Project 

Let's start a new Maven project, we use the Maven Archetype plugin from the command line.

You will need somewhere for your project to reside, create a directory somewhere and start a shell in that directory. On your command line, execute the following Maven goal:
mvn archetype:generate -DgroupId=com.companyname.projectname  
-DartifactId=simple-maven-project  
-Dpackage=com.companyname.projectname  
-Dversion=1.0-SNAPSHOT
Note that whole command should be single line. After build success, we will see below output in command line console.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:23 min
[INFO] Finished at: 2018-06-20T10:50:40+05:30
[INFO] ------------------------------------------------------------------------
Note that our project name: simple-maven-project 

2. Maven Standard Directory Layout 

Once we’ve generated a project, take a look at the directory structure Maven created under the simple directory:
simple-maven-project
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
This generated directory adheres to the Maven Standard Directory Layout. let's understand these few basic directories:
  1. The Maven Archetype plugin creates a directory simple-maven-project that matches the artifactId. This is known as the project’s base directory.
  2. Every Maven project has what is known as a Project Object Model (POM) in a file named pom.xml. This file describes the project, configures plugins, and declares dependencies.
  3. Our project’s source code and resources are placed under src/main. In the case of our simple Java project this will consist of a few Java classes and some properties file. In web project, this could be the document root of a web application or configuration files for an application server. In a Java project, Java classes are placed in src/main/java and classpath resources are placed in src/main/resources.
  4. Our project’s test cases are located in src/test. Under this directory, Java classes such as JUnit or TestNG tests are placed in src/test/java, and classpath resources for tests are located in src/test/resources.

3. App.java

The Maven Archetype plugin generated a single class com.companyname.projectname.App,which prints 'Hello World!'
package com.companyname.projectname;

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

4. Simple Project Object Model(pom.xml)

Every Maven project has what is known as a Project Object Model (POM) in a file named pom.xml. This file describes the project metadata, configures plugins, and declares dependencies.
<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.companyname.projectname</groupId>
  <artifactId>simple-maven-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-maven-project</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>

5. Maven Build the Project

Go to the project directory and run the following commands to build this Maven project:
> cd simple-maven-project
> mvn install
The command line will print out various actions, and end with the following:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.640 s
[INFO] Finished at: 2018-06-20T11:53:12+05:30
[INFO] ------------------------------------------------------------------------
You’ve just created, cleaned, compiled, tested, packaged, and installed the simplest possible Maven project.

6. Run Packaged JAR

You may test the newly compiled and packaged JAR with the following command:
java -cp target/simple-maven-project-1.0-SNAPSHOT.jar com.companyname.projectname.App
Output:
Hello World!

Conclusion

In this tutorial, we have seen how to create a simple standalone Maven project using the command line.

The source code for this simple maven web project is available on GithubGithub Repository : Simple Maven Application

Comments