1

MAVEN INSTALLATION

 sudo apt update

sudo apt install openjdk-17-jdk -y

java --version

sudo update-alternatives --config java

sudo apt install maven -y

mvn -version

Creating a Maven Project

Step 1: Open Terminal & Generate Project

If there is already a folder named MyMavenApp then change the -DrartifactId value to some other name or delete the MyMavenApp folder.

mvn archetype:generate -DgroupId=com.example -DartifactId=MyMavenApp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

cd MyMavenApp

Maven Project Layout

sudo snap install tree

tree

Open pom.xml file

gedit pom.xml

Remove the existing pom.xml file and paste the following

<?xml version="1.0" encoding="UTF-8"?>

<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>MyMavenApp</artifactId>

  <version>1.0-SNAPSHOT</version>

  <packaging>jar</packaging>


  <properties>

    <!-- Java version -->

    <maven.compiler.source>17</maven.compiler.source>

    <maven.compiler.target>17</maven.compiler.target>


    <!-- JUnit and Surefire versions -->

    <junit.jupiter.version>5.10.1</junit.jupiter.version>

    <surefire.plugin.version>3.0.0-M7</surefire.plugin.version>

  </properties>


  <dependencies>

    <!-- JUnit 5 API for writing tests -->

    <dependency>

      <groupId>org.junit.jupiter</groupId>

      <artifactId>junit-jupiter-api</artifactId>

      <version>${junit.jupiter.version}</version>

      <scope>test</scope>

    </dependency>


    <!-- JUnit 5 Engine for running tests -->

    <dependency>

      <groupId>org.junit.jupiter</groupId>

      <artifactId>junit-jupiter-engine</artifactId>

      <version>${junit.jupiter.version}</version>

      <scope>test</scope>

    </dependency>

  </dependencies>


  <build>

    <plugins>

      <!-- Compiler plugin for Java 17 -->

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-compiler-plugin</artifactId>

        <version>3.8.1</version>

        <configuration>

          <source>${maven.compiler.source}</source>

          <target>${maven.compiler.target}</target>

        </configuration>

      </plugin>


      <!-- Surefire plugin to run JUnit 5 tests -->

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-surefire-plugin</artifactId>

        <version>${surefire.plugin.version}</version>

        <configuration>

          <argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>

        </configuration>

      </plugin>

    </plugins>

  </build>


</project>

Updating the Default Java Files

gedit src/main/java/com/example/App.java

Replace the existing content with this code:

package com.example;

public class App {
    public String getMessage() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        System.out.println(new App().getMessage());
    }
}


gedit src/test/java/com/example/AppTest.java
Replace the existing content with this code:
package com.example;

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

class AppTest {

    @Test
    void testGetMessage() {
        App app = new App();
        assertEquals("Hello World!", app.getMessage(), "Message should be 'Hello World!'");
    }
}

Compile
mvn clean install
Running the Application
java -cp target/classes com.example.App

Comments

Popular posts from this blog

4