Introduction
In the previous article, we learned how to run Jenkins blue ocean docker container on aws ec2 ubuntu 18.04 lts.In this session, we will explain how to configure Jenkins’s blue ocean container to build a sample java application using maven and Java.
To know more about Apache maven visit https://maven.apache.org/download.cgi and if you want to install locally check out our previous tutorial on Install Apache Maven on ubuntu
Before We Begin:
- Ubuntu 18.04 server with root or user with sudo privileges
- Docker in running status
Step 1 :Run Jenkins blue-ocean docker container
sudo docker run -d --name myjenkins -p 8080:8080 -p 50000:50000 -v myjenkins_homelocal:/var/jenkins_home jenkinsci/blueocean
Check our Jenkins container running or not :
sudo docker ps
As you can see our container is up and running
Now go to the browser and complete Jenkins post-installation setup like Unlock Jenkins, Create Admin user and Install Suggested Plugins, We have already discussed these things in our previous tutorials. Check out Install and configure Jenkins on Docker
Tool installation on Jenkins (Running as container ):
So far it is good and we have Jenkins server configured with basic stuffs. Now it’s time for Java and Maven tool installation. Jenkins has the ability to configure any tool on the fly when it is required.
Install Java and Maven on Jenkins server running in a docker container:
Since Jenkins is running on docker we cannot install Java & Maven on the local system and give their paths. We have several methods to install required tools on Jenkins Docker container
Let’s start with Java. Even though we are actually running Jenkins on Java, meaning we must have Java installed, we aren’t going to use it.Instead we should be able to run our project with any version of Java
In the JDK section, You can see Install from java.sun.com and it requires an Oracle account. Give the username and password for your Oracle account
Next, select the required java version from the dropdown. Here I am selecting JDK8u221 and give any name for our JDK, let us give Java8
Also, enable the checkmark for the Java Licence agreement section
Now we have completed the necessary steps to install javaJDK by the Jenkins server itself. Next, we need to configure Jenkins to install the build tool maven
For maven installation, go to the Add Maven section and give any name to our Maven. Here I am giving Maven3. We are Installing Maven from Apache
Make sure You have enabled Install automatically option. Select the desired version, here we will install the latest version Maven 3.6.3.
Finally, save our tool configurations by first Apply& Save.
Install Maven Integration plugin:
Since our java project is built using Maven, We need to install a plugin named Maven Integration.
Click on Manage Jenkins
, Manage Plugins
, Available
tab, search for Maven,You can see the result itself shows Maven Integration.
Select it and click on Install without restart.It will install the plugin and finally we can see the status as below :
Clone the sample repository on GitHub
Obtain the simple “Hello world!” Java application from GitHub, by forking the sample repository of the application’s source code into your own GitHub account and then cloning this fork locally https://github.com/jenkins-docs/simple-java-maven-app
Build a Maven project
- Go back to Jenkins, log in again if necessary and click create new jobs under Welcome to Jenkins!
Note: If you don’t see this, click New Item at the top left. - In the Enter an item name field, specify the name for your new Maven project (e.g.
simple-java-maven-app
). - Scroll down and click Maven project, then click OK at the end of the page.
4.( Optional ) On the next page, specify a brief description for your job in the Description field (e.g:Testing of Jenkins to build a simple Java application with Maven.
5.Configure Source Code Management
This is the place where you can manage your source code to generate the final build. Here you have to configure git repository URL and credential in order to get the source code by Jenkins.
- From the SCM (Source Code Management) field, choose Git.
- In the Repository URL field, specify the directory repository where we have the cloned java project so that project build automatically.
- Give your git credentials (username & password
6.Build Triggers section will be by default selected with this option , Build whenever a SNAPSHOT dependency is built
7.In the Build tab you can set the Goals and Options of your final build generated by Jenkins using Gitrepository resources
Root POM –Already pom.xml must be present
Goals and options —Type clean install. This will compile and package, but it will also put the package in your local repository. This will make it so other projects can refer to it and grab it from your local repository.
On Post Steps tab select Run regardless of the build result.
Now just apply and save our configuration. We will discuss other available options later related to building configuration.
Next let’s Build Newly Created Item
Now all the necessary configurations are done. Then navigate to main dashboard after apply and save all the changes in new item configuration.
Select the configured job and click on Build Now.
After some time it will be completed and you can see Blue Color indication ,that means your build is successful
In order to view the build stages Go to Console Output and here you can see the process during build a project
You can see Jenkins is installing Maven in the steps like below :
https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip to /var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven3 on Jenkins
using credential d2b3497e-18c3-4695-a8c7-5da43aba9429
After that code will be fetched from our repo.JDK location downloaded by jenkins look like
/var/jenkins_home/tools/hudson.model.JDK/jdk8/bin/java
Finally you can see after build our jar file will be stored on /var/jenkins_home/workspace/our project/target folder
Conclusion
In this session, we learned how to configure blue ocean Jenkins for a sample java build. We learned how to install tools for java build and plugins. In the upcoming session, we will discuss more Jenkins configurations