Apache Maven is a powerful build automation tool used primarily for Java projects. It simplifies the process of building and managing any Java-based project. In this guide, we will cover installing Maven, setting up a simple Java project, and running the project using Maven.
Installing Java
Before installing Maven, you need to have Java installed on your system. Follow these steps to install Java:
java -version
You should see the Java version installed on your system.
Installing Maven
Follow these steps to install Maven:
mvn -version
You should see the Maven version installed on your system.
Let's set up a simple Java project using Maven. We'll create a basic "Hello World" application.
Step 1: Create Project Directory
Open a terminal or command prompt and create a new directory for your project:
mkdir my-maven-project cd my-maven-project
Step 2: Generate Maven Project
Use the Maven Archetype plugin to generate a new project:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-maven-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command generates a basic Maven project structure. The important files and directories are:
Step 3: Add "Hello World" Code
Navigate to the src/main/java/com/example directory and open the App.java file. Replace its content with the following code:
package com.example; public class App { public static void main(String[] args) { System.out.println("Hello World!"); } }
Step 4: Update pom.xml
Ensure your pom.xml contains the correct configuration. Here's an example configuration for the pom.xml with the latest 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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-maven-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>my-maven-project</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <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</artifactId> <version>5.10.0</version> <scope>test</scope> </dependency> </dependencies> </project>
Step 5: Build the Project
Run the following command to build the project:
mvn clean install
This command compiles the Java code, runs the tests, and packages the application into a JAR file.
After running the mvn archetype:generate command, your project directory structure will look like this:
my-maven-project ├── pom.xml ├── src │ ├── main │ │ └── java │ │ └── com │ │ └── example │ │ └── App.java │ └── test │ └── java │ └── com │ └── example │ └── AppTest.java
Step 6: Run the Application
After building the project, you can run the application using the following command:
java -cp target/my-maven-project-1.0-SNAPSHOT.jar com.example.App
You should see the output:
Hello World!
Step 7: Running the Application (Run Packaged JAR)
Alternatively, you can run the packaged JAR file directly:
java -jar target/my-maven-project-1.0-SNAPSHOT.jar
Ensure that the App class is correctly specified in the pom.xml file under the build section to create an executable JAR:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>com.example.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
In this guide, we covered the installation of Maven and Java, setting up a simple Java project using Maven, and running the project. Maven simplifies project setup and management, making it easier to handle dependencies, build processes, and project structure. By following these steps, you can quickly get started with Maven and Java to build and run your Java applications.