DEVOPS
DevOps is a collection of two words, “Development” and “Operations,”
It is defined as a software engineering methodology which aims to integrate the work of
development teams and operations teams
DevOps Life Cycle
DevOps Lifecycle is the set of phases that includes DevOps for taking part in
Development and Operation group duties for quicker software program delivery.
DevOps follows positive techniques that consist of code, building, testing, releasing,
deploying, operating, displaying, and planning.
DevOps lifecycle follows a range of phases such as non-stop development, non-stop
integration, non-stop testing, non-stop monitoring, and non-stop feedback
1. Introduction to Maven and Gradle: Overview of Build Automation
Tools, Key Differences Between Maven and Gradle, Installation and Setup.
Maven
What is Maven? Maven is a build automation tool primarily used for Java projects. It
uses an XML configuration file called pom.xml (Project Object Model) to define
project settings, dependencies, and build steps.
Main Features:
Predefined project structure and lifecycle phases.
Automatic dependency management through Maven Central.
Wide range of plugins for things like testing and deployment.
Supports complex projects with multiple modules.
Gradle
What is Gradle? Gradle is a more modern and versatile build tool that supports
multiple programming languages, including Java, Groovy, and Kotlin. It uses a domain-
specific language (DSL) for build scripts, written in Groovy or Kotlin.
Main Features:
Faster builds thanks to task caching and incremental builds.
Flexible and customizable build scripts.
Works with Maven repositories for dependency management.
Excellent support for multi-module and cross-language projects.
Integrates easily with CI/CD pipelines.
Key Differences between Maven and Gradle
Performance Slower Faster due to caching
Flexibility Less flexible Highly customizable
Learning Curve Easier to pick up Slightly steeper
Script Size Verbose More concise
Dependency Management Uses Maven Central Compatible with Maven too
Plugin Support Large ecosystem Extensible and versatile
How to Install Maven:
1. Download Maven:
Go to the Maven Download Page and download the latest binary ZIP file.
2. Extract the ZIP File:
Right-click the downloaded ZIP file and select Extract All… or use any
extraction tool like WinRAR or 7-Zip.
3. Move the Folder:
After extraction, move the extracted Maven folder (usually named apache-
maven-x.x.x) to a convenient directory like C:\Program Files\.
4. Navigate to the bin Folder:
Open the Maven folder, then navigate to the bin folder inside.
Copy the path from the File Explorer address bar(e.g., C:\Program Files\
apache-maven-x.x.x\bin).
5. Set Environment Variables:
Open the Start Menu, search for Environment Variables, and select Edit the
system environment variables.
Click Environment Variables.
Under System Variables:
Find the path, double click on it and click New.
Paste the full path to the bin folder of your Maven directory (e.g., C:\
Program Files\apache-maven-x.x.x\bin).
6. Save the Changes:
Click OK to close the windows and save your changes.
7. Verify the Installation:
Open Command Prompt and run: mvn -v If Maven is correctly installed, it will
display the version number.
How to install Gradle
1. Download Gradle:
Visit the Gradle Downloads Page and download the latest binary ZIP file.
2. Extract the ZIP File:
Right-click the downloaded ZIP file and select Extract All… or use any
extraction tool like WinRAR or 7-Zip.
3. Move the Folder:
After extraction, move the extracted Gradle folder (usually named gradle-
x.x.x) to a convenient directory like C:\Program Files\.
4. Navigate to the bin Folder:
Open the Gradle folder, then navigate to the bin folder inside.
Copy the path from the File Explorer address bar (e.g., C:\Program Files\
gradle-x.x\bin).
5. Set Environment Variables:
Open the Start Menu, search for Environment Variables, and select Edit the
system environment variables.
Click Environment Variables.
Under System Variables:
Find the path, double click on it and click New.
Paste the full path to the bin folder of your Gradle directory (e.g., C:\
Program Files\gradle-x.x.x\bin).
6. Save the Changes:
Click OK to close the windows and save your changes.
7. Verify the Installation:
Open a terminal or Command Prompt and run: gradle -v If it shows the Gradle
version, the setup is complete.
2. Working with Maven: Creating a Maven Project, Understanding the
POM File, Dependency Management and Plugins.
Working with Maven Project
Note: Always create separate folder to do any program.
Open command prompt.
mkdir program2 – this will create program2 folder.
cd program2 – navigate program2 folder.
After then follow the below step to working with Maven project.
Step 1: Creating a Maven Project
You can create a Maven project using the mvn command (or through your IDE, as
mentioned earlier). But here, I’ll give you the essential pom.xml and Java code.
Let’s use the Apache Commons Lang library as a dependency (which provides
utilities for working with strings, numbers, etc.). We will use this in a simple Java
program to work with strings
1. Using Command Line:
To create a basic Maven project using the command line, you can use the
following command:
mvn archetype:generate -DgroupId=com.example -DartifactId=myapp -
DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Explanation of each part:
mvn archetype:generate: This instructs Maven to generate a new project based on a
template (archetype).
NOTE: An archetype in Maven is a template or blueprint used to create a new project
with a predefined structure. It contains a set of files, directories, and configuration
settings that provide a starting point for specific types of projects.
-DgroupId=com.example: This sets the groupId for your project, which is a unique
identifier for your project group (usually your domain, e.g., com.example).
-DartifactId=myapp: This sets the artifactId of your project, which is the name of
the resulting project (in this case, myapp).
-DarchetypeArtifactId=maven-archetype-quickstart: This specifies the archetype
to use for generating the project. The maven-archetype-quickstart is a basic Java
project template for quickly starting a new Java application.
-DinteractiveMode=false: This disables the interactive mode, meaning Maven will
use the default settings and won't ask for any additional user input.
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<!-- JUnit Dependency for Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Surefire Plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<useSystemOut>true</useSystemOut>
</configuration>
</plugin>
</plugins>
</build>
</project>
What are Dependencies?
A dependency is an external JAR file, library, or project that your project relies on.
These libraries or components might provide functionality that your application needs
but doesn’t implement itself (e.g., a logging framework, testing library, or database
connector).
What Are Plugins?
Plugins in Maven are used to execute specific goals during the build lifecycle of a
project.
Each plugin typically consists of several goals, where a goal is a single task that the
plugin performs.
Plugins are defined in the pom.xml file, and their goals can be bound to specific
phases in the Maven build lifecycle.
App.java
package com.example;
public class App {
public int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
App app = new App();
int result = app.add(2, 3);
System.out.println("2 + 3 = " + result);
System.out.println("Application executed successfully!");
}
}
AppTest.java
package com.example;
import org.junit.Assert;
import org.junit.Test;
public class AppTest {
@Test
public void testAdd() {
App app = new App();
int result = app.add(2, 3);
System.out.println("Running test: 2 + 3 = " + result);
Assert.assertEquals(5, result);
}
}
To build and run maven project, follow these steps
1. mvn compile
2. mvn test
3. mvn package
4. java -cp target/myapp-1.0-SNAPSHOT.jar com.example.App
The above command is used to run a Java application from the command line. Here’s a
breakdown of each part:
java: This is the Java runtime command used to run Java applications.
-cp: This stands for classpath, and it specifies the location of the classes and
resources that the JVM needs to run the application. In this case, it’s pointing to the
JAR file where your compiled classes are stored.
target/myapp-1.0-SNAPSHOT.jar: This is the JAR file (Java ARchive) that
contains the compiled Java classes and resources. It’s located in the target directory,
which Maven creates after you run mvn package.
com.example.App: This is the main class that contains the main() method. When you
run this command, Java looks for the main() method inside the App class located in
the com.example package and executes it.
A JAR JAR (Java ARchive) and a WAR WAR (Web Application ARchive) are both
file types commonly used in Java-based applications, and they are packaging formats
used to distribute Java applications or components.
3. Working with Gradle: Setting Up a Gradle Project, Understanding Build
Scripts (Groovy and Kotlin DSL), Dependency Management and Task
Automation.
1.Working with Gradle Project (Groovy DSL):
Step 1: Create a new Project
gradle init --type java-application
1. gradle:
This is the name of the build automation tool. Gradle is a tool used to
automate the process of building, testing, and deploying software. It
helps developers manage dependencies, compile code, run tests, and
package applications.
2. init:
The init command is used to initialize a new project in Gradle. When you
run gradle init, it creates a new project structure with basic
configuration files like build.gradle and source directories.
3. --type:
This is an option/flag in Gradle that specifies the type of project you
want to create. By using --type, you define the template for the project,
telling Gradle what kind of structure and files it should generate.
4. java-application:
This is the type of project you are initializing. It indicates that you want
to set up a Java-based application project. The Java application template
includes files and configurations that are tailored to building, testing,
and running a Java program.
So, when you run gradle init --type java-application, you’re telling Gradle to
initialize a new project with the structure and configuration for a Java
application.
Step 2: build.gradle (Groovy DSL)
plugins {
id 'application'
}
application {
mainClass = 'com.example.AdditionOperation'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.13.2'
}
test {
outputs.upToDateWhen { false }
testLogging {
events "passed", "failed", "skipped"
exceptionFormat "full"
showStandardStreams = true
}
}
Step 3: AdditionOperation.java(Change file name and update below code)
AdditionOperation.java
package com.example;
public class AdditionOperation {
public static void main(String[] args) {
double num1 = 5;
double num2 = 10;
double sum = num1 + num2;
System.out.printf("The sum of %.2f and %.2f is %.2f%n", num1, num2, sum);
}
}
Step 4: AdditionOperationTest.java (JUnit Test) (Change file name and update
below code)
AdditionOperationTest.java
package com.example;
import org.junit.Test;
import static org.junit.Assert.*;
public class AdditionOperationTest {
@Test
public void testAddition() {
double num1 = 5;
double num2 = 10;
double expectedSum = num1 + num2;
double actualSum = num1 + num2;
assertEquals(expectedSum, actualSum, 0.01);
}
}
Step 5: Run Gradle Commands
gradle build
gradle run
gradle test
2.Working with Gradle Project (Kotlin DSL):
Step 1: Create a new Project
gradle init --type java-application
Step 2: build.gradle.kts (Kotlin DSL)
plugins {
kotlin("jvm") version "1.8.21"
application
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
testImplementation("junit:junit:4.13.2")
}
application {
mainClass.set("com.example.MainKt")
}
tasks.test {
useJUnit()
testLogging {
events("passed", "failed", "skipped")
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
showStandardStreams = true
}
outputs.upToDateWhen { false }
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
Step 3: Main.kt (Change file name and update below code)
Change the file name App.java to Main.kt
package com.example;
fun addNumbers(num1: Double, num2: Double): Double {
return num1 + num2;
}
fun main() {
val num1 = 10.0;
val num2 = 5.0;
val result = addNumbers(num1, num2);
println("The sum of $num1 and $num2 is: $result");
}
Step 4: MainTest.kt (JUnit Test) (Change file name and update below code)
Change the file name MainTest.java to MainTest.kt
package com.example
import org.junit.Assert.*;
import org.junit.Test;
class MainTest {
@Test
fun testAddNumbers() {
val num1 = 10.0;
val num2 = 5.0;
val result = addNumbers(num1, num2);
assertEquals("The sum of $num1 and $num2 should be 15.0", 15.0, result, 0.001);
}
}
To run the project:
gradle build
gradle run
gradle test
4. Practical Exercise: Build and Run a Java Application with Maven,
Migrate the Same Application to Gradle.
Step 1: Creating a Maven Project
mvn archetype:generate -DgroupId=com.example -DartifactId=maven-example -
DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Step 2: Open The pom.xml File
You can manually navigate the project folder named call maven-example and open the file
pom.xml and copy the below code and paste it then save it.
Navigate using “cd maven-example”
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>maven-example</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</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>
</plugins>
</build>
</project>
Step 3: Open Java Code (App.java) File
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello, Maven");
System.out.println("This is the simple realworld example....");
int a = 5;
int b = 10;
System.out.println("Sum of " + a + " and " + b + " is " + sum(a, b));
}
public static int sum(int x, int y) {
return x + y;
}
}
Step 4: Run the Project
mvn clean install
mvn exec:java -Dexec.mainClass="com.example.App"
Step 5: Migrate the Maven Project to Gradle
gradle init
1. It will ask Found a Maven build. Generate a Gradle build from this? (default: yes)
[yes, no]
Type Yes
Select build script DSL:
1: Kotlin
2: Groovy
Enter selection (default: Kotlin) [1..2]
Type 2
Generate build using new APIs and behavior (some features may change in the next minor
release)? (default: no) [yes, no]
Type No
2. Navigate the project folder and open build.gradle file then add the below code and save it.
plugins {
id 'java'
}
group = 'com.example'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.12'
}
task run(type: JavaExec) {
main = 'com.example.App'
classpath = sourceSets.main.runtimeClasspath
}
Step 6: Run the Gradle Project
gradlew build
gradlew run
Step 7: Verify the Migration
Compare the Output: Make sure that both the Maven and Gradle builds produce the same
output:
Maven Output:
Hello, Maven
This is the simple realworld example....
Sum of 5 and 10 is 15
Gradle Output:
Hello, Maven
This is the simple realworld example....
Sum of 5 and 10 is 15
5. Introduction to Jenkins: What is Jenkins?, Installing Jenkins on Local or
Cloud Environment, Configuring Jenkins for First Use.
Introduction to Jenkins
What is Jenkins?
Jenkins is an open-source automation server widely used in the field of Continuous
Integration (CI) and Continuous Delivery (CD). It allows developers to automate the
building, testing, and deployment of software projects, making the development
process more efficient and reliable.
Installing Jenkins
Jenkins can be installed on local machines, on a cloud environment, or even in
containers. Here’s how you can install Jenkins in Window local System environments:
1. Installing Jenkins Locally
Step-by-Step Guide (Window):
1. Prerequisites:
Ensure that Java (JDK) is installed on your system. Jenkins requires Java
21.
You can check if Java is installed by running java -version in the terminal.
2. Install Jenkins on Window System):
Download the Jenkins Windows installer from the official Jenkins website.
Run the installer and follow the on-screen instructions. While installing
choose login system: run service as LocalSystem (not recommended).
After then use default port or you can configure you own port like I’m
using port 3030 then click on test and next.
After then change the directory and choose java jdk-21 path look like C:\
Program Files\Java\jdk-21\.
After then click next, next and then it will ask permission click on yes and it
will start installing.
After successfully installed, Jenkins will be running on port either default
port or chosen port like i choose port 3030 by default (you can access it in
your browser at http://localhost:8080) or http://localhost:3030.
2. Jenkins Setup in browser:
After opening browser by visiting your local address the browser should look like
below screenshot.
It will ask administrator password so you have to navigate the above highlighted
path and open that initialAdminPassword in notepad or any software to see the
password.
Just copy that password and paste it and click on continue.
It will ask to customize Jenkins so click on install suggested plugin it
will automatically install all required plugin.
After then create admin profile by filling all details then click on save and
continue after then save and finish after then click on start using Jenkin.