Name –Rutvik Gondekar Subject – DevOps Lab
ID -VU4F2223031 TE IT A | Batch B
Experiment No. 8
Aim - Demonstration to Create Maven Pipeline.
Theory -
A Maven pipeline in Jenkins is a continuous integration/continuous delivery (CI/CD) process that
automates building, testing, and deploying Java applications using Apache Maven. Jenkins integrates
seamlessly with Maven and enables the automation of the entire software development lifecycle
(SDLC) through pipelines.
Key Concepts in a Maven Pipeline
1. Maven: Maven is a powerful build tool used primarily for Java projects. It manages project
builds, dependencies, packaging, testing, and deployment through a declarative
configuration file, pom.xml. The key idea behind Maven is to provide an easy way to
automate these tasks, making the build process reproducible and consistent.
2. Jenkins: Jenkins is an open-source automation server that facilitates CI/CD processes. It
supports pipelines for building, testing, and deploying applications. Jenkins allows for the
integration of various tools (like Maven) via plugins.
3. Pipeline: A Jenkins pipeline is a suite of automated tasks (like build, test, package, and
deploy) defined in a script (Jenkinsfile). Pipelines can be either declarative (simpler, preferred
for most use cases) or scripted.
Steps -
1. Create a Simple Maven Project (Cgpa Maven)
Create a Simple Maven Project Directory
Generate Maven Project by running the below code in command prompt.
“mvn archetype:generate -DgroupId=com.example -DartifactId=cgpa-percentage -
DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false”
Modify the App.java File, the pom.xml File, and push the Project to GitHub
2. Set Up Jenkins
Install Required Plugins (like Git Plugin and Maven Integration Plugin) by going to
Manage Jenkins > Manage Plugins.
Configure Maven in Jenkins by going to Manage Jenkins > Tools and Scrolling down
to Maven click on Add Maven, provide a name, and install Maven automatically.
3. Create a Pipeline Jenkins Job
Create a New Jenkins Job
Configure the GitHub Repository in the Pipeline Script section, choose Pipeline Script
from SCM. In the SCM dropdown, select Git. Enter the URL of your GitHub
repository. Specify the branch you want to build, usually master or main.
Set Up Jenkins Pipeline Script by creating a Jenkinsfile in your application.
Add, Commit and Push the file on Github Repository.
Save the Job
4. Run the Jenkins Job
Build the Job.
See Console Output.
Code:
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>percentage-giver</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>percentage-giver</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>com.example.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Jenkinsfile.txt
pipeline {
agent any
tools {
maven 'MAVEN_HOME' // Make sure 'Maven' is configured in Jenkins
}
parameters {
string(name: 'CGPA', defaultValue: '', description: 'Enter your CGPA')
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/Rutvikgg/percentage-giver.git' // Replace with your GitHub repo URL
}
}
stage('Build') {
steps {
// Use 'bat' for Windows instead of 'sh'
bat 'mvn clean compile'
}
}
stage('Run App') {
steps {
// Use 'bat' and Windows-specific command for running Java application
bat 'mvn exec:java -Dexec.mainClass="com.example.App"'
}
}
}
post {
success {
echo 'Pipeline executed successfully.'
}
failure {
echo 'Pipeline failed.'
}
}
}
App.java
package com.example;
public class App {
// Conversion factor can vary by institution, here we assume a factor of 9.5 for the conversion
public static final double CONVERSION_FACTOR = 9.5;
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Error: CGPA not provided. Please provide CGPA as a command-line argument.");
return;
}
// Parse the CGPA from the command-line argument
double cgpa = Double.parseDouble(args[0]);
// Convert CGPA into percentage using the conversion factor
double percentage = convertCgpaToPercentage(cgpa);
// Display the percentage result
System.out.printf("Your percentage is: %.2f%%\n", percentage);
}
// Method to convert CGPA into percentage
public static double convertCgpaToPercentage(double cgpa) {
return cgpa * CONVERSION_FACTOR;
}
}
Conclusion:
Hence, successfully demonstrated Maven Pipeline.