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.
Verify Maven Installation
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
Verify Java Installation
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
Key Files and Directories
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</modelVersion> <groupId>com.companyname.projectname</groupId> <artifactId>simple-maven-project</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.9.3</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <source>21</source> <target>21</target> </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!" ); } }
Running the Packaged JAR
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.