Heroku
From Documentation
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 allows Maven Project. Follow this guide to create a ZK-Maven Project.
ZK Maven Project With Jetty Server Embedded
- Modify pom.xml to add embedded Jetty web container for running in Heroku.
- Create a file named Procfile in Project root to tell Heroku how to executed the application
<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
ZK Maven Project With Tomcat Server Embedded
- Create Main.java in src/main/java with package launch to start embedded Tomcat server.
- Modify pom.xml.
- a) Remove <package>war</package> line to make Main.class work.
- b) Add embedded Tomcat web container for running in Heroku.
- Create a file named Procfile in Project root to tell Heroku how to executed the application
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();
}
}
<!-- <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 is used for JSP, if you application don't contain JSP, just 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
Deploy to Heroku
- Initialize git repository.
- Create a .gitignore file to ignore target folder where Maven package the project war and zip file.
- Commit project source code to git repository.
- Create Heroku app on the Cedar stack and Heroku will create a app with a random AppName that can be changed later:
- Deploy the code
- Rename the AppName (Optional)
- Visit the application
cd projectName
git init
target
git add .
git commit -m "CommitMessageNote"
heroku create --stack cedar
git push heroku master
heroku rename newname
heroku open
Update Source Code changes to Heroku
- Commit project source code to git repository.
- Deploy the code
- Visit the changes
git add .
git commit -m "CommitMessageNote"
git push heroku master
heroku open
Sample
- Download the sample maven project named heroku_zksandbox.zip.
- You can visit it at http://zksandbox.herokuapp.com/.