0% found this document useful (0 votes)
12 views32 pages

Devops Lab Manula 1

The document provides a comprehensive guide on build automation tools, specifically Maven and Gradle, including their installation, project creation, and dependency management. It also covers Jenkins for continuous integration, Ansible for configuration management, and Azure DevOps for building and deploying applications. Practical exercises are included to reinforce the concepts learned throughout the document.

Uploaded by

Manu Manoj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views32 pages

Devops Lab Manula 1

The document provides a comprehensive guide on build automation tools, specifically Maven and Gradle, including their installation, project creation, and dependency management. It also covers Jenkins for continuous integration, Ansible for configuration management, and Azure DevOps for building and deploying applications. Practical exercises are included to reinforce the concepts learned throughout the document.

Uploaded by

Manu Manoj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

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
4. Practical Exercise: Build and Run a Java Application with Maven, Migrate
the Same Application to Gradle
5. Introduction to Jenkins: What is Jenkins?, Installing Jenkins on Local or
Cloud Environment, Configuring Jenkins for First Use
6. Continuous Integration with Jenkins: Setting Up a CI Pipeline, Integrating
Jenkins with Maven/Gradle, Running Automated Builds and Tests
7. Configuration Management with Ansible: Basics of Ansible: Inventory,
Playbooks, and Modules, Automating Server Configurations with
Playbooks, Hands-On: Writing and Running a Basic Playbook
8. Practical Exercise: Set Up a Jenkins CI Pipeline for a Maven Project, Use
Ansible to Deploy Artifacts Generated by Jenkins
9. Introduction to Azure DevOps: Overview of Azure DevOps Services, Setting
Up an Azure DevOps Account and Project.
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
Program -1
Introduction to Maven and Gradle: Overview of Build Automation Tools,
Key Differences Between Maven and Gradle, Installation and Setup
Introduction to Maven and Gradle
Overview of Build Automation Tools
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:
 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

Aspect Maven Gradle


Configuration XML (pom.xml) Groovy or Kotlin DSL
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 Compatible with Maven
Uses Maven Central
Management too
Extensible and
Plugin Support Large ecosystem
versatile

Installation and Setup

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.

Program 2
Working with Maven: Creating a Maven Project, Understanding the POM
File, Dependency Management and Plugins
1: Install the Java JDK
 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:
Overview of the Project
2: Creating a Maven Project
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.
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
 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:
1. Go to File > New Project.
2. Choose Maven from the list of project types.
3. Provide the groupId and artifactId for your project.

3: Understanding the POM File


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:
A basic pom.xml structure looks like this:

<?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>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<dependencies>
<!-- Dependencies go here -->
</dependencies>

<build>
<plugins>
<!-- Plugins go here -->
</plugins>
</build>
</project>

Key element in pom.xml:


 <groupId>: The group or organization that the project belongs to.
 <artifactId>: The name of the project or artifact.
 <version>: The version of the project (often follows a format like 1.0-
SNAPSHOT).
 <packaging>: Type of artifact, e.g., jar, war, pom, etc.
 <dependencies>: A list of dependencies the project requires.
 <build>: Specifies the build settings, such as plugins to use.

4: Dependency Management

Maven uses the <dependencies> tag in the pom.xml to manage external


libraries or dependencies that your project needs. When Maven builds the
project, it will automatically download these dependencies from a repository
(like Maven Central).

Example of adding a dependency:


<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>

 Transitive Dependencies
Maven automatically resolves transitive dependencies. For
example, if you add a library that depends on other libraries,
Maven will also download those.

 Scopes
Dependencies can have different scopes that determine when
they are available:

 compile (default): Available in all build phases.


 provided: Available during compilation but not at runtime
(e.g., a web server container).
 runtime: Needed only at runtime, not during compilation.
 test: Required only for testing.

5: Using Plugins

Maven plugins are used to perform tasks during the build lifecycle, such
as compiling code, running tests, packaging, and deploying. You can
specify plugins within the <build> section of your pom.xml.

 Adding Plugins

You can add a plugin to your pom.xml like so:

<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>
In this example, the maven-compiler-plugin is used to compile Java code
and specify the source and target JDK versions.
1. Common Plugins
 maven-compiler-plugin: Compiles Java code.
 maven-surefire-plugin: Runs unit tests.
 maven-jar-plugin: Packages the project as a JAR file.
 maven-clean-plugin: Cleans up the target/ directory.

2. Plugin Goals Each plugin consists of goals, which are specific tasks to
be executed. For example:
 mvn clean install: This will clean the target directory and then
install the package in the local repository.
 mvn compile: This will compile the source code.
 mvn test: This will run unit tests.

6: Dependency Versions and Repositories

1. Version Ranges

You can specify a version range for dependencies, allowing Maven to


choose a compatible version automatically. for example:

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>[30.0,)</version> <!-- Guava version 30.0 or higher -->
</dependency>

2. Repositories

Maven primarily fetches dependencies from Maven Central, but


you can also specify custom repositories. For example:

<repositories>
<repository>
<id>custom-repo</id>
<url>https://repo.example.com/maven2</url>
</repository>
</repositories>

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.

mvn archetype:generate -DgroupId=com.example -DartifactId=myapp


-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Note: See in your terminal below the project folder path showing after
executing the cmd manually navigate the path and see the project folder name
called myapp.

Step 2: Open The pom.xml File

 You can manually navigate the project folder named call myapp 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 myapp – is use to navigate the project folder.
 notepad pom.xml – is use to open pom file in notepad.
<?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>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<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>
<source>11</source>
<target>11</target>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<useSystemOut>true</useSystemOut>
</configuration>
</plugin>
</plugins>
</build>
</project>

Step 3: Open Java Code (App.java) File

 Open a file App.java inside


the src/main/java/com/example/ directory.

 After opening the App.java copy the below code and paste it in that file
then save it.

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!");


}
}

Step 4: Open Java Code (AppTest.java) File

 Open a file AppTest.java inside


the src/test/java/com/example/ directory.

 After opening the AppTest.java copy the below code and paste it in that
file then save it.

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

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
myapp

Step 4: Building the Project

To build and run this project, follow these steps:


1. Compile the Project

mvn compile

2. Run the Unit Tests

mvn test

3. Package the project into a JAR

mvn package
4. Run the application (using JAR)

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.
3. Working with Gradle: Setting Up a Gradle Project, Understanding Build
Scripts (Groovy and Kotlin DSL), Dependency Management and Task
Automation.
Gradle Project Overview
1: Setting Up a Gradle Project
 Install Gradle (If you haven’t already):
 Follow Gradle installation Program 1 click here
 Create a new Gradle project: You can set up a new Gradle project
using the Gradle Wrapper or manually. Using the Gradle Wrapper is
the preferred approach as it ensures your project will use the correct
version of Gradle.
 To create a new Gradle project using the command line:
gradle init --type java-application
This command creates a new Java application project with a
sample build.gradle file.
2: Understanding Build Scripts
Gradle uses a DSL (Domain-Specific Language) to define the build scripts.
Gradle supports two DSLs:
 Groovy DSL (default)
 Kotlin DSL (alternative)
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'
}
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")
}
}
Difference between Groovy and Kotlin DSL:
 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>.
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.
Example of adding a dependency in build.gradle (Groovy DSL):
dependencies {
implementation 'com.google.guava:guava:30.1-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
}
Example in build.gradle.kts (Kotlin DSL):
dependencies {
implementation("com.google.guava:guava:30.1-jre")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
}
2. Declaring repositories: To resolve dependencies, you need to specify
repositories where Gradle should look for them. Typically, you’ll use
Maven Central or JCenter, but you can also configure private
repositories.
Example (Groovy):
repositories {
mavenCentral()
}
Example (Kotlin):
repositories {
mavenCentral()
}
4: Task Automation
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:
gradle build
3. Creating custom tasks: You can define your own tasks to automate
specific actions. For example, creating a custom task to print a
message.
 Example Groovy DSL:
task printMessage {
doLast {
println 'This is a custom task automation'
}
}
 Example Kotlin DSL:
tasks.register("printMessage") {
doLast {
println("This is a custom task automation")
}
}
5: Running Gradle Tasks

To run a task, use the following command in the terminal:


gradle <task-name>
For example:
 To run the build task: gradle build
 To run a custom task: gradle printMessage
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.

Working with Gradle Project (Groovy DSL):


Step 1: Create a new Project
gradle init --type java-application
Step 2: build.gradle (Groovy DSL)
// Apply the java plugin to add support for Java
apply plugin: 'java'
// Apply the application plugin to add support for building an application
apply plugin: 'application'
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:23.0'
testCompile 'junit:junit:4.12'
}
mainClassName = 'App'
test {
outputs.upToDateWhen { false }
testLogging {
events "passed", "failed", "skipped"
exceptionFormat "full"
showStandardStreams = true
}
}
Step 3: AdditionOperation.java(Change file name and update below code)
 After creating project manually navigate the folder path
like src/main/java/ App.java
 After then open that file and copy the below code and past it, save it.
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)
 After creating project manually navigate the folder path
like src/test/java/ AppTest.java
 After then open that file and copy the below code and past it, save it.
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
 To build the project:
gradle build

 To run the project:


gradle test

 To test the project:


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
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.
 I’m 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=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.
 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.

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


 Open a file App.java inside
the src/main/java/com/example/ directory.
 After opening the App.java copy the below code and paste it in that file
then save it.

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


To build and run this project, follow these steps:
 Open the terminal in the project directory and run the following
command to build the project.

mvn clean install


 Run the program with below command:
mvn exec:java -Dexec.mainClass="com.example.App"

Step 5: Migrate the Maven Project to Gradle


1. Initialize Gradle: Navigate to the project directory (gradle-example)
and run:
gradle init

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


 Build the Project: In the project directory (gradle-example), run the
below command to build the project:
gradle build

 Run the Application: Once the build is successful, run the


application using below command:
gradle 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

Experiment 5: Introduction to Jenkins: What is Jenkins?, Installing


Jenkins on Local or Cloud Environment, Configuring Jenkins for First
Use
1. What Is Jenkins?
Definition and Overview
Jenkins is an open-source automation server written in Java. It is widely
used to facilitate Continuous Integration (CI) and Continuous
Deployment/Delivery (CD) pipelines in software development. Jenkins
automates repetitive tasks related to building, testing, and deploying
software, helping teams to integrate changes continuously and deliver high-
quality applications faster.
Key Functionalities
 Continuous Integration/Delivery: Automatically builds, tests, and
deploys code after each commit.
 Extensible Plugin Ecosystem: Over 1,500 plugins allow integration
with numerous tools such as Git, Maven, Gradle, Docker, and many
cloud providers.
 Automated Builds: Jenkins can poll version control systems, trigger
builds on code changes, and even schedule builds.
 Pipeline as Code: With the introduction of Jenkins Pipeline (using
either scripted or declarative syntax), you can define your entire build
process in a code file (Jenkinsfile) and store it alongside your code.
 Distributed Builds: Jenkins can distribute workloads across multiple
machines, helping to run tests and builds faster.
 Monitoring and Reporting: Provides detailed logs, test reports, and
build history.
Why Use Jenkins?
 Improved Efficiency: Automates mundane tasks to free up developer
time.
 Rapid Feedback: Quickly identifies integration issues with automated
tests.
 Scalability: Supports distributed builds and parallel testing.
 Customization: A highly configurable system that can integrate with
nearly every tool in the software development lifecycle.
 Community Support: Extensive community and plugin ecosystem that
continuously evolve Jenkins' capabilities.

2. Installing Jenkins on a Local Machine (Ubuntu)


Below are detailed step-by-step instructions for installing Jenkins on an
Ubuntu machine.
Step 1: Update Your System
Open your terminal and update your system repositories:
sudo apt update
sudo apt upgrade -y

Step 2: Install Java


Jenkins requires Java to run. It is recommended to use Java 11 or later. Install
OpenJDK 11:
sudo apt install openjdk-11-jdk -y
java -version

Step 3: Add the Jenkins Repository Key


Download and add the repository key so that your system trusts the Jenkins
packages:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add
-
Step 4: Add the Jenkins Repository

Add the Jenkins repository to your system’s sources list:


sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ >
/etc/apt/sources.list.d/jenkins.list'
Step 5: Update the Repository and Install Jenkins
Update the package list and install Jenkins:
sudo apt update
sudo apt install jenkins -y
Step 6: Start and Enable the Jenkins Service

Start Jenkins and configure it to start automatically on boot:


sudo systemctl start jenkins
sudo systemctl status jenkins
What it does: start jenkins launches the Jenkins service.
status jenkins confirms Jenkins is running.

Expected Output: A status message indicating Jenkins is active (running).

Step 7: Access Jenkins on the Local Machine


1. Open a Web Browser: Navigate to:

2. http://localhost:8080
3. Unlock Jenkins: The initial Jenkins screen will ask for an administrator password.
Retrieve it by running:

4. sudo cat /var/lib/jenkins/secrets/initialAdminPassword


What it does: Displays the auto-generated admin password.
Screenshot Tip: Capture the terminal output showing the password and the
Jenkins unlock screen.
5. Follow the Setup Wizard:
 Install Suggested Plugins: Click on “Install suggested plugins” for a typical setup.
 Create an Admin User: Follow prompts to create your first admin user.
 Finalize Configuration: Complete the remaining setup steps (e.g., instance
configuration).
 Screenshot Tip: Capture each major step (unlocking Jenkins, plugin installation, and
admin user creation).

3. Installing Jenkins on the Cloud (Optional)

There are several ways to run Jenkins on the cloud. One common method is to run
Jenkins using a Docker container on a cloud virtual machine. Below are instructions
using Docker on an Ubuntu cloud server (for example, on AWS EC2, DigitalOcean,
or any cloud provider that supports Ubuntu).
Step 1: Set Up a Cloud VM Running Ubuntu
Provision a New Ubuntu Server:
 Create a new VM using your cloud provider’s console.
 Ensure the VM has at least 1–2 GB RAM.
 Open port 8080 (and port 50000 for agent communication) in the security
group settings.
 Connect to the VM via SSH:
 ssh your-username@your-cloud-server-ip

Step 2: Install Docker on Your Cloud VM

1. Update the System:

sudo apt update


sudo apt upgrade -y

2. Install Docker:
sudo apt install docker.io -y
sudo systemctl start docker

sudo systemctl enable docker


3. Verify Docker Installation:

docker --version
Step 3: Run Jenkins in a Docker Container
1. Pull the Official Jenkins Image:

sudo docker pull jenkins/jenkins:lts


2. Run the Jenkins Container:
sudo docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v
jenkins_home:/var/jenkins_home jenkins/jenkins:lts
o What it does:

▪ -d runs the container in detached mode.


▪ --name jenkins names the container.
▪ -p 8080:8080 maps port 8080 (Jenkins UI) to the host.
▪ -p 50000:50000 maps the agent communication port.
▪ -v jenkins_home:/var/jenkins_home mounts a persistent volume for
Jenkins data.

o sudo docker ps

3. Access Jenkins on the Cloud VM:


• Open your browser and navigate to:
• http://your-cloud-server-ip:8080
• Follow the same unlocking and setup wizard process as described in the
local installation steps.

4. Configuring Jenkins for First Use


After installing Jenkins (locally or on the cloud), complete these steps to configure it
for first use:

Step 1: Unlock Jenkins


• Retrieve the Admin Password: As described earlier, run:
• sudo cat /var/lib/jenkins/secrets/initialAdminPassword
• Enter the Password: Copy the password into the “Administrator Password”
field on the Jenkins unlock page.
Step 2: Install Suggested Plugins
• Click “Install suggested plugins”: This option installs a set of commonly
used plugins (e.g., Git, Maven Integration, Pipeline).
• Wait for Installation: The plugin installation process might take several
minutes.
Step 3: Create Your First Admin User
• Enter the Admin User Details: Provide a username, password, full name,
and email address.

• Confirm Creation: Click “Save and Continue” once the details are entered.
Step 4: Configure the Instance
• Instance Configuration: Jenkins may ask you to confirm the URL for your
Jenkins instance. Verify that it is correct (e.g., http://localhost:8080 for local
installations or your cloud server’s IP address for cloud installations).
• Save the Configuration: Confirm the settings and proceed.
Step 5: Start Using Jenkins
Dashboard: Once the setup is complete, you will be taken to the Jenkins dashboard
where you can:

o Create new jobs (Freestyle projects, Pipelines, etc.)


o Install additional plugins as needed.

o Monitor builds and view logs.

You might also like