Heroku"

From Documentation
m (correct highlight (via JWB))
 
(16 intermediate revisions by 3 users not shown)
Line 5: Line 5:
  
 
= Deploy ZK Project to Heroku =
 
= Deploy ZK Project to Heroku =
Heroku '''only''' allow '''Maven Project'''. Follow the [http://books.zkoss.org/wiki/ZK_Installation_Guide/Setting_up_IDE/Maven/Create_and_Run_ZK_Application_with_Maven_Archetype guide] to create a ZK-Maven Project.
+
Heroku '''only''' allow '''Maven Projects'''. Follow this [http://books.zkoss.org/wiki/ZK_Installation_Guide/Setting_up_IDE/Maven/Create_and_Run_ZK_Application_with_Maven_Archetype 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 ==
 
== ZK Maven Project With Jetty Server Embedded ==
 
<ol>
 
<ol>
<li>Modify '''pom.xml''' to add embedded '''Jetty''' web container for running in Heroku.</li>
+
<li>Modify '''pom.xml''' to add embedded '''Jetty''' web container for running on Heroku.</li>
 
<source lang="xml">
 
<source lang="xml">
 
<plugin>
 
<plugin>
Line 35: Line 35:
 
</plugin>
 
</plugin>
 
</source>
 
</source>
<li>Create a file named '''Procfile''' in Project root to tell Heroku how to executed the application</li>
+
<li>Create a file named '''Procfile''' in Project root to tell Heroku how to execute the application</li>
 
<source lang="bash">
 
<source lang="bash">
 
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
 
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
Line 74: Line 74:
 
</source>
 
</source>
 
<li>Modify '''pom.xml'''.</li>
 
<li>Modify '''pom.xml'''.</li>
:a) '''Remove''' '''''<package>war</package>''''' line to make Main.class work.
+
:a) '''Remove''' '''''<package>war</package>''''' line to make ''Main.class'' work  
:b) Add embedded '''Tomcat''' web container for running in Heroku.
+
:b) Add embedded '''Tomcat''' web container for running on Heroku.
<source lang="xml" high="1">
+
<source lang="xml" highlight="1">
 
<!-- <packaging>war</packaging> -->
 
<!-- <packaging>war</packaging> -->
 
...
 
...
Line 94: Line 94:
 
<version>7.0.22</version>
 
<version>7.0.22</version>
 
</dependency>
 
</dependency>
<!-- The following three dependencies is used for JSP, if you application don't contain JSP, just remove them -->
+
<!-- The following three dependencies are used for JSP. If you application doesn't contain JSP, simply remove them -->
 
<dependency>
 
<dependency>
 
<groupId>org.apache.tomcat</groupId>
 
<groupId>org.apache.tomcat</groupId>
Line 111: Line 111:
 
</dependency>
 
</dependency>
 
...
 
...
<!-- Add the plugin to start Main.class -->
+
<!-- Add the plugin to start ''Main.class'' -->
 
<plugin>
 
<plugin>
 
<groupId>org.codehaus.mojo</groupId>
 
<groupId>org.codehaus.mojo</groupId>
Line 136: Line 136:
 
...
 
...
 
</source>
 
</source>
<li>Create a file named '''Procfile''' in Project root to tell Heroku how to executed the application</li>
+
<li>Create a file named '''Procfile''' in Project root to tell Heroku how to execute the application</li>
 
<source lang="bash">
 
<source lang="bash">
 
web: sh target/bin/webapp
 
web: sh target/bin/webapp
Line 143: Line 143:
  
 
== Deploy to Heroku ==
 
== Deploy to Heroku ==
 +
Once the server is ready, we can deploy to Heroku.
 
<ol>
 
<ol>
 
<li>Initialize git repository.</li>
 
<li>Initialize git repository.</li>
Line 149: Line 150:
 
git init
 
git init
 
</source>
 
</source>
<li>Create a '''.gitignore''' file to ignore target folder where Maven package the project war and zip file.</li>
+
<li>Create a <b>.gitignore</b> file to ignore all the files under '''target''' folder as these files are unnecessary to be managed and included by git repository.</li>
 
<source lang="bash">
 
<source lang="bash">
 
target
 
target
Line 158: Line 159:
 
git commit -m "CommitMessageNote"
 
git commit -m "CommitMessageNote"
 
</source>
 
</source>
<li>Create Heroku app on the Cedar stack and Heroku will create a app with a random AppName that can be changed later:</li>
+
<li>Create Heroku app on Cedar stack and Heroku will create an app with a random ''AppName'' that can be changed later:</li>
 
<source lang="bash">
 
<source lang="bash">
 
heroku create --stack cedar
 
heroku create --stack cedar
Line 166: Line 167:
 
git push heroku master
 
git push heroku master
 
</source>
 
</source>
<li>Rename the AppName (Optional)</li>
+
<li>Rename the ''AppName'' (Optional)</li>
 
<source lang="bash">
 
<source lang="bash">
 
heroku rename newname
 
heroku rename newname
Line 177: Line 178:
  
 
== Update Source Code changes to Heroku ==
 
== Update Source Code changes to Heroku ==
 +
If you wish to make changes to the app deployed on Heroku, follow the steps below:
 
<ol>
 
<ol>
 
<li>Commit project source code to git repository.</li>
 
<li>Commit project source code to git repository.</li>
Line 194: Line 196:
  
 
= Sample =
 
= Sample =
*[http://sourceforge.net/projects/zk1/files/ZK%20for%20Heroku/ Download] the sample maven project named heroku_zksandbox.zip.
+
*[http://sourceforge.net/projects/zkforge/files/Small%20Talks/ZK%20Sandbox%20for%20Heroku/ Download] the sample maven project named heroku_zksandbox.zip.
*You can visit it at [http://zksandbox.herokuapp.com/ http://zksandbox.herokuapp.com/].
+
*Sandbox sample is deployed on Heroku at [http://zksandbox.herokuapp.com/ http://zksandbox.herokuapp.com/].
  
 +
= Version History =
 +
{{LastUpdated}}
 
{{ZKInstallationGuidePageFooter}}
 
{{ZKInstallationGuidePageFooter}}

Latest revision as of 12:39, 19 January 2022


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

  1. Modify pom.xml to add embedded Jetty web container for running on 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 execute 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 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>
    ...
    
  5. Create a file named Procfile in Project root to tell Heroku how to execute the application
  6. web: sh target/bin/webapp
    

Deploy to Heroku

Once the server is ready, we can deploy to Heroku.

  1. Initialize git repository.
  2. cd projectName
    git init
    
  3. Create a .gitignore file to ignore all the files under target folder as these files are unnecessary to be managed and included by git repository.
  4. target
    
  5. Commit project source code to git repository.
  6. git add .
    git commit -m "CommitMessageNote"
    
  7. Create Heroku app on Cedar stack and Heroku will create an 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

If you wish to make changes to the app deployed on Heroku, follow the steps below:

  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

Version History

Last Update : 2022/01/19



Last Update : 2022/01/19

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