Building Developer Cloud Applications Using Gradle

The Spring Boot Getting Started application includes build files for both Maven and Gradle. Given that the Oracle Developer Cloud Service also supports Gradle I thought this would be a good opportunity to demonstrate it in action.

I’m going to pick up where I left off in my previous entry, simply creating a new build that will use Gradle instead of Maven.

Step 1: Configure for Deployment to the Application Container Cloud Service (ACCS)

manifest.json

This file already exists and doesn’t need to change.

Deployment Archive

Just like with Maven, we need to instruct Gradle to package our application into a zip file for deployment to ACCS. With Gradle, this is achieved using the Zip plugin.

Edit build.gradle and add the following:

apply plugin: 'spring-boot'

task zip(type: Zip) {
    from jar.outputs.files   
    from( 'manifest.json' ) 
}

Step 2: Commit and Push the Changes

You know the drill.

Step 3: Create the Build

I could modify the existing Maven build, but instead I’m going to create a new build from the existing build.

Create a new Build Job by copying the Spring Boot GS. Name it something like Spring Boot GS (Gradle).

g00_copy-job

Switch to the Build Steps tab, delete the Invoke Maven 3 step and add a Gradle build step. Set the Tasks to build zip. Everything else can remain as is:

g01_build-step

Switch to the Post Build tab select Archive the Artifacts. Change the Files to Archive to build/distributions/*.zip:

g02_post-build

Save the configuration and click Build Now.

The build fails with the following:

> Connect to repo1.maven.org:443 [repo1.maven.org/151.101.68.209] failed: Connection timed out

The Gradle plug-in doesn’t (yet) pick up the proxy settings from the build engine so we need to provide this information to Gradle. The Developer Cloud Service exposes environment variables that include proxy information:

g03_env

Using these variables we can dynamically create a gradle.properties file as follows (see Accessing the web via a proxy):

#
# Generate a gradle.properties file for the DevCS environment running the build...
#
echo "systemProp.http.proxyHost="$HTTPS_PROXY_HOST >> gradle.properties
echo "systemProp.http.proxyPort="$HTTP_PROXY_PORT >> gradle.properties
echo "systemProp.http.nonProxyHosts="$NO_PROXY >> gradle.properties
echo "systemProp.https.proxyHost="$HTTPS_PROXY_HOST >> gradle.properties
echo "systemProp.https.proxyPort="$HTTPS_PROXY_PORT >> gradle.properties
echo "systemProp.http.nonProxyHosts="$NO_PROXY >> gradle.properties

I’m going to add these commands to a shell script, create_gradle_properties.sh, which I will execute before I call the Gradle build.

Once the script is created and pushed to the repository, return to the Build Steps tab. Unfortunately, there’s not a way to insert a step before an existing step, so delete the Invoke Gradle build step and add an Execute shell step. Set the Command to sh create_gradle_properties.sh. Then add back the Invoke Gradle step as we did before:

g04_shell

Click Save and Build Now (if the Build Now button is disabled, click the Enable Build button).

Yikes, another failed build. It seems Gradle is configured to run an additional integration test which Maven was not. So wee also need to update the HelloControllerIT.java with our new greeting:

   @Test
    public void getHello() throws Exception {
        ResponseEntity<String> response = template.getForEntity(base.toString(),
                String.class);
        assertThat(response.getBody(), equalTo("Greetings from Spring Boot running on ACCS!"));
    }

Commit and push to GitHub, which should trigger the build again (third time’s a charm, right?).

g05_build-history

Advertisements

2 thoughts on “Building Developer Cloud Applications Using Gradle

  1. S Das

    Dear Brian,
    I am still getting an error in connecting to maven central repo (both https ans http) even after setting the proxy params for gradle using sh as you described. Any suggestions please?
    Thanks in advance.
    Regards,
    Das

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s