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
✅ Apache Maven Installation on Windows
🛠️ Prerequisites
-
Java JDK (Java Development Kit) must be installed.
-
Set
JAVA_HOME
environment variable.
1. Download Maven
-
Visit the official Apache Maven website:
👉 https://maven.apache.org/download.cgi -
Click on Binary zip archive to download (e.g.,
apache-maven-3.9.6-bin.zip
)
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 Edit → New → Add:
-
4. Verify Installation
-
Open Command Prompt
-
Type:
✅ 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:
✅ 3. View or Edit the Code
📄
App.java
Located at:
src/main/java/com/example/App.java
✅ 4. Build the Project
Open terminal or command prompt in your project directory:
✅ 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 thetarget/
directory:Expected Output:
✅ 6. (Optional) Add Dependencies to
pom.xml
You can add external libraries like this:
Then run:
-
✅ Basic Maven Commands
Command | Description |
---|---|
mvn -v | Check Maven version |
mvn clean | Deletes the target/ directory (cleans previous build) |
mvn compile | Compiles the source code of the project |
mvn test | Runs unit tests |
mvn package | Compiles, tests, and packages the code into a .jar or .war |
mvn install | Installs the .jar or .war into your local Maven repository (~/.m2) |
mvn deploy | Deploys the package to a remote repository (e.g., Nexus, Artifactory) |
mvn validate | Validates if the project is correct and all info is available |
mvn verify | Runs any checks (like integration tests) before packaging |
✅ Step-by-Step: Jenkins + Maven Integration
🔧 Step 1: Install Maven in Jenkins
-
Go to Jenkins Dashboard → Manage Jenkins → Global Tool Configuration
-
Scroll to Maven section → click Add Maven
-
Name it something like:
Maven_3.9.6
-
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:
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.
Comments
Post a Comment
POST Answer of Questions and ASK to Doubt