brett@3riverdev.com 260.349.5732

Deploy an existing Java app to Heroku without the project template

Posted on December 26 , 2012

Heroku has some great project templates usable as a starting point for a new application (here).  However, it’s not 100% clear how to deploy an existing Java web application, without starting from their templates.  Attempting to use your project as-is will sometimes prevent the Dynos from running or cause other startup issues.

Assuming you have an existing Maven application and WAR packaging, the following are the few steps necessary to deploy to Heroku.

  1. At the root of your project, create a ‘system.properties’ file with the following contents:
    java.runtime.version=1.6
  2. At the root of your project, create a ‘Procfile’ file with the following contents:
    web: java $JAVA_OPTS -Dspring.profiles.active=prod -jar target/dependency/webapp-runner.jar --port $PORT target/*.war
  3. In your pom.xml file, add the following:
    <build>
    	<plugins>
    		<plugin>
    			<artifactId>maven-compiler-plugin</artifactId>
    			<configuration>
    				<source>1.6</source>
    				<target>1.6</target>
    			</configuration>
    		</plugin>
    		<plugin>
    			<artifactId>maven-war-plugin</artifactId>
    			<version>2.2</version>
    		</plugin>
    		<plugin>
    			<groupId>org.apache.maven.plugins</groupId>
    			<artifactId>maven-dependency-plugin</artifactId>
    			<version>2.4</version>
    			<executions>
    				<execution>
    					<phase>package</phase>
    					<goals>
    						<goal>copy</goal>
    					</goals>
    					<configuration>
    						<artifactItems>
    							<artifactItem>
    								<groupId>com.github.jsimone</groupId>
    								<artifactId>webapp-runner</artifactId>
    								<version>7.0.27.1</version>
    								<destFileName>webapp-runner.jar</destFileName>
    							</artifactItem>
    						</artifactItems>
    					</configuration>
    				</execution>
    			</executions>
    		</plugin>
    	</plugins>
    </build>
  4. Finally, add the following dependency to the pom.xml file:
    <dependency>
    	<groupId>com.github.jsimone</groupId>
    	<artifactId>webapp-runner</artifactId>
    	<version>7.0.27.1</version>
    	<scope>provided</scope>
    </dependency>

And that’s it!  Deploy your entire project to Heroku and it should automatically set everything up correctly without having to use the templates.

Leave a Reply

Your email address will not be published. Required fields are marked *