Create a simple Java Web App using Maven and Deploy to Heroku


This blog post will feature on creating a simple web application with a Maven Archetype plugin. The web application will:

  • Run in an embedded web application server (Jetty Server)
  • Have some dependencies added
  • Contain a simple servlet
  •  Generate a WAR file for deploying on heroku

1. Using Maven archetype:generate to create a web app which has an embedded Jetty Server (jetty-archetype-assembler) and a java class which starts the Jetty Server.

mvn archetype:generate -DarchetypeGroupId=org.mortbay.jetty.archetype -DarchetypeArtifactId=jetty-archetype-assembler -DarchetypeVersion=7.5.1.v20110908 -DgroupId=com.heroku.webapp -DartifactId=heroku1
thanks to John Simone & heroku team for working with Jetty team in creating this jetty-archetype-assembler!

2. This will generate a Java web project with standard web application structure. Inspect the pom.xml file and note that it has a maven app assembler plugin which will generate a script that sets up the required class path and runs the web application.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<generateRepository>false</generateRepository>
<programs>
<program>
<mainClass>com.heroku.webapp.Main</mainClass>
<name>webapp</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>

Important –

Use your favorite editor and delete this line from the pom.xml file:

<generateRepository>false</generateRepository>

This is necessary because this archetype relies on the local maven repository being present at runtime, which is not the case once the app is deployed on heroku.

3. Compile the project using mvn compile.

mvn compile
[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building heroku1 1.0-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-resources-plugin:2.4.3:resources (default-resources) @ heroku1 —
[INFO] Using ‘UTF-8′ encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] — maven-compiler-plugin:2.3.2:compile (default-compile) @ heroku1 —
[INFO] Compiling 1 source file to /Users/ssharaf/heroku/heroku1/target/classes
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 2.079s
[INFO] Finished at: Thu Nov 17 20:40:08 PST 2011
[INFO] Final Memory: 8M/81M
[INFO] ————————————————————————

 

4. Now we can package it using mvn package:

mvn package

Output –

[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building heroku1 1.0-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-resources-plugin:2.4.3:resources (default-resources) @ heroku1 —
[INFO] Using ‘UTF-8′ encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] — maven-compiler-plugin:2.3.2:compile (default-compile) @ heroku1 —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] — maven-resources-plugin:2.4.3:testResources (default-testResources) @ heroku1 —
[INFO] Using ‘UTF-8′ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/ssharaf/heroku/heroku1/src/test/resources
[INFO]
[INFO] — maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ heroku1 —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] — maven-surefire-plugin:2.7.2:test (default-test) @ heroku1 —
[INFO] No tests to run.
[INFO] Surefire report directory: /Users/ssharaf/heroku/heroku1/target/surefire-reports

——————————————————-
T E S T S
——————————————————-
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] — maven-jar-plugin:2.3.1:jar (default-jar) @ heroku1 —
[INFO] Building jar: /Users/ssharaf/heroku/heroku1/target/heroku1-1.0-SNAPSHOT.jar
[INFO]
[INFO] — appassembler-maven-plugin:1.1.1:assemble (default) @ heroku1 —
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 2.496s
[INFO] Finished at: Thu Nov 17 20:41:15 PST 2011
[INFO] Final Memory: 5M/81M
[INFO] ————————————————————————

 

5. Next we create a Procfile which will define the process we want to run i.e. our main web app

I use VI editor on MAC.

e.g. vi Procfile

web:sh target/bin/webapp

 

6. Initialize the Git Repository by typing:

git init

now add the files using:

git add .

now commit the files to the repository using:

git commit -m “added heroku1 to the heroku cloud”

7. Now we are ready to create the Cedar Stack on heroku

$heroku create -s cedar

this will provision an application with some random name like cold-summer-876. Also it will configure git repository on heroku.

You can rename this to your app by using the heroku rename command.

e.g. $heroku rename heroku1

8. Lastly push the local git repository to heroku.

git push heroku master

This will take the whole git repository and upload it to heroku. Heroku recognizes it as java application using POM file and will use the dependencies defined to download appropriate artifacts. It will create a slug, which is basically a run time application instance. This slug gets replicated to a new dyno when we scale the app.

9. Now we can visit the app by using the command:

$heroku open

10. Some simple management commands to manage the app:

a. $heroku ps (shows the running processes)

b. $heroku log -t (will tail the logs)

c. $heroku scale web=2 (scale the web processes to 2)

d. $heroku releases (will show the last 2 releases)

Rel Change By When
—- ———————- ———- ———-
v33 Deploy d075337 ssharaf@salesforce.. 12 minutes ago
v32 Deploy 973f6e4 ssharaf@salesforce.. 16 minutes ago

Note that the content in the blog post was inspired by the tutorials on github by James Ward.

 

About these ads

, , ,

  1. Leave a comment

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: