Next monday is Eclipse Demo camp. Since I am going to have to prepare something to demo, and since I decided its going to be showing off how easy it is now, to deploy your
Grails Apps from STS directly into
Cloud Foundry...
I thought it would be useful to write something up at the same time as something that can be used as 'how to' tutorial.
Here goes...
Installing STS / Grails / Cloud Foundry
Let's start setting up the stuff we'll need to develop Grails apps in STS and to deploy them to the cloud. Below is a list of the things we'll be installing and which versions.
- STS (2.7.0)
- Groovy Eclipse (2.5.1)
- Grails (1.3.7)
- Grails Support for STS (2.7.0)
- Cloudfoundry Support for STS (2.7.0)
All of the needed stuff should be installable from the STS 2.7.0 Dashboard.
So step 1 is getting the STS 2.7.0 release and installing it on your machine.
Once installed open it go to the Dashboard's extension page and install items 2..5 listed above.
Getting a CF account
I've already got one. Maybe you do too. But if you don't, you'll need to
signup for one, it is free.
Creating a Test Application
We'll be using an extremely simple 'gTunes' application. So, let's start by creating an empty Grails App.
Menu: File >> New >> Grails Project
If you aren't already in the Grails perspective, you'll get asked if you want to switch to it. click 'Yes'.
The app we just created is already fully functional, albeit not very interesting. Anyway... before we add some stuff to it to make it more interesting, lets see if we can deploy it, as is, to Cloud Foundry.
Creating a CF Server in STS
When you first start STS in a new workspace, you will get an instance of tc Server automatically setup. You could try running the Grails app on that, but for this Demo, we're going to want to try Cloud Foundry instead. We'll have to configure a Cloud Foundry 'Server' in STS to make this possible.
Cloud Foundry isn't a 'Server' really, but a "Platform as a Service" (PaaS) for deploying Web applications directly to "The Cloud". Nevertheless, it is represented as a Server in the STS UI. So Right click in the servers view and...
Choose 'Cloud Foundry' as the type of server. Note the 'Server's host-name' is by default set to 'localhost'. This may seem odd and a little confusing. Just realize that CF isn't really a 'sever' and the value of this field is just ignored, so you don't need to change it.
Click next and enter your credentials, then click 'Finish'.
You should now have a CF Server in the server's view.
Cloud Foundry isn't a 'real' server but PaaS service, so many of the 'standard' functions for Servers don't apply to CF. For example, you can't 'Stop' or 'Start' Cloud Foundry. This is why you'll see most of the buttons in the Server's view disabled for Cloud Foundry.
We are already 'Connected' to the Cloud Foundry account we have just configured. This is indicated by the green triangle in the Cloud server icon.
However, when you exit and restart STS the next time, you will start out in a 'Disconnected State'. Instead of a green triangle you'll see a blue square:
You can always Connect/Disconnect to/from the CF account by right-clicking on the CF server and selecting Connect/Disconnect.
Deploying Grails App to CF
The STS-UI provides a few different ways to add or remove apps to the CF server. The easiest one is probably to Drag and Drop the app you want to deploy onto the CF server. After you drop your app onto Cloud Foundry, you'll be presented with a Wizard Dialog asking you to configure some basic properties of the deployed app.
That's it. The app is now running in the cloud... and it can be accessed at
http://gtunes.cloudfoundry.com.
Of course the app isn't very interesting yet... It's just a welcome page with nothing on it. So... let's add something.
Modifying and Redeploying the App
Let's add a 'Song' domain class with a title and artist field. Right click on the 'Domain' node in the Project Explorer view and select 'New Domain Class'. Type the name of the Domain class and hit 'Finish'.
This will execute the Grails command "create-domain-class Song". The output of this command will be shown in the console view and the generated Song.groovy will be opened in the editor once the command finishes.
Let's add some data members to the Song class.
class Song {
String title //ADDED!
String artist //ADDED!
static constraints = {
}
}
Also let's create a controller for this domain class. Right click on the Song class in the Project Explorer and select "New Controller". The wizard that pops up will already have the name of the Song domain class filled in, so just click Finish.
Similar to the the 'New Domain Class' functionality, this executes the Grails command 'create-controller' and then opens up the generated controller class in the editor. We modify it slighly to use Grails's dynamic scaffolding to generate a fully functional web UI based on the domain class.
package gtunes
class SongController {
def scaffold = Song // CHANGED
}
OK, now let's check the results of our work.
The changes we made won't be automatically propagated to CF. We need to 'Restart' our app to make the changes appear on the Cloud Foundry URL.
To redeploy a changed app right click on the app in the server's view and select 'Restart'.
When you restart the App, you will notice in the console view that a 'grails war' command is being executed to build a war file.
After the app got restarted, it may take some time before this change actually becomes 'live' at the Cloud Foundry URL. Keep reloading the URL, you can expect the following stages:
- For some time you'll keep seeing the 'old content'.
- The app goes offline and you'll get a VCAP 404 'not found' error.
- The app comes back online serving the new content.
We now have a link to the newly added Song controller. Click on it to get a list of Songs, create new Songs etc.
It should be noted that you won't always see a grails war build happening. This is because the Grails STS tooling will try to just apply changes from the workspace incrementally to the last war build, instead of rebuilding the war file each time you make a change.
For example, if you just make some changes to an existing gsp file, domain class or controller, you will not see the war build. STS will simply repackage the result from the last war build with the changed files replaced in it.
As an example, we can add a "Genre" field to the Song domain class.
class Song {
String title
String artist
String genre //Added
}
Now restart the app again, you won't see a war build, but the changes should eventually propagate to the CF URL.
Links / Further Reading
Cloud Foundry:
SpringSource Tools Suite (STS) = the “one stop shop” for all the tools used in this tutorial. Download and use it for free.