Using Jenkins

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 to 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 can 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.

Adding the Gradle plugin

  1. First, we must install the Gradle plugin in Jenkins.
  2. We will launch a web browser and access http://localhost:8080 . From the Jenkins main page, we will select the Manage Jenkins link, which takes us to the appropriate page, as shown in the following image:

    Adding the Gradle plugin

    Overview of options to manage Jenkins

  3. Here, we will select Manage Plugins.
  4. On the Plugin Manager page, we can use the Filter box at the top-right corner to search for Gradle Plugin:

    Adding the Gradle plugin

    Search results for Gradle plugin

  5. We select the plugin and click on the Install without restart button. After the installation of the plugin, we see the following screen:

    Adding the Gradle plugin

    Result after successful installation of the Gradle plugin

  6. We need to restart Jenkins to make the plugin active and usable for our Jenkins projects.

Configuring a Jenkins job

Jenkins is now set up with the Gradle plugin and it is time to create a job:

  1. From the main page, we select the New job link.
  2. We get a screen, where we can fill in a name for the job and select the Freestyle project radio button:

    Configuring a Jenkins job

    Creating a new job in Jenkins

  3. If we have filled in the name and selected the radio button, we can click on the OK button. We will go to the configuration page of our job. The name of the job is already filled with the value from the previous screen:

    Configuring a Jenkins job

    Configuring a Jenkins job

  4. We must at least define our Git repository in the Source Code Management section.
  5. Also, we must add a build step in the Build section.
  6. We select the Git radio button to define the location of our Git repository in the URL of the repository field. Note that the Git is not supported by default. We must install the Git client plugin in Jenkins to have Git support.
  7. If we select the Add build step button in the Build section, we can see the Invoke Gradle script option. Thanks to the Gradle plugin, we now have this option highlighted in the following screenshot:

    Configuring a Jenkins job

    Adding a build step to invoke Gradle

  8. We will select the  Invoke Gradle script option, and Jenkins adds new fields to configure our Gradle build:

    Configuring a Jenkins job

    Details for a Gradle build step

  9. First, we can choose whether we want to use the Gradle Wrapper for this project. We don't need it for our project, so we leave this unchecked.
  10. Next, we can choose Gradle Version. We can install multiple Gradle versions for Jenkins, and we can choose the version that we want to use. The default version is the one that is available on the system path. We will discuss how to add more Gradle versions to Jenkins later.
  11. We can give our build step a short description in the  Build step description field.
  12. The Switches field can contain the Gradle command-line options that we want to use. For example, to exclude a task, we can set the value for the Switches field to -x <taskName>.
  13. The Tasks field must contain the tasks that we want to execute. If our project has default tasks set and we want to run those, we can leave the Tasks field empty. For our project, we want to invoke the clean and build tasks, so we will set the value to  clean build.
  14. 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.
  15. 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.

Running the job

We have the basic setup for running our Gradle project:

  1. We will click on the Save button and close the configuration.
  2. We will return to the job page. On the left-hand side, we can see a menu with the Build Now link. We will click on the link and Jenkins will start the job:

    Running the job

    Overview of the Gradle job we have configured

    Our code will be checked from the Git repository, and the clean and build Gradle tasks are run.

  3. 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:

    Running the job

    Console output for running Gradle job in Jenkins

    On the left-hand side, we can also see all the Gradle tasks that have been executed. We can click on the links and jump directly to any output of the task.

Configuring artifacts and test results

To see the generated artifacts and test results, we must add the following two post-build actions to the job configuration:

  1. First, we will select the configure job link. In the Post-build Actions section, we will click on the Add post-build action button. Here, we will first select Archive the artifacts:

    Configuring artifacts and test results

    Adding build step for archiving artifacts

  2. Next, we will select Publish JUnit test result report:

    Configuring artifacts and test results

    Adding build step to publish test results

  3. The artifacts are saved in the build/libs directory of our project. So, in the Files to archive field, we will enter build/libs/.jar. We will also set the value for the Test report XMLs field to build/test-results/.xml:

    Configuring artifacts and test results

    Configuration for the build steps

  4. The configuration is done, so we click on the Save button. We can run the job again, and this time, we will 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:

    Configuring artifacts and test results

    Job overview page with published test results and archived artifacts

Adding Gradle versions

We can add extra Gradle versions to Jenkins. If, for example, some projects rely on Gradle 2.10 and others on Gradle 2.11, we can add other Gradle versions to Jenkins. Let's see how we can add another Gradle version to Jenkins:

  1. From the Manage Jenkins page, we will select Configure System. The page has a Gradle section, where we can add new Gradle installations:

    Adding Gradle versions

    Option to add a new Gradle version in Jenkins

  2. If we click on the Add Gradle button, we can define a name for our Gradle installation in the Gradle name field.
  3. We will also see an Install automatically checkbox. If this is checked, Jenkins will download a Gradle version for us from the Internet. We can select the version from the Version drop-down box:

    Adding Gradle versions

    Configuration to automatically install Gradle version

  4. 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:

    Adding Gradle versions

    Configuration for existing Gradle installation

  5. We must click on the Save button to save the changes. Now, we can choose the correct Gradle version in the jobs.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.208.97