It is good practice to have a continuous integration tool in a software project. With a continuous integration tool, we can automatically build our software in a controlled environment. In this chapter, we are going to take a look at the support for Gradle in several continuous integration tools.
First, we are going to create a sample Java project and use Git as a version control repository. Then, we are going to see how the continuous integration servers Jenkins, JetBrains TeamCity, and Atlassian Bamboo support Gradle.
Before we can see the support for Gradle in the several continuous integration servers, we must have a sample project. We are going to create a very simple Java project with a test class and add it to a Git repository, in this section.
We already created a Java project earlier. We are going to re-use the code in this chapter for our sample project. We want to have a test in our project, so that we can see how the continuous integration tools can handle test results. Finally, we want to have more than one artifact for our project; we want to have a JAR file with the compiled classes, source code, and Javadoc generated documentation.
We first create a build.gradle
file in a directory, with the following contents:
// We create a Java project so we need the Java plugin apply plugin: 'java' // Set base name for archives. archivesBaseName = 'gradle-sample' // Version of the project. version = '1.0' // Definine Maven central repository for downloading // dependencies. repositories { mavenCentral() } // We have a single dependency on JUnit // for the testCompile configuration dependencies { testCompile 'junit:junit:[4.8,)' } // Extra task to create a JAR file with the sources. task sourcesJar(type: Jar) { classifier = 'sources' from sourceSets.main.allSource } // Extra task to create a JAR file with Javadoc // generated documentation. task docJar(type: Jar, dependsOn: javadoc) { classifier = 'docs' from javadoc.destinationDir } // Add extra JAR file to the list of artifacts // for this project. artifacts { archives sourcesJar archives docJar }
Next, we create three Java source files in the src/main/java/gradle/sample
directory. First, we have an interface with a single method to return a welcome message:
// File: src/main/java/gradle/sample/ReadWelcomeMessage.java package gradle.sample; /** * Read welcome message from source and return value. */ public interface ReadWelcomeMessage { /** * @return Welcome message */ String getWelcomeMessage(); }
Next, we create an implementation of this interface and return a String
value:
// File: src/main/java/gradle/sample/ReadWelcomeMessageImpl.java package gradle.sample; import java.util.ResourceBundle; /** * Simple implementation to return welcome message. */ public class ReadWelcomeMessageImpl implements ReadWelcomeMessage { public ReadWelcomeMessageImpl() { } /** * Return "Welcome to Gradle." String value. * * @return Welcome to Gradle. */ public String getWelcomeMessage() { return "Welcome to Gradle."; } }
Finally, we have a Java application class that uses the interface and implementation class we already added:
// File: src/main/java/gradle/sample/SampleApp.java package gradle.sample; import java.util.ResourceBundle; public class SampleApp { public SampleApp() { } public static void main(final String[] arguments) { final SampleApp app = new SampleApp(); app.welcomeMessage(); } public void welcomeMessage() { final String welcomeMessage = readMessage(); showMessage(welcomeMessage); } private String readMessage() { final ReadWelcomeMessage reader = new ReadWelcomeMessageImpl(); final String message = reader.getWelcomeMessage(); return message; } private void showMessage(final String message) { System.out.println(message); } }
Let's create a test to verify that our ReadWelcomeMessageImpl
class returns the expected String
value. We add the file ReadWelcomeMessageTest.java
in the directory src/test/java/gradle/sample
:
// File: src/test/gradle/sample/ReadWelcomeMessageTest.java package gradle.sample; import org.junit.Assert; import org.junit.Test; public class ReadWelcomeMessageTest { @Test public void readWelcomeMessage() { final ReadWelcomeMessage reader = new ReadWelcomeMessageImpl(); final String realMessage = reader.getWelcomeMessage(); final String expectedMessage = "Welcome to Gradle."; Assert.assertEquals("Get text from implementation", expectedMessage, realMessage); } }
To check if everything is okay, we run Gradle with the build
task. We should see the following output:
$ gradle build :compileJava :processResources UP-TO-DATE :classes :javadoc :docJar :jar :sourcesJar :assemble :compileTestJava :processTestResources UP-TO-DATE :testClasses :test :check :build BUILD SUCCESSFUL Total time: 9.031 secs
We have all the source code, so let's put it in a version control repository. We can use any version control system we want, as long as the continuous integration server supports the version control system. We create a Git repository for our example, because it is easy to set up a local repository and then to use it in the continuous integration tools. In order to use Git, we must have it installed on our computers. We create a new Git repository in the current project directory, with the init
command in Git:
$ git init Initialized empty Git repository in /Users/mrhaki/Projects/java-project
Next, we add the file to the Git staging area, with the add
command:
$ git add .
We commit the code to the repository, with the commit
command in Git:
$ git commit -m "First commit." [master (root-commit) e80a23f] First commit. 6 files changed, 121 insertions(+), 0 deletions(-) create mode 100644 build.gradle create mode 100644 src/main/java/gradle/sample/ReadWelcomeMessage.java create mode 100644 src/main/java/gradle/sample/ReadWelcomeMessageImpl.java create mode 100644 src/main/java/gradle/sample/SampleApp.java create mode 100644 src/test/java/gradle/sample/ReadWelcomeMessageTest.java
Our project is ready to be used in the continuous integration tools.
3.16.137.38