Heroku
Get Your Heroku Ready
Refer to Getting Started with Heroku for getting Heroku ready. You should have Ruby, Git, and Heroku Gem installed and have applied for a Heroku account.
Deploy ZK Project to Heroku
Heroku only allow Maven Projects. Follow this guide to create a ZK-Maven Project. After you have created a ZK-Maven project, you can decide to run with either Jetty or Tomcat.
ZK Maven Project With Jetty Server Embedded
- Modify pom.xml to add embedded Jetty web container for running on Heroku.
<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>
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
</ol>
ZK Maven Project With Tomcat Server Embedded
- Create Main.java in src/main/java with package launch to start embedded Tomcat server.
package launch;
import java.io.File;
import org.apache.catalina.startup.Tomcat;
public class Main {
public static void main(String[] args) throws Exception {
String webappDirLocation = "src/main/webapp/";
Tomcat tomcat = new Tomcat();
// The port that we should run on can be set into an environment variable
// Look for that variable and default to 8080 if it isn't there.
String webPort = System.getenv("PORT");
if (webPort == null || webPort.isEmpty()) {
webPort = "8080";
}
tomcat.setPort(Integer.valueOf(webPort).intValue());
tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath());
tomcat.start();
tomcat.getServer().await();
}
}
a) Remove
b) Add embedded Tomcat web container for running on Heroku.
<!-- <packaging>war</packaging> -->
...
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.22</version>
</dependency>
<!-- The following three dependencies are used for JSP. If you application doesn't contain JSP, simply remove them -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>7.0.22</version>
</dependency>
...
<!-- Add the plugin to start ''Main.class'' -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>launch.Main</mainClass>
<name>webapp</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
...
web: sh target/bin/webapp
</ol>
Deploy to Heroku
Once the server is ready, we can deploy to Heroku.
- Initialize git repository.
cd projectName
git init
target
git add .
git commit -m "CommitMessageNote"
heroku create --stack cedar
git push heroku master
heroku rename newname
heroku open
</ol>
Update Source Code changes to Heroku
If you wish to make changes to the app deployed on Heroku, follow the steps below:
- Commit project source code to git repository.
git add .
git commit -m "CommitMessageNote"
git push heroku master
heroku open
</ol>
Sample
- Download the sample maven project named heroku_zksandbox.zip.
- Sandbox sample is deployed on Heroku at http://zksandbox.herokuapp.com/.
Comments