Maven is a powerful project management tool that streamlines the build process for Java projects. It manages project builds, dependencies, and documentation from a central piece of information. If you’re new to Maven, this guide will help you set up your first Java project using Maven. Follow these steps to get started.
Prerequisites
Before you begin, ensure you have the following installed on your system:
- JDK (Java Development Kit) 1.8 or above
- Apache Maven 3.6.3 or newer
You can verify the installations by running java -version
and mvn -version
in your terminal or command prompt.
Step 1: Generate Project Structure
The first step is to generate the structure of your Java project. Maven comes with a feature called archetypes, which are project templates. The maven-archetype-quickstart is a basic template for creating a simple Java application.
Open your terminal or command prompt and run the following command:
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-java-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
Replace com.example with your domain and my-java-app with the name of your project.
This command creates a directory named my-java-app containing the project structure and default files:
- src/main/java for your Java source files.
- src/test/java for your test files.
- A pom.xml file, which is the Project Object Model (POM) file containing project and configuration details.
Step 2: Explore the Project Structure
Navigate to your project directory:
cd my-java-app
Take a moment to explore the generated files and directories. The pom.xml file is especially important, as it defines the project’s dependencies, plugins, and other configuration details.
Step 3: Editing the POM File
Open the pom.xml file in your favorite text editor or IDE. This file contains essential information about your project, including dependencies.
For a simple Java project, the default configuration should sufficient. However, as your project grows, you may need to add dependencies. The default pom.xml will look like below:
<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.example</groupId>
<artifactId>my-java-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-java-app</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>
Step 4: Building the Project
To compile your project, run the following command in the project directory:
mvn compile
This command compiles your source code and places the generated class files in the target/classes directory.
Step 5: Running Tests
If you have written tests, you can run them using:
mvn test
Maven will execute tests located in the src/test/java directory.
Step 6: Packaging the Project
To package your project into a JAR (Java ARchive) file, use:
mvn package
This command compiles your code, executes tests, and packages the compiled code into a JAR file located in the target directory.
Step 7: Running Your Application
To run your application, you can use the java command to execute the JAR file:
java -cp target/my-java-a-p1.0-SNAPSHOT.jar com.example.App
Replace com.example.App with the fully qualified name of your main class.
Conclusion
Congratulations! You have successfully created and managed your first Java project using Maven. This guide covered generating a project structure, exploring the project, editing the POM file, building the project, running tests, packaging the project, and running your application. As you become more familiar with Maven, you will appreciate its power and flexibility in managing Java projects.