What is Maven | How to create MAVEN Environment in details

 What is Maven?

Apache Maven is a build automation and project management tool primarily used for Java projects. It is based on a Project Object Model (POM) file (pom.xml) which defines a project’s structure, dependencies, plugins, and build lifecycle.



📂 POM (Project Object Model)

The heart of Maven is the pom.xml file which defines:

  • Project metadata (name, version, etc.)

  • Dependencies

  • Plugins

  • Build instructions

Example of pom.xml
<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>myapp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Your dependencies go here -->
    </dependencies>
</project>

✅ Apache Maven Installation on Windows

🛠️ Prerequisites

  • Java JDK (Java Development Kit) must be installed.

  • Set JAVA_HOME environment variable.

1. Download Maven

2. Extract the Zip File

  • Extract it to a preferred location, e.g.:
    C:\Program Files\Apache\Maven

3. Set Environment Variables

a) MAVEN_HOME

  • Open System Properties → Environment Variables

  • Under System variables, click New:

    • Variable name: MAVEN_HOME

    • Variable value: C:\Program Files\Apache\Maven\apache-maven-3.9.6 (adjust path if different)

b) Update the PATH variable

  • In the same Environment Variables window:

    • Find the variable named Path

    • Click EditNew → Add:


      %MAVEN_HOME%\bin

4. Verify Installation

  • Open Command Prompt

  • Type:

mvn -version

✅ 1. Create a New Maven Project

Option 1: Using Command Line (Recommended)

Run this in Command Prompt or Terminal:

mvn archetype:generate -DgroupId=com.test -DartifactId=MyFirstMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


📁 This creates a basic Maven project with:

  • Group ID: com.example

  • Artifact ID: MyFirstMavenProject

  • Java source files

  • pom.xml (project descriptor)

    ✅ 2. Project Structure

    Once created, your folder structure will look like:

    css

    MyFirstMavenProject/ ├── pom.xml └── src ├── main │ └── java │ └── com │ └── example │ └── App.java └── test └── java └── com └── example └── AppTest.java

    ✅ 3. View or Edit the Code

    📄 App.java

    Located at: src/main/java/com/example/App.java

    java

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

    ✅ 4. Build the Project

    Open terminal or command prompt in your project directory:

    bash

    cd MyFirstMavenProject mvn clean install

    ✅ This will:

    • Compile your Java code

    • Run unit tests (if any)

    • Package the app into a .jar file


    ✅ 5. Run the Project

    You’ll find the .jar file in the target/ directory:

    bash

    cd target java -cp MyFirstMavenProject-1.0-SNAPSHOT.jar com.example.App

    Expected Output:


    Hello, Maven World!

    ✅ 6. (Optional) Add Dependencies to pom.xml

    You can add external libraries like this:

    xml

    <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> </dependencies>

    Then run:


    mvn install
Basic Maven Commands:

Basic Maven Commands

CommandDescription
mvn -vCheck Maven version
mvn cleanDeletes the target/ directory (cleans previous build)
mvn compileCompiles the source code of the project
mvn testRuns unit tests
mvn packageCompiles, tests, and packages the code into a .jar or .war
mvn installInstalls the .jar or .war into your local Maven repository (~/.m2)
mvn deployDeploys the package to a remote repository (e.g., Nexus, Artifactory)


mvn validateValidates if the project is correct and all info is available
mvn verifyRuns any checks (like integration tests) before packaging


Step-by-Step: Jenkins + Maven Integration


🔧 Step 1: Install Maven in Jenkins

  1. Go to Jenkins Dashboard → Manage Jenkins → Global Tool Configuration

  2. Scroll to Maven section → click Add Maven

  3. Name it something like: Maven_3.9.6

  4. Choose:

    • Install automatically (recommended)

    • Or manually provide Maven home path

📌 Save changes.


Option 1: Freestyle Jenkins Job

🎯 Goal: Run Maven commands (clean, compile, test, package, install)

1. Create Job

  • Go to Jenkins → New Item → Freestyle project

  • Name: MavenBuildJob

2. Configure Source Code (optional)

  • Under Source Code Management, connect your GitHub/Git repo.

3. Add Maven Build Step

  • Scroll to Build → Add build step → Invoke top-level Maven targets

  • In Goals, enter:


clean install

You can replace it with compile, package, etc.

  • Choose Maven version you configured earlier: Maven_3.9.6

4. Save and Run

  • Click Build Now

  • Check console output.


Option 2: Jenkins Pipeline (Recommended)

📄 Example: Jenkinsfile with all Maven commands


pipeline { agent any tools { maven 'Maven_3.9.6' // Name from Global Tool Configuration jdk 'JDK17' // Ensure JDK is configured } stages { stage('Checkout') { steps { git 'https://github.com/your-username/your-java-project.git' } } stage('Clean') { steps { sh 'mvn clean' } } stage('Compile') { steps { sh 'mvn compile' } } stage('Test') { steps { sh 'mvn test' } } stage('Package') { steps { sh 'mvn package' } } stage('Install') { steps { sh 'mvn install' } } stage('Run App') { steps { sh 'java -cp target/*.jar com.example.App' } } } }
for windows:

✅ Jenkins Pipeline Script for Windows
Here is a complete Jenkinsfile for a typical Maven-based Java project:

📄 Windows-Compatible Jenkinsfile
groovy
Copy
Edit
pipeline {
    agent any

    tools {
        maven 'Maven_3.9.6'   // This name should match what you configured in Jenkins Global Tools
        jdk 'JDK17'           // Also configure JDK17 in Global Tools
    }

    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/your-username/your-java-project.git' // Replace with your repo
            }
        }

        stage('Clean') {
            steps {
                bat 'mvn clean'
            }
        }

        stage('Compile') {
            steps {
                bat 'mvn compile'
            }
        }

        stage('Test') {
            steps {
                bat 'mvn test'
            }
        }

        stage('Package') {
            steps {
                bat 'mvn package'
            }
        }

        stage('Install') {
            steps {
                bat 'mvn install'
            }
        }

        stage('Run Application') {
            steps {
                bat 'java -cp target\\*.jar com.example.App'  // Change `com.example.App` to your main class
            }
        }
    }
}


Post a Comment

0 Comments