We have just added the well known pet-clinic Java application to the Stackato App Store, and it works well!
One thing about Java applications is that you need to generate byte code to execute it, and every Java developer knows that you should never commit your byte code to source control because of the size (for pet-clinic, 500KB of sources become 40MB of compiled jars/wars). For this reason, with Stackato demo applications we generally recommend that Java developers do the extra step of compiling the bytecode before deployment.
With the preliminary support of Heroku Buildpacks in Stackato 1.2, there's now a way to make compilation happen as part of the app deployment.
So let's explain both methods of deploying Java to Stackato.
Building it locally
The first way is to build the application before pushing to Stackato. This method has been available all along in Stackato when using the 'java_web' and 'spring' frameworks:
- Install Maven on your local system if you don't have it
Build the application
mvn clean package
Push it to Stackato
stackato push -n
That's all.
However, if your application uses a database like pet-clinic does, and if there are maven tests on this database, you will need to set up a local database to pass these tests. You could use the '-Dmaven.test.skip' argument to skip tests, but this is not recommended.
maven clean package -Dmaven.test.skip=true
It would be nice to run these tests against the actual database we'll be using. We can do that using the 'buildpack' framework.
Using a Java Buildpack
Heroku Buildpack support is new in Stackato 1.2. Heroku introduced this system as a way for users to add custom frameworks to their PaaS. Stackato has other ways of customizing the application environment, but buildpacks have become popular, so we decided to emulate them in the Stackato environment.
In the case of our demo Java application, the Java buildpack allows the application bytecode to be built on Stackato instead of on your local machine. The Java buildpack also includes OpenJDK if you want to run a simple java application.
For the pet-clinic application for example, we need a Java-based server to run the application. To do that, we will use Jetty. Jetty is a light Java application server. It includes a Jetty Runner. The good thing about Jetty is that you can run your Java web application with a simple command just by providing the war file:
java -jar jetty-runner.jar application.war
Advantages of using the buildpack framework?
- You don't need Maven on your local machine
- The application builds on the PaaS instead of on your machine
- You don't have to push all the byte code
- You don't need to set up a special environment for tests.
- You don't have to rely on Tomcat. You can specify another web server (such as Jetty or Netty).
How does it work with Stackato?
It is really easy! You only need to modify three files.
First, in 'stackato.yml' you will need to define the framework type and to provide the buildpack url. Here is the pet-clinic stackato.yml:
name: pet-clinic
mem: 512M
framework:
type: buildpack
env:
BUILDPACK_URL: https://github.com/heroku/heroku-buildpack-java.git
services:
mysql-spring: mysql
As it is a buildpack application, you also need to create a Procfile in which you declare how you want your application to be executed. Here is the one from pet-clinic.
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
And finally, add the jetty dependency in your pom.xml
in order to run your application.
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>7.5.4.v20111024</version>
<destFileName>jetty-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And that's it! Target your Stackato server/cluster with the client, login, and push your application.
So there you have it - more ways to get your applications working in Stackato. You choose the one that fits your development style.