Tag Archives: CloudFoundry

Spring Roo, MongoDB and CloudFoundry on Mac OS X

For an university project I currently need to develop an application in Java using Spring Roo and MongoDB which should be deployed to Cloud Foundry. Spring Roo is tool for rapid development in Java and Cloud Foundry is a Platform as a Service (PaaS) provider. In order to be able to deploy applications on Cloud Foundry, you need to create an account. It will take some time until it is activated, thus you should signup now.

Since installing the required components on OS X 10.7 is not as easy as the tutorials and getting started guides suggest, I decided to write down what I did to get it all working.

I will be using Homebrew to install components. If you haven’t installed it yet, you can do that by executing the following line in Terminal.app:

/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

I will be deploying my app to Could Foundry and therefore my application will not be executed on my notebook. However we need Spring Roo to generate the project, entities, packages and so on and we need the command line tools of Cloud Foundry to be able to upload our application and start, restart and stop it. I am not sure if MongoDB must be installed on the local machine, since I had it already installed. If an error pops up during the installation or configuration of Spring Roo you can easily install MongoDB using Homebrew.

brew install mongodb
sudo mkdir -p /data/db

If required, you can start MongoDB:

sudo /usr/local/Cellar/mongodb/2.0.5-x86_64/bin/mongod

Next we need to install Spring Roo:

brew install spring-roo

When the installation is finished we first need to navigate to the folder where we want to create the project in and than we can start the interactive command line tool of Roo:

mkdir -p ~./playground/roo-mongodb-cloudfoundry-howto
cd ~./playground/roo-mongodb-cloudfoundry-howto
roo

You are now using the Spring ROO CLI, which can be used to generate projects, entities, execute actions and so on. If want to return to Bash you just need to type exit.

project --topLevelPackage playground.usermanager

The Roo CLI has autocompletion built in, so you could type proj and then hit TAB to complete project. This also works for arguments.

Now we need to setup the MongoDB data store and the generation of the required MVC classes for our web interface.

mongo setup --cloudFoundry true
web mvc setup

We can now already start to create the entities:

entity mongo --class ~.domain.User
field string --fieldName name --notNull --class ~.User
field string --fieldName password --notNull --class ~.domain.User
repository mongo --interface ~.repository.UserRepository --entity ~.domain.User
service --interface ~.service.UserService --entity ~.domain.User

For this example I created an entity User with two string fields name and password as well as a repository and a service class. Since I only want to explain how to set things up this will do for our example.

It is time to package our little application.

web mvc all --package ~.web
perform package

When the perform package is executed for the first time this will take a while. It will be much faster in subsequent calls.

Since we are done with Roo for the moment we exit the Roo tool with exit and will now deploy it to Cloud Foundry.

Spring Roo has an Cloud Foundry add-on, however it does not work with the latest version of Roo and I did not want to manually install an older version of Roo so I decided use the tools provided by Cloud Foundry.

First of all we need to install the Cloud Foundry CLI tool using RubyGems.

gem install vmc

Hopefully your account on Cloud Foundry is activated by now, since you need to login:

vmc login

You have two possibilities to deploy an application. The easier one is to directly deploy your application to Cloud Foundry, the other one is to deploy it to a virtual machine on your local computer. The later is called Micro Cloud Foundry and you will find instructions on how to set it up after you logged in on Cloud Foundry.

Next you need to define the target of the deployment. If you decide to deploy directly to the public cloud you need to do:

vmc target api.cloudfoundry.com

If you rather want to deploy to a Micro Cloud you need to do: (of course you need to insert the name you choose for your Micro Cloud in that command)

vmc target api.[--INSERT-YOUR-DOMAIN--].cloudfoundry.me

However, I did not try deploying to a Micro Cloud so I can guarantee that it will work just out of the box.

Since Roo created the package in the ./target directory we first need to switch to that directory before we can push the application to the cloud. Before we push we need to create a new MongoDB service.

cd ./target
vmc create-service mongodb mongo-howto
vmc push

An interactive dialog pops up, which you need to complete in order to deploy your application to Cloud Foundry. It is important that you bind the service you created above (mongo-howto) to your application. This is how it looked liked for me:

Would you like to deploy from the current directory? [Yn]: 
Application Name: howto-wa 
Detected a Java SpringSource Spring Application, is this correct? [Yn]: 
Application Deployed URL [howto-wa.cloudfoundry.com]: 
Memory reservation (128M, 256M, 512M, 1G, 2G) [512M]: 
How many instances? [1]: 
Bind existing services to 'howto-wa'? [yN]: y
1: mongo-howto
2: mongo-opm
Which one?: 1
Bind another? [yN]: n
Create services to bind to 'howto-wa'? [yN]: 
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Binding Service [mongo-howto]: OK
Uploading Application:
  Checking for available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading (5K): OK   
Push Status: OK
Staging Application 'howto-wa': OK                                              
Starting Application 'howto-wa': OK

If everything was ok, you should now be able to open the Application Deployed URL you defined in the push dialog. Please note that I probably delete the application I deployed at some point. Therefore instead of linking to the URL I will post some screenshots of the resulting application below.

Whenever you change something in your application and want to redeploy it, you first need to regenerate the application in Roo:

web mvc all --package ~.web
perform package

Then you need to update the application on Cloud Foundry.

cd ./target
vmc update

That’s it.

Spring Roo on Cloud Foundry: Index

Spring Roo on Cloud Foundry: Create

Spring Roo on Cloud Foundry: List

I am aware that there are Eclipse plugins for both Spring Roo and Cloud Foundry. However, I abandon Eclipse, NetBeans and all other IDEs some time ago and I am currently using Sublime Text 2 and have no plan of switching back to an IDE. Therefore I decided to use the command line tools.