In this tutorial, you’ll learn how to create, build, and test a simple RollDice Java application using Maven on Ubuntu 24.04 LTS. We’ll also cover the installation of Java 17 and configuring Maven to manage the project.

Step 1: Install Java 17 on Ubuntu 24.04 LT

To build and run Java projects, we need Java Development Kit (JDK) 17. Follow these steps:

  1. Update System Packages:
sudo apt update
sudo apt upgrade -y
  1. Install OpenJDK 17:
sudo apt install openjdk-17-jdk -y
  1. Verify Java Installation:
java -version

You should see output like this:

openjdk version "17.0.8" 2024-09-24
OpenJDK Runtime Environment (build 17.0.8+10-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 17.0.8+10-Ubuntu-1, mixed mode, sharing)

Step 2: Install Apache Maven

Maven is a popular build tool for managing Java projects.

  1. Install Maven:
sudo apt install maven -y
  1. Verify Maven Installation:
mvn -version

You should see output like this:

Apache Maven 3.8.1
Maven home: /usr/share/maven
Java version: 17.0.8, vendor: Ubuntu

Step 3: Create a Simple RollDice Application Using Maven

  1. Generate a New Maven Project:
sudo mvn archetype:generate -DgroupId=com.example -DartifactId=RollDiceApp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will generate the following directory structure:

RollDiceApp/
├── pom.xml
├── src/
    ├── main/java/com/example/App.java
    ├── test/java/com/example/AppTest.java
  1. Navigate to the Project Directory:
cd RollDiceApp

Step 4: Write the RollDice Application

  1. Open the App.java file in an editor:
sudo nano src/main/java/com/example/App.java
  1. Replace the content of App.java with the following code:
package com.example;

import java.util.Random;

public class App {
    public static void main(String[] args) {
        Random random = new Random();
        int diceRoll = random.nextInt(6) + 1; // Generates a random number between 1 and 6
        System.out.println("You rolled a: " + diceRoll);
    }
}
  1. Save and exit.

Step 5: Update the pom.xml File

The pom.xml file defines the dependencies and build plugins for the project. Open it for editing:

sudo nano pom.xml

Replace its content with the following:

<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>RollDiceApp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>RollDiceApp</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <!-- JUnit 5 for testing -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</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.8.1</version>
        <configuration>
          <source>17</source>
          <target>17</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.2</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.example.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Save and exit.

Step 6: Add Tests for the RollDice Application

  1. Open the AppTest.java file for editing:
sudo nano src/test/java/com/example/AppTest.java
  1. Replace its content with the following:
package com.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Random;

public class AppTest {

    @Test
    public void testDiceRoll() {
        Random random = new Random();
        int diceRoll = random.nextInt(6) + 1; // Simulates a dice roll
        assertTrue(diceRoll >= 1 && diceRoll <= 6, "Dice roll is out of range!");
    }
}
  1. Save and exit.
  2. Run the tests using Maven:
sudo mvn test

You should see:

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

Step 7: Build and Run the Application

  1. Build the Project:
sudo mvn clean package

This will generate a JAR file in the target directory: RollDiceApp-1.0-SNAPSHOT.jar.

  1. Run the Application:
sudo java -jar target/RollDiceApp-1.0-SNAPSHOT.jar

You should see output like:

You rolled a: 4

Conclusion

Congratulations! You’ve successfully created a RollDice Java application, added automated tests, built it with Maven, and executed it on Ubuntu 24.04 LTS. This project serves as a foundation for understanding Maven’s capabilities and Java development.

Happy coding! 🚀

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *