How to Create your First Java EE Application for Heroku

1) Install the Heroku Eclipse plugin in Eclipse IDE.

2) Setup your account.

3) Create a new project from the “Blank Heroku App” template.

4) Add Tomcat to your project:

<!-- Only if using JSPs -->
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jasper</artifactId>
  <version>7.0.30</version>
</dependency>
<!-- Only if using JSPs -->
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jasper-el</artifactId>
  <version>7.0.30</version>
</dependency>
<!-- Only if using JSPs -->
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jsp-api</artifactId>
  <version>7.0.30</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-servlet-api</artifactId>
  <version>7.0.30</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-core</artifactId>
  <version>7.0.30</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <version>7.0.30</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-logging-juli</artifactId>
  <version>7.0.30</version>
</dependency>

5) Create a launcher class:

package br.eti.fernandoribeiro.sample.javaee;

import java.io.File;

import org.apache.catalina.startup.Tomcat;

public final class Main {

  public static void main(final String[] args) throws Exception {
    final Tomcat tomcat = new Tomcat();

    tomcat.setPort(Integer.valueOf(System.getenv("PORT")));

    tomcat.addWebapp("/", new File("src/main/webapp/").getAbsolutePath());

    tomcat.start();

    tomcat.getServer().await();
  }

}

6) Add the Application Assembler Maven Plugin to your project:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>appassembler-maven-plugin</artifactId>
  <version>1.2.2</version>
  <configuration>
    <programs>
      <program>
        <mainClass>br.eti.fernandoribeiro.sample.javaee.Main</mainClass>
        <name>webapp</name>
      </program>
    </programs>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>assemble</goal>
      </goals>
    </execution>
  </executions>
</plugin>

7) Create a procfile file:

web: sh target/bin/webapp

8) Deploy.

You can find more information here.

A sample is available here.

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