Skip to content

This project provides a comprehensive guide for setting up a Jenkins pipeline to automate the build, test, and deployment processes of a basic Java Maven application. It includes sample Java code, Maven configuration (pom.xml), and a Jenkinsfile to facilitate continuous integration and deployment.

Notifications You must be signed in to change notification settings

atulkamble/jenkins-java-maven-pipeline

Repository files navigation

Jenkins Pipeline Setup for Basic Java Maven Application

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.

1. Basic Java Code

Create a simple Java project with the following file structure:

basic-java-app/
│
├── src/
│   └── main/
│       └── java/
│           └── com/
│               └── example/
│                   └── App.java
│
├── pom.xml
└── Jenkinsfile

App.java

package com.example;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

2. Maven Configuration (pom.xml)

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>

3. Jenkinsfile

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!'
        }
    }
}

4. Steps to Set Up and Run the Pipeline

  1. Set Up Jenkins:

    • Install Jenkins and necessary plugins: Git Plugin, Pipeline Plugin, Maven Integration Plugin, and JDK Tool.
  2. Configure Tools in Jenkins:

    • Go to Manage Jenkins > Global Tool Configuration.
    • Add Maven and JDK installations that match the versions specified in the Jenkinsfile.
  3. Create a New Jenkins Pipeline Job:

    • Open Jenkins and click on New Item.
    • Choose Pipeline, enter a name for your job, and click OK.
    • In the Pipeline section, select Pipeline script from SCM.
    • Choose Git as the SCM and enter your repository URL.
    • Set Script Path to Jenkinsfile.
  4. Commit the Code to Your Repository:

    • Save the App.java, pom.xml, and Jenkinsfile in the root directory of your repository.
    • Push the code to your Git repository.
  5. 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.

Customization

  • 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.

About

This project provides a comprehensive guide for setting up a Jenkins pipeline to automate the build, test, and deployment processes of a basic Java Maven application. It includes sample Java code, Maven configuration (pom.xml), and a Jenkinsfile to facilitate continuous integration and deployment.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published