Devops_Lab_Manual_BCS_657D (1)
Devops_Lab_Manual_BCS_657D (1)
ENGINEERING
LAB MANUAL
DEVOPS – BCS657D
1
CONTENTS
1. Introduction to Maven and Gradle: Overview of Build Automation Tools, Key
Differences Between Maven and Gradle, Installation and Setup.
2. Working with Maven: Creating a Maven Project, Understanding the POM File,
Dependency Management and Plugins.
3. Working with Gradle: Setting Up a Gradle Project, Understanding Build Scripts (Groovy
and Kotlin DSL), Dependency Management and Task Automation.Working with Gradle:
Setting Up a Gradle Project, Understanding Build Scripts (Groovy and Kotlin DSL),
Dependency Management and Task Automation.
4. Practical Exercise: Build and Run a Java Application with Maven, Migrate the Same
Application to Gradle.
8. Practical Exercise: Set Up a Jenkins CI Pipeline for a Maven Project, Use Ansible to
Deploy Artifacts Generated by Jenkins.
10. Creating Build Pipelines: Building a Maven/Gradle Project with Azure Pipelines,
Integrating Code Repositories (e.g., GitHub, Azure Repos), Running Unit Tests and
Generating Reports.
11. Creating Release Pipelines: Deploying Applications to Azure App Services, Managing
Secrets and Configuration with Azure Key Vault, Hands-On: Continuous Deployment
with Azure Pipelines.
12. Practical Exercise and Wrap-Up: Build and Deploy a Complete DevOps Pipeline,
Discussion on Best Practices and Q&A.
2
Program 1
1. Introduction to Maven and Gradle: Overview of Build Automation Tools, Key
Differences Between Maven and Gradle, Installation and Setup.
Build automation tools help developers streamline the process of building, testing, and deploying
software projects. They take care of repetitive tasks like compiling code, managing
dependencies, and packaging applications, which makes development more efficient and error-
free.
Two popular tools in the Java ecosystem are Maven and Gradle. Both are great for managing
project builds and dependencies, but they have some key differences.
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:
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:
3
Integrates easily with CI/CD pipelines.
Download Maven:
o Go to the Maven Download Page and download the latest binary ZIP file.
Extract the ZIP File:
o Right-click the downloaded ZIP file and select Extract All… or use any extraction
tool like WinRAR or 7-Zip.
Move the Folder:
o After extraction, move the extracted Maven folder (usually named apache-maven-
x.x.x) to a convenient directory like C:\Program Files\.
Navigate to the bin Folder:
o Open the Maven folder, then navigate to the bin folder inside.
o Copy the path from the File Explorer address bar(e.g., C:\Program Files\apache-
maven-x.x.x\bin).
Set Environment Variables:
o Open the Start Menu, search for Environment Variables, and select Edit the
system environment variables.
o Click Environment Variables.
o Under System Variables:
1. Find the path, double click on it and click New.
2. Paste the full path to the bin folder of your Maven directory (e.g.,
C:\Program Files\apache-maven-x.x.x\bin).
Save the Changes:
o Click OK to close the windows and save your changes.
Verify the Installation:
4
o Open Command Prompt and run: mvn -v If Maven is correctly installed, it will
display the version number.
Download Gradle:
o Visit the Gradle Downloads Page and download the latest binary ZIP file.
Extract the ZIP File:
o Right-click the downloaded ZIP file and select Extract All… or use any extraction
tool like WinRAR or 7-Zip.
Move the Folder:
o After extraction, move the extracted Gradle folder (usually named gradle-x.x.x) to
a convenient directory like C:\Program Files\.
Navigate to the bin Folder:
o Open the Gradle folder, then navigate to the bin folder inside.
o Copy the path from the File Explorer address bar (e.g., C:\Program Files\gradle-
x.x\bin).
Set Environment Variables:
o Open the Start Menu, search for Environment Variables, and select Edit the
system environment variables.
o Click Environment Variables.
o Under System Variables:
1. Find the path, double click on it and click New.
2. Paste the full path to the bin folder of your Gradle directory (e.g.,
C:\Program Files\gradle-x.x.x\bin).
Save the Changes:
o Click OK to close the windows and save your changes.
Verify the Installation:
o Open a terminal or Command Prompt and run: gradle -v If it shows the Gradle
version, the setup is complete.
5
Program 2
2. Working with Maven: Creating a Maven Project, Understanding the POM File, Dependency
Management and Plugins.
If you haven’t installed the Java JDK yet, you can follow the link below to download and
install it. Download Java JDK from Oracle
Working with Maven is a key skill for managing Java-based projects, particularly in the areas of
build automation, dependency management, and project configuration. Below is a guide on
creating a Maven project, understanding the POM file, and using dependency management and
plugins:
There are a few ways to create a Maven project, such as using the command line, IDEs like
IntelliJ IDEA or Eclipse, or generating it via an archetype.
groupId: A unique identifier for the group (usually the domain name).
artifactId: A unique name for the project artifact (your project).
archetypeArtifactId: The template you want to use for the project.
DinteractiveMode=false: Disables prompts during project generation.
This will create a basic Maven project with the required directory structure and pom.xml file.
2. Using IDEs
Most modern IDEs (like IntelliJ IDEA or Eclipse) provide wizards to generate Maven projects.
For example, in IntelliJ IDEA:
6
3. Provide the groupId and artifactId for your project.
The POM (Project Object Model) file is the heart of a Maven project. It is an XML file that
contains all the configuration details about the project. Below is an example of a simple POM
file:
7
Program 3
3. Working with Gradle: Setting Up a Gradle Project, Understanding Build Scripts (Groovy and
Kotlin DSL), Dependency Management and Task Automation.
This command creates a new Java application project with a sample build.gradle file.
Gradle uses a DSL (Domain-Specific Language) to define the build scripts. Gradle supports two
DSLs:
Groovy DSL: This is the default language used for Gradle build scripts (build.gradle).
Example of a simple build.gradle file (Groovy DSL):
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4'
}
8
task customTask {
doLast {
println 'This is a custom task'
}
}
Kotlin DSL: Gradle also supports Kotlin for its build scripts (build.gradle.kts). Example of a
simple build.gradle.kts file (Kotlin DSL):
plugins {
kotlin("jvm") version "1.5.21"
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:2.5.4")
}
tasks.register("customTask") {
doLast {
println("This is a custom task")
}
}
Syntax: Groovy uses a more concise, dynamic syntax, while Kotlin offers a more
structured, statically-typed approach.
Error handling: Kotlin provides better error detection at compile time due to its static
nature.
Task Block: Tasks define operations in Gradle, and they can be executed from the command
line using gradle <task-name>.
9
3: Dependency Management
Gradle provides a powerful dependency management system. You define your project’s
dependencies in the dependencies block.
1. Adding dependencies:
Gradle supports various dependency scopes such
as implementation, compileOnly, testImplementation, and others.
Gradle tasks automate various tasks in your project lifecycle, like compiling code, running tests,
and creating builds.
1. Using predefined tasks: Gradle provides many predefined tasks for common activities,
such as:
build – compiles the project, runs tests, and creates the build output.
test – runs tests.
clean – deletes the build output.
2. Example of running the build task:
11
5: Running Gradle Tasks
For example:
6: Advanced Automation
You can define task dependencies and configure tasks to run in a specific order. Example of task
dependency:
task firstTask {
doLast {
println 'Running the first task'
}
}
task secondTask {
dependsOn firstTask
doLast {
println 'Running the second task'
}
}
In this case, secondTask will depend on the completion of firstTask before it runs.
12
Working with Gradle Project (Groovy DSL):
13
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
}
}
package com.example;
14
Step 4: AdditionOperationTest.java (JUnit Test) (Change file name and update below
code)
package com.example;
import org.junit.Test;
import static org.junit.Assert.*;
@Test
public void testAddition() {
double num1 = 5;
double num2 = 10;
double expectedSum = num1 + num2;
gradle build
15
To run the project:
gradle run
gradle test
16
Working with Gradle Project (Kotlin DSL):
17
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))
}
}
package com.example
fun main() {
val num1 = 10.0
18
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)
package com.example
import org.junit.Assert.*
import org.junit.Test
class MainTest {
@Test
fun testAddNumbers() {
val num1 = 10.0
val num2 = 5.0
19
Step 5: Run Gradle Commands
gradle build
gradle run
20
To test the project:
gradle test
21
Program 4
4. Practical Exercise: Build and Run a Java Application with Maven, Migrate the Same
Application to Gradle.
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.
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.
In case if you not getting project folder then type command in your cmd.
cd maven-example – is use to navigate the project folder.
notepad pom.xml – is use to open pom file in notepad.
<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>
22
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.example;
int a = 5;
int b = 10;
System.out.println("Sum of " + a + " and " + b + " is " + sum(a, b));
}
Note: before building the project make sure you are in the project folder if not navigate the project folder type
command in your command prompt cd maven-example.
Open the terminal in the project directory and run the following command to build the
project.
23
Step 5: Migrate the Maven Project to Gradle
gradle init
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
24
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'
}
Build the Project: In the project directory (gradle-example), run the below command
to build the project:
gradlew build
25
Run the Application: Once the build is successful, run the application using below
command:
gradlew run
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
26
Program 5
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. Prerequisites:
Ensure that Java (JDK) is installed on your system. Jenkins requires Java 21. If
not then click here.
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).
27
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.
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.
28
29