Mastering Unit Testing Using Mockito and JUnit
上QQ阅读APP看书,第一时间看更新

Jenkins

Jenkins is an open source CI tool written in Java. It runs on any web container compliant with Servlet Specification 2.4. The new Apache Tomcat server is an example of a web container with which Jenkins can be integrated as a Windows service.

Jenkins supports various source control platforms, such as CVS, SVN, Git, Mercurial, and ClearCase through the use of plugins.

It can execute automated builds on Ant and Maven projects. Jenkins is free (MIT license) and runs on many operating systems. Jenkins doesn't allow you to create a Gradle project, but you can create a free-style project and build Gradle projects.

To install Jenkins on your local machine, follow the instructions at https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins.

Once Jenkins is installed, we will perform the following steps to configure Jenkins:

  1. Launch the Jenkins URL; from the home page go to Manage Jenkins | Configure system.
  2. Now you need to set up the JDK. Go to the JDK section, click on JDK installations and then click on Add JDK. Uncheck the Install automatically checkbox, and enter a Name and JAVA_HOME path. You can add as many JDKs as you want. Name and JAVA_HOME location uniquely identify the version of JDK. In your project, you can refer to the JDK you want to use. The following screenshot shows the installation of JDK:
  3. Now, set up Maven. Go to the Maven section and click on Maven installations. Now, click on Add Maven, uncheck the Install automatically checkbox, enter a Name, and set it to MAVEN_HOME.

    In general, if the checkbox Install automatically is checked, then Jenkins will ask you to select a version of the tool and download the version. You can install or add multiple versions of the software and just give a unique name. For example, you can add a name, Maven3, to refer to Maven Version 3.1.1 and add Maven2 to refer to Version 2.2.1. In your build job, Jenkins will show you the list and select the appropriate version you need. The following screenshot shows the installation of Maven:

  4. Go to the Ant section and click on Ant installations. Then, click on Add Ant, uncheck the Install automatically checkbox, enter a Name, and set it to ANT_HOME.

Our basic configuration is complete. Next, we will start building a Java project using Gradle.

The Gradle project

Jenkins doesn't come with Gradle. You need to install a plugin as follows:

  1. Launch the Jenkins URL; from the home page, go to Manage Jenkins | Manage Plugins. Go to the Available tab; in the Filter textbox (located at the right top corner of the page), enter gradle. It will bring you Gradle Plugin. Check the checkbox associated with Gradle Plugin and click on Install without restart.

    This will install the Gradle plugin. Jenkins will show you the progress of installation. Once the installation is over, you need to configure Gradle, like we did for Ant and Maven. Refer to the following screenshot to install Gradle plugin:

  2. From the home page, go to Manage Jenkins | Configure System. Scroll down to the Gradle section and click on Gradle installation. Then, click on Add Gradle, uncheck the Install automatically checkbox, enter a Name, and set GRADLE_HOME.
  3. Go back to the home page. The Jenkins convention for project building is job. A job runs continuously, invokes scripts, and gives feedback. To set up an automated build process, the user has to configure a job. Click on the create new jobs hyperlink to add a new project type. Jenkins supports several types of build jobs. Two most commonly used jobs are the free-style builds and the Maven 2/3 builds. The free-style projects allow you to configure any sort of build job; this job type is highly flexible and configurable. However, you can install plugins for other types.

    The following screenshot displays how to create a gradleProject free-style job:

  4. The free-style project has several settings. In Advance Project Options, you can set Quiet period (time to wait after a build), Retry Count (number of attempts to checkout from the repository), and so on. In Source Code Management, you can choose a version control tool type. Version control is one of the most important things in CI. It keeps track of software versions, we can revert our changes at any point in time, look at file history, and much more. By default, Jenkins comes with the source code management tool plugins, CVS and SVN, but we can install plugins to support other types, such as Git and Rational ClearCase. We didn't configure any version control tool yet; so, choose None, as shown in the following screenshot:
  5. Next is the Build Trigger event, and the build trigger knows when to start a job. There are several types:
    • Build after other projects are built: This implies that the job will be invoked after another job
    • Build periodically: This signifies the periodic schedule for cron expressions, that is, every 5 minutes or every 30 minutes and so on
    • Poll SCM: This implies polling the version control location after a specific time set in the Schedule option

    We don't have other jobs or a version control tool, so choose Build periodically and set Schedule to H/5**** to execute the build every 5 minutes, as shown in the following screenshot:

  6. The next section is Build. You can add several steps to a build. Click on Add build step. It will show you a step; choose Invoke Gradle script to call our Gradle project, as shown in the following screenshot:
  7. Now click on the Invoke Gradle radio button and choose the Gradle version we added. In the Tasks field, enter build to invoke the build task; you can add multiple tasks here. In the Build File field, enter the full path of your Gradle build file \Packt\chapter02\java\build.gradle, as shown in the following screenshot:
  8. Now click on Save. Jenkins will take you to the project's home page. Click on the Build Now hyperlink. It will start building our first project. It will show you a build history table with a build number, such as #1 Feb 4. 2014 09:18:45 PM. Click on the build# hyperlink and then click on Console Output. It will show you the build log. The following screenshot shows our Gradle build log:
  9. Now go back to the home page; it shows you the list of all builds and their status. It has a weather column—when all builds are failing, the weather shows a cloudy image, and when all builds are passing, the weather becomes sunny. You can invoke a build by clicking on the wheel symbol to the right of each build row. Refer to the following screenshot:

Our Gradle build configuration is complete. Automatically, after every five minutes, the build will be kicked off. We can configure a post build action to send an e-mail after each build. That way, if a build fails, then immediately a mail will be sent, and the concerned person can take care of the issue. So, the feedback cycle is faster.

In the next section, we will configure a Maven job.

The Maven project

In this section, we will configure Jenkins to execute a Maven build job. Please perform the following steps:

  1. Click on the New Job hyperlink to add a new project type. Select Build a maven2/3 project and enter a job name, as shown in the following screenshot:
  2. On the details page, choose Source Code Management as None, Build Triggers as Build periodically, and set H/5**** to execute the build in every 5 minutes.
  3. Next, go to the Build section and set the Root POM value; set the full file path location of your pom.xml file in the Demo project. You can leave the Goals and options section blank. Gradle will issue the default mvn install command. Refer to the following screenshot:
  4. Now click on Save. Jenkins will take you to the project's home page. Click on the Build Now hyperlink and it will start building our first project. It will show you a build history table with a build number such as #1 Feb 4. 2014 09:18:45 PM. Click on the build# hyperlink and then click on Console Output. It will show you the following build log:
    Executing Maven: -B -f D:\Packt\chapter02\maven\Demo\pom.xml install
    [INFO] Scanning for projects...
     
    [INFO] Building Demo 1.0-SNAPSHOT
     [INFO] Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugin
    (226 KB at 33.4 KB/sec)
    [INFO] BUILD SUCCESS
     [INFO] Total time: 2:28.150s
    [
     [JENKINS] Archiving D:\Packt\chapter02\maven\Demo\pom.xml to org.packt/Demo/1.0-SNAPSHOT/Demo-1.0-SNAPSHOT.pom
    [JENKINS] Archiving D:\Packt\chapter02\maven\Demo\target\Demo-1.0-SNAPSHOT.jar to org.packt/Demo/1.0-SNAPSHOT/Demo-1.0-SNAPSHOT.jar
    channel stopped
    Finished: SUCCESS
    
  5. Check whether Jenkins issued the mvn install command, created the JAR, and installed the artifacts in the .m2 repository.

Building the Ant project

We will set up a free-style software project to build using Ant. The following are the steps:

  1. Open the Jenkins URL, click on New Job, and select Build a free-style software project. Enter the name, ant, and then click on Ok.
  2. We don't have source code management, so skip this section. Go to Build Triggers and set the H/5 * * * * value to kick off build automatically in every 5 minutes.
  3. Go to the Build section and add a Invoke Ant build step, as shown in the following screenshot:
  4. Select an Ant version from the dropdown, set jar as Targets; jar will invoke test and compile. In Build File, browse to our build.xml file location and set the value, as shown in the following screenshot:
  5. Save the setting and the new job will be saved. Click on Build Now. It will start building the Demo project we created earlier in this chapter. The following is a screenshot of Console Output:

You can read about securing Jenkins, the post-build action, broken build claim plugins, and the CI game from the Jenkins wiki at http://jenkins-ci.org/.