Skip to main content

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
            }
        }
    }
}


Comments

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC Database Connectivity using JSP and Servlet, Database connectivity on Java

JDBC Database Connectivity using JSP and Servlet, Database connectivity on Java JDBC:-   JDBC means Java database connectivity, it is used to connect from the front-end(application server) to the back-end(database server) in the case of Java web application. The database provides a set of tables to store records and JDBC will work similarly to the  bridge between the database table and application form. 1)  Class.forName("drivername")  // Manage Drive         Class.formName("com.mysql.jdbc.Driver");  // MYSQL      Class.forName ("oracle.jdbc.driver.OracleDriver"); //Oracle 2)  Manage Connection String     It establish connection from application server to database server, Java provide DriverManage class and getConnection that will return Connection object.    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","username","password"); 3)  Manage Statement to...