Comprehensive guide to set up a Jenkins pipeline for a basic Java Maven application, including the necessary Java code, pom.xml
, Jenkinsfile
, and steps to get everything up and running.
Create a simple Java project with the following file structure:
basic-java-app/
│
├── src/
│ └── main/
│ └── java/
│ └── com/
│ └── example/
│ └── App.java
│
├── pom.xml
└── Jenkinsfile
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Create a pom.xml
file in the root directory of your project to define your Maven configuration:
<?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/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>basic-java-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Basic Java Application</name>
<description>A basic Java application with Maven</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Add dependencies here if needed -->
</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>
Create a Jenkinsfile
in the root directory of your project to define the Jenkins pipeline:
pipeline {
agent any
tools {
maven 'Maven 3.8.6' // Ensure this version is configured in Jenkins
jdk 'JDK 11' // Ensure this JDK version is configured in Jenkins
}
stages {
stage('Checkout') {
steps {
// Checkout code from SCM
checkout scm
}
}
stage('Build') {
steps {
// Build the project using Maven
sh 'mvn clean install'
}
}
stage('Test') {
steps {
// Run unit tests
sh 'mvn test'
}
}
stage('Package') {
steps {
// Package the application
sh 'mvn package'
}
}
stage('Deploy') {
steps {
// Simple deployment example
sh 'echo "Deploying application..."'
// Example of copying artifacts to a deploy location
sh 'cp target/basic-java-app-1.0-SNAPSHOT.jar /path/to/deploy/'
}
}
}
post {
always {
// Clean up actions
sh 'echo "Cleaning up..."'
}
success {
// Actions on successful build
echo 'Build succeeded!'
}
failure {
// Actions on failed build
echo 'Build failed!'
}
}
}
-
Set Up Jenkins:
- Install Jenkins and necessary plugins:
Git Plugin
,Pipeline Plugin
,Maven Integration Plugin
, andJDK Tool
.
- Install Jenkins and necessary plugins:
-
Configure Tools in Jenkins:
- Go to
Manage Jenkins
>Global Tool Configuration
. - Add Maven and JDK installations that match the versions specified in the
Jenkinsfile
.
- Go to
-
Create a New Jenkins Pipeline Job:
- Open Jenkins and click on
New Item
. - Choose
Pipeline
, enter a name for your job, and clickOK
. - In the
Pipeline
section, selectPipeline script from SCM
. - Choose
Git
as the SCM and enter your repository URL. - Set
Script Path
toJenkinsfile
.
- Open Jenkins and click on
-
Commit the Code to Your Repository:
- Save the
App.java
,pom.xml
, andJenkinsfile
in the root directory of your repository. - Push the code to your Git repository.
- Save the
-
Run the Pipeline:
- Trigger a build in Jenkins to start the pipeline. Jenkins will checkout the code, build, test, package, and deploy your application according to the stages defined in the
Jenkinsfile
.
- Trigger a build in Jenkins to start the pipeline. Jenkins will checkout the code, build, test, package, and deploy your application according to the stages defined in the
- Deployment: Customize the
Deploy
stage based on your actual deployment requirements. - Notifications: Add additional steps in the
post
section for notifications or alerts.
This setup will allow Jenkins to automate the build, test, and deployment processes for your basic Java Maven application. Adjust the Jenkinsfile
and pom.xml
as needed to fit your specific project requirements.