To deploy a Web project in Heroku I’ve followed the official documentation. Nevertheless, my project artifact (WAR) needed to run Jetty and the documentation didn’t specify that case.

 

First, I’ve configured the pom file to copy the jetty-runner library to the final artifact.

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.10</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>copy</goal>
						</goals>
						<configuration>
							<artifactItems>
								<artifactItem>
								    <groupId>org.eclipse.jetty</groupId>
								    <artifactId>jetty-runner</artifactId>
								    <version>9.4.6.v20170531</version>
									<destFileName>jetty-runner.jar</destFileName>
								</artifactItem>
							</artifactItems>
						</configuration>
					</execution>
				</executions>
			</plugin>

 

Then, I’ve created the Procfile file, where I specify how to execute the project:

web:    java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war

I also created a system.properties file, to set the Java version:

java.runtime.version=1.8.0_131

To configure the Heroku database in my project, I’ve created a specific configuration file and followed the official documentation:

	<bean class="java.net.URI" id="dbUrl">
		<constructor-arg value="#{systemEnvironment['DATABASE_URL']}" />
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url"
			value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + ':' + @dbUrl.getPort() + @dbUrl.getPath() }" />
		<property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }" />
		<property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }" />
	</bean>

To test the web application in local, I’ve used Tomcat and H2, for this reason I’ve needed to maintain two profiles: local and heroku by default (to deploy in production):

	<profiles>
		<profile>
			<id>local</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-antrun-plugin</artifactId>
						<executions>
							<execution>
								<phase>prepare-package</phase>
								<goals>
									<goal>run</goal>
								</goals>
								<configuration>
									<tasks>
										<echo>Using local properties</echo>
										<copy
											file="src/main/resources/conf/local/applicationContext-dataSource.xml"
											tofile="${project.build.outputDirectory}/spring/applicationContext-dataSource.xml" />
									</tasks>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
		<profile>
			<id>heroku</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-antrun-plugin</artifactId>
						<executions>
							<execution>
								<phase>prepare-package</phase>
								<goals>
									<goal>run</goal>
								</goals>
								<configuration>
									<tasks>
										<echo>Using heroku properties</echo>
										<copy
											file="src/main/resources/conf/heroku/applicationContext-dataSource.xml"
											tofile="${project.build.outputDirectory}/spring/applicationContext-dataSource.xml" />
									</tasks>
								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>