Navigation Bar

Saturday, July 18, 2026

Java : Using Maven to build and compile your HelloWorld application

Install java, set the JAVA_HOME to the java installation path and 
PATH variable to existing PATH + %JAVA_HOME%\bin folder.

Install MAVEN and set MAVEN_HOME to the maven installation path and
PATH variable to existing PATH + %MAVEN_HOME%\bin folder.

(BASE_FOLDER)/com/example
App.java
package com.example;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

In BASE_FOLDER
javac com/example/App.java

java com.example.App
Hello, World!

https://maven.apache.org/ 
Maven is a build tool for java projects. ......

Creating a MAVEN pom.xml file.
<project xmlns="http://apache.org" xmlns:xsi="http://w3.org"
  xsi:schemaLocation="http://apache.org http://apache.org">
<modelVersion>4.0.0</modelVersion>  
 
<groupId>com.example</groupId>
<artifactId>myapp1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

<build>
        <plugins>
            <plugin>
<groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <mainClass>com.example.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The Root and Metadata Elements
<project xmlns="http://apache.org" xmlns:xsi="http://w3.org" xsi:schemaLocation="http://apache.org http://apache.org">
  • <project>: The root tag that encloses the entire Maven configuration.
  • xmlns and xsi: These tell your code editor and Maven where to find the rules (schemas) for validating the XML structure. 

  • <modelVersion>4.0.0</modelVersion>  
    <modelVersion>: Defines the version of the POM structure layout. Version 4.0.0 is the mandatory standard for Maven 2 and Maven 3.

    Project Coordinates (The GAV)
    These three elements uniquely identify your project across the Java ecosystem.
    <groupId>com.example</groupId>

    <groupId>: The organization or package name. It usually follows reverse domain name rules (like com.companyname).

    <artifactId>myapp1</artifactId>
    <artifactId>: The specific name of your project. This becomes the actual name of your compiled JAR file.

    <version>1.0-SNAPSHOT</version>
    <version>: The current version of your app. -SNAPSHOT means it is a work-in-progress development version.

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    
  • <properties>: A section used to define variables or settings.
  • source: Tells Maven your source code is written using Java 17 features.
  • target: Tells Maven to compile bytecode that is fully compatible with Java 17 runtimes.
  • <build>: Contains configurations that manage how your project compiles, runs, and bundles.
  • <plugins>: A wrapper tag holding the list of external tools/extensions Maven will use. 
  •         <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>

    • <plugin>: Declares an external tool to use.
    • groupId & artifactId: Points Maven to download the official plugin that can execute Java programs.
    • version: Locks down the version of the plugin so your build remains stable over time.
                <configuration>
                    <mainClass>com.example.App</mainClass>
                </configuration>
    • <configuration>: Passes custom settings directly to the plugin.
    • <mainClass>: Tells the plugin exactly where your public static void main method lives so it knows what to run.


    See what below command does
    mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false




    No comments:

    Post a Comment