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+ (if not check out How to install Java JDK).

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

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.favtuts.simpleproject -DartifactId=maven-cli-simple-project -Dpackage=com.favtuts.simpleproject -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Note that whole command should be single line. After build success, we will see below output in command line console.

[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: basedir, Value: /home/tuts.heomi.net/github/java-maven-tutorials
[INFO] Parameter: package, Value: com.favtuts.simpleproject
[INFO] Parameter: groupId, Value: com.favtuts.simpleproject
[INFO] Parameter: artifactId, Value: maven-cli-simple-project
[INFO] Parameter: packageName, Value: com.favtuts.simpleproject
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/tuts.heomi.net/github/java-maven-tutorials/maven-cli-simple-project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  46.972 s
[INFO] Finished at: 2022-06-24T09:33:25+07:00
[INFO] ------------------------------------------------------------------------

Note that our project name: maven-cli-simple-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:

$ tree maven-cli-simple-project

maven-cli-simple-project
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── favtuts
    │               └── simpleproject
    │                   └── App.java
    └── test
        └── java
            └── com
                └── favtuts
                    └── simpleproject
                        └── AppTest.java

11 directories, 3 files

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 maven-cli-simple-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.favtuts.simpleproject.App,which prints ‘Hello World!’

package com.favtuts.simpleproject;

/**
 * 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.favtuts.simpleproject</groupId>
  <artifactId>maven-cli-simple-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven-cli-simple-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 maven-cli-simple-project
> mvn install

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

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.favtuts.simpleproject.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec

Results :

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

....

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.606 s
[INFO] Finished at: 2022-06-24T09:48:08+07:00
[INFO] ------------------------------------------------------------------------

You’ve just created, cleaned, compiled, tested, packaged, and installed the simplest possible Maven project.

Note: You may encounter the error : ERROR Source or Target option 1.5 is no longer supported. Use 1.6 or later. To fix it, Now, let’s configure our maven project to use the maven-compiler-plugin to compile the source code using Java 8.

   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>

The full pom.xml

<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.favtuts.simpleproject</groupId>
  <artifactId>maven-cli-simple-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven-cli-simple-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>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

6. Run Packaged JAR

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

java -cp target/maven-cli-simple-project-1.0-SNAPSHOT.jar com.favtuts.simpleproject.App

Output:

Hello World!

Conclusion

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

Download Source Code

$ git clone https://github.com/favtuts/java-maven-tutorials.git
$ cd maven-cli-simple-project
$ mvn install
$ java -cp target/maven-cli-simple-project-1.0-SNAPSHOT.jar com.favtuts.simpleproject.App

References

  1. Maven – How to create a Java project
  2. Create a Simple Maven Web Application using Command Line
  3. How to Create a Simple Maven Project in Eclipse

Leave a Reply

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