A guide for Beginners to configure Maven Project with Jenkins
Jenkins is a tool used for Continuous Integration. It is an open-source tool written in Java. It helps to automate all the tasks related to building, testing, and deploying the code from a shared repository and integrating project changes. It is compatible with many open-source plugins and tools such as Maven, Ant, Gradle, BitBucket, etc.
If we want to execute the automated scripts unattended or in a scheduled timeframe, generate the reports, and send the reports as email attachments to recipients, then we can go for Jenkins.
To run the Maven project in Jenkins, we need Jenkins.war file to run in the local system.
Software needs to be installed
We need the following software to be installed in our system as we are going to run a Maven project in Jenkins. Make sure that Git and Maven are already installed in the system.
How to Install Jenkins?
Step 1: Go to the URL https://jenkins.io/download/ to download Jenkins. war file.
Step 2: Click on “Generic Java Package (.war)” under Download Jenkins LTS.
Step 3: Navigate to the Folder after downloading.
Step 4: Type cmd in the address bar. This will open the Command Prompt.
Step 5: Type java -jar jenkins.war in the Command prompt to get started Jenkins server. After some time, we should be able to see the message “Jenkins is fully up and running” once Jenkins is launched successfully.
· If the command prompt is closed, Jenkins will not run. Until we are done with our Jenkins activities, the Jenkins server should be on run status.
· Jenkins always runs in localhost:8080. If some other tasks are running on port 8080, we can try some other port for Jenkins.
· Example : java -jar Jenkins.war --httpPort=8081
Step 6: When we run Jenkins. war file for the first time, we will get the initial admin password in the console. We can copy and paste this password into a Notepad for future use.
Alternatively, we can get the password from the following folder.
Right-click the file and open it with Notepad.
Step 7: Open a Chrome browser and type localhost:8080.
Step 8: When launching Jenkins for the first time, it asks us to type the Administrator Password. Copy and Paste the Administrator Password from Step 6.
Step 9: Click on ‘Continue’.
Step 10: We can see “Customize Jenkins”. Click on “Install suggested plugins” which takes some time to install.
Step 11: We can see “Create First Admin User”. Here either we can create a user with all the required details such as Username, Password, Confirm password, Full name, and E-mail address or “Continue as admin” if we want to continue as an admin.
Step 12: Click “Save and Finish”.
Step 13: We can see “Jenkins is ready!”. Click on “Start using Jenkins”.
Step 14: Click on “Manage Jenkins”.
Step 15: Click on “Manage Plugins”.
Step 16: We are going to integrate Maven, and Git with Jenkins. So, we have to make sure that Maven Integration plugin, and Git plugin are already installed in Jenkins. To check it, click on “Installed plugins” and search for one plugin at a time. If any of the plugins are not installed, then do the following steps below for the missing plugin.
Example: To install “Maven Integration” plugin
· In “Search available plugins”, type “Maven integration” and press “Enter”.
· Select “Maven Integration plugin” and click “Install without restart”.
· When the downloading is over, we can see “Success” for the Maven Integration plugin.
Step 17: Click “Manage Jenkins” again.
Step 18: Click on “Global Tool Configuration”.
Step 19: Under “JDK Installations”, Type “JAVA_HOME” as JDK Name.
Step 20: Set Path for JDK (The location where jdk file is located in the local system)
Step 21: Uncheck “Install Automatically”.
Step 22: Under “Git installations”, let the Name be as “Default”.
Step 23: Set the Path for Git under “Path to Git executable”. (The location where git is located in the local system).
Step 24: Uncheck “Install Automatically”.
Step 25: Under “Maven”, click on “Maven installations”, let the Maven Name be as “Default”.
Step 26: Set the Path for “MAVEN_HOME” (The location where maven is located in the local system).
Step 27: Uncheck “Install Automatically”.
Step 28: Click on the “Apply” and “Save” buttons. That’s it. Jenkins installation is done.
How to schedule and run Maven Project using Jenkins?
Step 1: Go to “Dashboard” and click on “New Item”.
Step 2: Enter an item name. Here I give the item name as “JenkinsDemoProject”.
Step 3: Select “Maven project”. This option will not be available if “Maven Integration plugin” is not installed.
Step 4: Click on “OK” button.
Step 5: Let’s give a description to the project in “Description”.
Step 6: Click on “Git” under Source Code Management.
Step 7: To get the GitHub URL, go to GitHub, and select the required Project, copy the URL.
In “Repository URL”, Paste the URL of the Git Repository.
It does not ask for Credentials; hence the project is public. In case of Private project, we have to add the Credentials.
Step 8: In “Branches to build”, we have to specify the GitHub branch name. In our project, there is only one branch – main. So, I specify the branch name as */main.
Note : We can check the branch name from GitHub.
Step 9: We can run the build either manually or automatically. “Build periodically” is one of the options to run the build automatically. To run manually, go to Step 11 directly.
To build periodically, check the box “Build periodically”.
Step 10: To explore more options about the schedule options, click on the question mark symbol next to “Schedule”.
We have to provide 5 values in Schedule textbox – MINUTE HOUR DOM MONTH DOW.
To schedule a run at 10.37 pm on Feb 22,Wednesday, the values will be 37 22 22 2 3.
MINUTE – 37
HOUR – 22
DOM (Day of the month) - 22
MONTH - 2 (1-January, 2-February, etc)
DOW (Day of the week) - 3 (0-Sunday, 1-Monday,2-Tuesday, etc)
Step 11: If we want to run our Maven goals, then it can be specified under “Build” -> “Goals and options”.
I enter the path for Root POM and “clean test” as my maven goal. Make sure the dependencies and testing.xml are added in pom.xml.
Note: In case of Cucumber projects, add the runner file name in testing.xml.
Step 12: Click “Apply” and “Save” button.
Step 13: In Dashboard, we can see the build automatically starts at 10.37 pm. Click on Build Number (#1) or Build time.
Step 14: Click on “Console Output” to view the results. Build is Success.
In this blog, we learned how to install Jenkins and run Maven project in Jenkins. It automates the build process quickly and delivers high quality software projects.