One of the most popular open source continuous integration tools is Jenkins. The good news is that Jenkins has support for Gradle via the Gradle plugin. Let's see how we can use the plugin to add our little Java project to Jenkins.
To install Jenkins on our computer, we must first download the installation files from the Jenkins website. A native installer is available for Mac OS X, Windows, and Linux. We simply run the installer software to install Jenkins on our computer. We can also download a WAR file and deploy it to a Java web container to install Jenkins. The WAR file is also a Java executable archive. This means that we can simply run the WAR file with the java -jar
command to execute Jenkins.
First, we must install the Gradle plugin in Jenkins. We launch a web browser and access the URL http://localhost:8080
. From the Jenkins main page, we select the link Manage Jenkins, which takes us to the appropriate page:
Here, we select Manage plugins. On the Plugin Manager page, we can use the Filter box at the top-right corner to search for Gradle Plugin
:
We select the plugin and click on the button Install without restart. If the installation of the plugin is successful, we see the following screen:
We need to restart Jenkins to make the plugin active and usable for our Jenkins projects.
Jenkins is now set up with the Gradle plugin, and it is time to create a job. From the main page, we select the New job link. We get a screen where we can fill in a name for the job and select the Build a free-style software project radio button:
If we have filled in the name and selected the radio button, we can click on the OK button. We go to the configuration page of our job. The name of the job is already filled with the value from the previous screen:
We must at least define our Git repository in the Source Code Management section. Also, we must add a build step in the Build section. We select the Git radio button to define the location of our Git repository in the URL of repository field. If we select the button Add build step, in the Build section, we can see the option Invoke Gradle script. Thanks to the Gradle plugin, we now have this option highlighted in the following screenshot:
We select the option Invoke Gradle script, and Jenkins adds new fields to configure our Gradle build:
First, we can choose if we want to use the Gradle wrapper for this project. We don't need it for our project, so we leave this unchecked.
Next, we can choose a Gradle version. We can install multiple Gradle versions for Jenkins, and we can choose which version we want to use. The default version is the one that is available on the system path. We will learn later how we can add more Gradle versions to Jenkins.
We can give our build step a short description in the Build step description field. The Switches field can contain the Gradle command-line options we want to use. For example, to exclude a task, we can set the value -x taskName
. The Tasks field must contain the tasks we want to execute. If our project has default tasks set and we want to run those, we can leave the Tasksfield empty. For our project, we want to invoke the clean
and build
tasks, so we set the value to clean build.
The Root Build script field is for a multi-project build where the root script is not in a default location. We can define the custom location here.
If a Gradle project has a build file name other than the default build.gradle
, we can set the value in the Build File field.
We have the basic setup for running our Gradle project. We click on the Save button and close the configuration. We return to the job page. At the left, we see a menu with the link Build now. We click on the link, and Jenkins starts the job:
Our code will be checked out from the Git repository, and the Gradle tasks clean
and build
are run. If the job is done, we can see the build result. From the build result page, we can see the console output when we click on the Console Output link:
At the left, we even see all Gradle tasks that have been executed. We can click on the links and jump directly to any output of the task.
To see the generated artifacts and test results, we must add two post-build actions to the job configuration. First, we select the Configure job link. At the Post-build Actions section, we click on the Add post-build action button. Here, we first select Archive the artifacts:
Next, we select Publish JUnit test result report:
The artifacts are saved in the build/libs
directory of our project. So, in the Files to archive field, we enter build/libs/*.jar
. And, we set the value for the field Test report XMLs to build/test-results/*.xml:
The configuration is done, so we click on the Save button. We can run the job again, and this time, we see the artifacts of our projects as downloadable links on the job page. The test results are also shown, and we can even see more details if we click on the Test Result link:
We can add extra Gradle versions to Jenkins. If, for example, some projects rely on Gradle 1.0 and others on Gradle 1.1, we must be able to add the Gradle versions.
From the Manage Jenkins page, we select Configure System. The page has a Gradle section, where we can add new Gradle installations:
If we click on the Add Gradle button, we can define a name for our Gradle installation in the Gradle name field. We also see a checkbox, Install automatically. If this is checked, Jenkins will download a Gradle version for us from the Internet. We select the version from the Version drop-down box:
If we want to use a locally installed instance of Gradle, we must uncheck the Install automatically checkbox. Now, we can set the Gradle location in the GRADLE_HOME field:
We must click on the Save button to save the changes. Now, we can choose the correct Gradle version in the jobs.
18.227.111.197