In this guide, we'll walk through the steps to create a simple Maven project using the command line. This tutorial assumes that you have already installed Maven and JDK 21 on your machine.
Before we begin, let's verify that Maven and Java are correctly installed on your system.
Open your command line terminal and type:
mvn -v
You should see an output similar to:
Apache Maven 3.8.5 (b89d5959fcde851dcb1c8946e2987cb6db844df6)
Maven home: /usr/local/apache-maven-3.8.5
Java version: 21, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-21
Type the following command to check your Java version:
java -version
You should see an output similar to:
java version "21" 2023-09-21 LTS
Java(TM) SE Runtime Environment (build 21+36-LTS-1491)
Java HotSpot(TM) 64-Bit Server VM (build 21+36-LTS-1491, mixed mode, sharing)
We'll use Maven's archetype:generate
goal to create a simple project. This goal generates a
project structure based on a predefined archetype.
Run the following command in your terminal:
mvn archetype:generate -DgroupId=com.companyname.projectname -DartifactId=simple-maven-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command creates a new Maven project with the following parameters:
groupId:
The identifier for your project’s group. Typically follows the reverse domain name
convention.artifactId:
The name of your project.archetypeArtifactId:
Specifies the archetype to use.
maven-archetype-quickstart
is a simple archetype suitable for beginners.
interactiveMode:
If set to false, it runs in batch mode without prompting for user input.
After executing the command, you should see an output similar to:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.345 s
[INFO] Finished at: 2023-07-09T14:50:30+05:30
[INFO] ------------------------------------------------------------------------
Change your directory to the newly created project directory:
cd simple-maven-project
The generated project structure looks like this:
simple-maven-project
│ pom.xml
└───src
├───main
│ └───java
│ └───com
│ └───companyname
│ └───projectname
│ └───App.java
└───test
└───java
└───com
└───companyname
└───projectname
└───AppTest.java
pom.xml:
The Project Object Model file where you define your project configuration and
dependencies.src/main/java:
Contains the source code of your application.src/test/java:
Contains the test code for your application.Open the pom.xml
file and update it with the following content to ensure we use the latest
versions of Java and JUnit:
<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
<groupId>com.companyname.projectname
<artifactId>simple-maven-project
<version>1.0-SNAPSHOT
<properties>
<maven.compiler.source>21
<maven.compiler.target>21
<properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter
<artifactId>junit-jupiter-api
<version>5.9.3
<scope>test
<dependency>
<dependency>
<groupId>org.junit.jupiter
<artifactId>junit-jupiter-engine
<version>5.9.3
<scope>test
<dependency>
<dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins
<artifactId>maven-compiler-plugin
<version>3.10.1
<configuration>
<source>21
<target>21
<configuration>
<plugin>
<plugins>
<build>
<project>
Run the following command to build the project:
mvn clean install
This command will compile your project and run any tests. The output should indicate a successful build:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.companyname.projectname.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.345 s
[INFO] Finished at: 2023-07-09T14:52:30+05:30
[INFO] ------------------------------------------------------------------------
The "Hello World!" message comes from the App.java
file created by the Maven archetype. Open the
src/main/java/com/companyname/projectname/App.java
file, and you will see the following code:
package com.companyname.projectname;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
To run the application, first package it into a JAR file using Maven:
mvn package
This command will create a JAR file in the target
directory.
Navigate to the target
directory and run the JAR file using the following command:
java -jar target/simple-maven-project-1.0-SNAPSHOT.jar
You should see the output:
Hello World!
In this tutorial, we created a simple Maven project using the command line. We used Maven's
archetype:generate
goal to generate a project structure, updated the pom.xml
to
use the latest versions of
Java and JUnit, built the project, and ran the packaged JAR. Maven simplifies project management and
dependency management, making it a valuable tool for Java developers.