Skip to main content

Experiment: CI/CD Pipeline Implementation with Maven and Jenkins on Ubuntu

 Title: CI/CD Pipeline Implementation with Maven and Jenkins on Ubuntu

Objective:

In this example, we will demonstrate the step-by-step implementation of a CI/CD pipeline using Maven and Jenkins on an Ubuntu system. We'll create a simple Java application, set up Jenkins to build and test the application, and trigger the pipeline whenever changes are pushed to a Git repository.

Prerequisites:

Ubuntu system (or a compatible Linux distribution) with JDK and Maven installed. Jenkins installed and running on the Ubuntu system. Git repository (e.g., GitHub, GitLab) to store the code.

Steps:

To organize your Java source code with the correct package structure and compile it in Ubuntu, you can follow these steps:

Open a terminal on your Ubuntu machine. Navigate to your project directory where you want to create the Java source code and the pom.xml file. For example: cd ~/my-java-project

Create the necessary directory structure for your Java source code:

mkdir -p src/main/java/com/example

Java Application:

  • Create a simple Java application. For example, let's create a HelloWorld.java file

Use a text editor to create the HelloWorldApp.java file in the appropriate directory: nano src/main/java/com/example/HelloWorldApp.java

In the text editor, add the following content to the HelloWorldApp.java file: package com.example; public class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello, Jenkins CI/CD Pipeline!"); } }

Save and exit the text editor (in nano, press Ctrl + X, then press Y, and finally press Enter). Now that you have your Java source code, navigate back to your project root directory:

cd ~/my-java-project

create pom.xml file with following content in this directory.

pom.xm file creation

The pom.xml file is a mandatory configuration file for Maven projects. It defines project information, dependencies, build configuration, and more.

Here's a basic example of what the content of a pom.xml file might look like for a simple Java project:


<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>jenkins-demo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- Add your dependencies here --> </dependencies> <build> <plugins> <!-- Add your build plugins here -->

          <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass>com.example.HelloWorldApp</mainClass> </manifest> </archive> </configuration> </plugin>

</plugins> </build> </project>


Make sure you have a pom.xml file in the root directory of your project, and it should contain information specific to your project, including the groupId, artifactId, and any necessary dependencies.

----------------------------------

Optional Part:

---------------------------------

if you wish to check use the mvn command to compile and package your project:

mvn clean compile package

After the build is successful, you will find the compiled JAR file in the target directory:

ls target

Run the JAR file using the java -jar command:

java -jar target/jenkins-demo-1.0-SNAPSHOT.jar

This should execute your Java application and print the "Hello, Jenkins CI/CD Pipeline!" message.

By following these steps, you've organized your Java source code with the correct package structure and compiled it into a JAR file. This JAR file can be executed on Ubuntu using the java -jar command as shown above.

--------------------------------------------

Git Repository:

  • Create a Git repository on your preferred platform (e.g., GitHub). Push the HelloWorld.java file to the repository.
Jenkins Setup:
  • Access your Jenkins instance in a web browser (http://localhost:8080).
  • Install the required plugins: Git Plugin, Maven Integration Plugin.
  • Create a new Jenkins job:
    • Choose "Freestyle project."
    • Configure the Git repository URL.
    • Set up Git credentials.
    • Add a build step:
      • Choose "Invoke top-level Maven targets."
      • Specify the Maven goals: clean compile.
  • Save the job configuration.
Webhook Configuration:
  • Configure Build Triggers:
    • Under the "Build Triggers" section, select "Poll SCM."
    • Set the polling schedule to check for changes (e.g.,***** for every minuts or */5 * * * * for every 5 minutes).
  • Configure Build Environment:
    • Under the "Build" section, click on "Add build step" and select "Invoke top-level Maven targets."
    • Enter the Maven goals (e.g., clean install) that you want Jenkins to execute.
Test the CI/CD Pipeline:
  • Make changes to the HelloWorldApp.java file and push them to the repository.
  • The webhook will trigger the Jenkins job.
  • Jenkins will build the Maven project and print the "Hello, Jenkins CI/CD!" message.

Conclusion:

You've successfully demonstrated the implementation of a CI/CD pipeline using Maven and Jenkins on an Ubuntu system. The pipeline automatically builds and tests your Java application whenever changes are pushed to the Git repository. This example provides a foundation for setting up more complex pipelines, including additional build stages, automated testing, deployment, and integration with other tools. Remember to customize the pipeline according to your project's requirements.

Comments

Popular posts from this blog

Experiment No. 5 Title: Applying CI/CD Principles to Web Development Using Jenkins, Git, and Local HTTP Server

  Experiment No. 5 Title: Applying CI/CD Principles to Web Development Using Jenkins, Git, and Local HTTP Server  Objective: The objective of this experiment is to set up a CI/CD pipeline for a web development project using Jenkins, Git, and webhooks, without the need for a Jenkinsfile. You will learn how to automatically build and deploy a web application to a local HTTP server whenever changes are pushed to the Git repository, using Jenkins' "Execute Shell" build step. Introduction: Continuous Integration and Continuous Deployment (CI/CD) is a critical practice in modern software development, allowing teams to automate the building, testing, and deployment of applications. This process ensures that software updates are consistently and reliably delivered to end-users, leading to improved development efficiency and product quality. In this context, this introduction sets the stage for an exploration of how to apply CI/CD principles specifically to web development using J

Experiment No. 6 Title: Exploring Containerization and Application Deployment with Docker

  Experiment No. 6 Title: Exploring Containerization and Application Deployment with Docker  Objective: The objective of this experiment is to provide hands-on experience with Docker containerization and application deployment by deploying an Apache web server in a Docker container. By the end of this experiment, you will understand the basics of Docker, how to create Docker containers, and how to deploy a simple web server application. Introduction Containerization is a technology that has revolutionised the way applications are developed, deployed, and managed in the modern IT landscape. It provides a standardised and efficient way to package, distribute, and run software applications and their dependencies in isolated environments called containers. Containerization technology has gained immense popularity, with Docker being one of the most well-known containerization platforms. This introduction explores the fundamental concepts of containerization, its benefits, and how it differs

Experiment No. 10 Title: Create the GitHub Account to demonstrate CI/CD pipeline using Cloud Platform.

  Experiment No. 10 Title: Create the GitHub Account to demonstrate CI/CD pipeline using Cloud Platform. Objective: The objective of this experiment is to help you create a GitHub account and set up a basic CI/CD pipeline on GCP. You will learn how to connect your GitHub repository to GCP, configure CI/CD using Cloud Build, and automatically deploy web pages to an Apache web server when code is pushed to your repository. Introduction: Continuous Integration and Continuous Deployment (CI/CD) pipelines are essential for automating the deployment of web applications. In this experiment, we will guide you through creating a GitHub account and setting up a basic CI/CD pipeline using Google Cloud Platform (GCP) to copy web pages for an Apache HTTP web application. Continuous Integration and Continuous Deployment (CI/CD) is a crucial practice in modern software development. It involves automating the processes of code integration, testing, and deployment to ensure that software changes are co