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

Example of Maven project that interacts with a MySQL database and includes testing

Example Maven project that interacts with a MySQL database and includes testing To install Java, MySQL, Maven, and write a Java program to fetch table data, execute, and create a JAR file using Maven on Ubuntu, you can follow these steps: Step 1: Install Java You can install Java using the following commands: sudo apt update sudo apt install default-jre sudo apt install default-jdk Verify the installation by running: java -version Step 2: Install MySQL You can install MySQL using the following commands: sudo apt update sudo apt install mysql-server During the installation, you'll be prompted to set a root password for MySQL or you can set password at latter stage using following steps.  sudo mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; exit Step 3: Install Maven You can install Maven using the following commands: sudo apt update sudo apt install maven Verify the installation by running: mvn -version Step 4: Create ...

Maven Create and Build Artifacts

In Maven, you can create and build artifacts using the package phase of the build lifecycle. The package phase is responsible for taking the compiled code and other project resources and packaging them into a distributable format, such as a JAR (Java Archive), WAR (Web Application Archive), or other custom formats. Here are the steps to create and build artifacts using Maven: Configure the Build Output: In your project's pom.xml file, you need to configure the output of the build. This includes specifying the type of artifact you want to create (e.g., JAR, WAR) and any additional resources to include. You do this in the <build> section of your pom.xml: <build>     <finalName>my-artifact</finalName> <!-- Name of the artifact without the extension -->     <plugins>         <!-- Plugin configurations for creating the artifact -->         <!-- For example, maven-jar-plugin or maven-war-p...

Maven Repositories (local, central, global)

Maven relies on repositories to manage dependencies, plugins, and other artifacts required for a project. There are typically three types of repositories in Maven: local, central, and remote/global repositories. Local Repository: Location: The local repository is located on your local development machine. By default, it's in the .m2 directory within your user home directory (e.g., C:\Users\<username>\.m2\repository on Windows or /Users/<username>/.m2/repository on macOS and Linux). Purpose: The local repository is used to store artifacts (JARs, POMs, and other files) that your machine has downloaded or built during previous Maven builds. These artifacts are specific to your local development environment. Benefits: Using a local repository improves build performance since it caches dependencies locally, reducing the need to download them repeatedly. It also ensures reproducibility by maintaining a local copy of dependencies. Central Repository: Location: The central repo...