Title: CI/CD Pipeline Implementation with Maven and Jenkins on Ubuntu
Objective:
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
- 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>
----------------------------------
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.
--------------------------------------------
- Create a Git repository on your preferred platform (e.g., GitHub). Push the HelloWorld.java file to the repository.
- 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.
- 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.
- 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.
Comments
Post a Comment