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

  1. Modify pom.xml to add embedded Jetty web container for running in Heroku.
  2. <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>
    
  3. Create a file named Procfile in Project root to tell Heroku how to executed the application
  4. web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
    

ZK Maven Project With Tomcat Server Embedded

  1. Create Main.java in src/main/java with package launch to start embedded Tomcat server.
  2. 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();
    	}
    }
    
  3. Modify pom.xml.
  4. a) Remove <package>war</package> line to make Main.class work.
    b) Add embedded Tomcat web container for running in 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 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>
    ...
    
  5. Create a file named Procfile in Project root to tell Heroku how to executed the application
  6. web: sh target/bin/webapp
    

Deploy to Heroku

  1. Initialize git repository.
  2. cd projectName
    git init
    
  3. Create a .gitignore file to ignore target folder where Maven package the project war and zip file.
  4. target
    
  5. Commit project source code to git repository.
  6. git add .
    git commit -m "CommitMessageNote"
    
  7. Create Heroku app on the Cedar stack and Heroku will create a app with a random AppName that can be changed later:
  8. heroku create --stack cedar
    
  9. Deploy the code
  10. git push heroku master
    
  11. Rename the AppName (Optional)
  12. heroku rename newname
    
  13. Visit the application
  14. heroku open
    

Update Source Code changes to Heroku

  1. Commit project source code to git repository.
  2. git add .
    git commit -m "CommitMessageNote"
    
  3. Deploy the code
  4. git push heroku master
    
  5. Visit the changes
  6. heroku open
    

Sample



Last Update : 2011/12/30

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.