Getting Started with Maven and Java

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.

1. Installation

Installing Java

Before installing Maven, you need to have Java installed on your system. Follow these steps to install Java:

  1. Download JDK: Download the latest JDK (Java Development Kit) from the official Oracle website or OpenJDK website.

  2. Install JDK: Follow the installation instructions provided on the download page. Make sure to set the JAVA_HOME environment variable to the JDK installation directory.

  3. Verify Installation: Open a terminal or command prompt and type the following command to verify the installation:

    java -version
    

    You should see the Java version installed on your system.

Installing Maven

Follow these steps to install Maven:

  1. Download Maven: Download the latest version of Maven from the official Apache Maven website.

  2. Install Maven: Extract the downloaded archive to a directory of your choice. Set the M2_HOME and MAVEN_HOME environment variables to the Maven installation directory and add the bin directory to your PATH.

  3. Verify Installation: Open a terminal or command prompt and type the following command to verify the installation:

    mvn -version
    

    You should see the Maven version installed on your system.

2. Setting Up a Simple Java Project

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:

  • src/main/java: Contains the Java source files.
  • src/test/java: Contains the test Java source files.
  • pom.xml: The Project Object Model file that contains the project configuration.

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.

Project Directory Structure

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

3. Running the Project

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>

Conclusion

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.

Comments