Build a Java app with Maven using Jenkins Pipeline

by Ismail.EL
0 comment

1- Introduction

This tutorial allow you tu know how to use Jenkins to deploying and building a simple Java application (hello world) using Maven.

If you are a Java developer who uses Maven and who is new to CI/CD concepts, or you might be familiar with these concepts but don’t know how to implement building your application using Jenkins, then you are lucky! this tutorial is for you.

2- Prerequisites

For this tutorial, you will require:

3. Run Jenkins in Docker

In this tutorial, we will use docker container to build jenkins andw we will use jenkinsci/blueocean Docker image.

To run Jenkins in Docker, follow the commands below for either macOS and Linux or Windows.

3.1. On macOS and Linux

1- Open up a terminal consol.
2- Run the jenkinsci/blueocean image as a container i
n Docker using the following docker run command:

  • Maps the /var/jenkins_home directory in the container to the Docker volume with the name jenkins-data. If this volume does not exist, then this docker run command will automatically create the volume for you.
  • Maps the $HOME directory on the host (i.e. your local) machine (usually the /Users/ directory) to the /home directory in the container.

4. Proceed to the Setup wizard.

3.2. On Windows

1.Open up a command prompt window.
2. Run the jenkinsci/blueocean image as a container in Docker
using the following docker run command (bearing in mind that this command automatically downloads the image if this hasn’t been done):

For an explanation of these options, refer to the macOS and Linux instructions above. Proceed to the Setup wizard

4. Proceed to the Setup wizard.

to finish jenkins installation enter this url in your brower and follow the steps localhost:8080

or follow this artice that allow you how to install jenkins on linux

4. Create your Pipeline project in Jenkins

2. Go back to Jenkins, log in again if necessary and click create new jobs under Welcome to Jenkins!

2. In the Enter an item name field, specify the name for your new Pipeline project (e.g. simple-java-maven-app ).

3. Scroll down and click Multibranch Pipeline, then click OK at the end of the page.

how to build and deploy sbrin boot application using jenkins

4. From the Branch source field, choose the Github option. This option instructs Jenkins to obtain your Pipeline from github repository

finnaly click Apply and Save your jenkins pipeline:

5. Create your initial Pipeline as a Jenkinsfile

5. In “Project Repository” section you can set github repository url :

 Build a Java app with Maven

You’re now ready to create your Pipeline that will automate building your Java application with Maven in Jenkins. Your Pipeline will be created as a Jenkinsfile, which will be committed to your Git repository (simple-java-maven-app).

First, create an initial Pipeline to download a Maven Docker image and run it as a Docker container.
Also add a “Build” stage to the Pipeline that begins orchestrating this whole process.

1. Using your favorite text editor or IDE, create and save new text file with the name Jenkinsfile at the root of your local simple-java-maven-app Git repository.

2. Copy the following Declarative Pipeline code and paste it into your empty Jenkinsfile:

3. Save your edited Jenkinsfile and commit it to your Git repository, run the commands

the

And then push your chnges to uour remote github repository using the command below :

Now go to home page and you can say your pipeline created 😉

 Build a Java app with Maven

Click on the pipeline and you will see branch of your repository and then you click on the branch you want and “Build Now” button to build your java application code

after build you will see the result like that :

 Build a Java app with Maven

6. Add a test stage to your Pipeline

Go back to your project cloned in your lacal machine and open JenkisFile.

Copy and paste the following Declarative Pipeline syntax immediately under the Build stage of your Jenkinsfile:

so that you end up with:

Click on the pipeline again and then you click on “Build Now” button to build your java application code with Test stage

after build you will see the result like that :

7. Add a final deliver stage to your Pipeline

1. Go back to your text editor/IDE and ensure your Jenkinsfile is open.

2. Copy and paste the following Declarative Pipeline syntax immediately under the Test stage of your Jenkinsfile:

and add a skipStagesAfterUnstable option so that you end up with:

skipStagesAfterUnstable : Skip stages once the build status has gone to UNSTABLE

after commit it will be obligatory add a deviver.sh script in your project

3. Save your edited Jenkinsfile and commit it to your Git repository, run the commands

and commit all changes

And then push your changes to uour remote github repository using the command below :

Click on the pipeline again and then you click on “Build Now” button to build your java application code with Test stage

after build you will see the result like that :

 Build a Java app with Maven using Jenkins Pipeline

8. Conclusion

In this article I explained to you how to build a “hello world” application with maven using a jenkins pripeline, as you see i created 3 stages : build, test and deliver.
If you have any questions or feedback, feel free to leave a comment

5690cookie-checkBuild a Java app with Maven using Jenkins Pipeline

Related Posts

Leave a Comment