Deploying Spring Boot Groovy scripts as a Jar file in Cloud Foundry

This post is a step by step guide on deploying a Spring Boot application on Cloud Foundry using the spring jar feature introduced in 1.0.0.RC2.

If you read the Cloud Foundry documentation, they would claim that the proper way to deploy Spring Boot Groovy scripts is via:

spring grab *.groovy
cf push

Unfortunately, this approach is almost as effective as Sex Panther Cologne.

It fails badly when you try to include other starter packs, such as the websocket starter pack.

Using the jar approach outlined here allows us to overcome the errors that you might encounter in Cloud Foundry otherwise.

Configuring the Spring CLI

Follow the guide at http://projects.spring.io/spring-boot/docs/README.html My preferred way is via the GVM tool:

curl -s get.gvmtool.net | bash
gvm install springboot

Building and Running Your Application

Build Your App

Let’s add a Controller by creating a file called HelloController.groovy

import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping

@RestController
class HelloController {

    @RequestMapping('/')
    String index() {
        'Greetings from Spring Boot!'
    }

}

We can also add a directory called static and we’ll add a picture there:

static
└── images
    └── canada.jpg

Run Your App

We start up our application via

spring run HelloController.groovy

We can add more Controllers and classes and YOLO them onto Cloud Foundry if we wanted too.

Here is an example of a Spring boot and websocket app that is runnable and deployable via spring run *.groovy.

Navigate to localhost:8080 and we see the message ‘Greetings from Spring Boot’. Go to localhost:8080/images/canada.jpg and we see our lovely picture:

Screenshot 2014-02-28 04.15.12

Deploy to Cloud Foundry as a jar

Install Cloud Foundry CLI

First, we want to make sure you have the Cloud Foundry CLI installed. You can follow the instructions from http://docs.cloudfoundry.org/devguide/installcf/install-go-cli.html. My preferred method is just to call

brew install cloudfoundry-cli

Compile into a jar file via Spring Boot CLI

Next, compile your application into a jar file.

spring jar myapp.jar *.groovy static

You should see a file called myapp.jar that contains your groovy files and your static directory.

Push Your Application

Login to Cloud Foundry

cf login

Enter your credentials. (Get new ones from https://run.pivotal.io/ or http://www.anynines.com/)

Deploy your App.

cf push helloSpringJar -p myapp.jar

helloSpringJar is the name of my application, you should choose your own.

I can verify my application is running by visiting http://hellospringjar.cfapps.io/ and I should see the words ‘Greetings from Spring Boot’. ( Links probably won’t work after a while since my account is set to expire in 10 days )

I can also see my image is now available to me at http://http://hellospringjar.cfapps.io/images/canada.jpg

[Disclaimer: Despite what Spring.io Guides claim, responsible developers actually put code in packages, use Gradle and write automated tests. Just because it fits in a tweet doesn’t mean you should do it this way.]

1 thought on “Deploying Spring Boot Groovy scripts as a Jar file in Cloud Foundry

Leave a comment