📣 Attention Salesforce Certified Trailblazers! Link your Trailhead and Webassessor accounts and maintain your credentials by December 14th. Learn more.
close
trailhead

Modify and Add Services to a Java App on Heroku

Running Your App Locally

Any app you run on Heroku eventually requires new features and enhancements. As you make these changes, you want the ability to run your Java process locally so you can easily debug problems, run tests, and confirm that your app is working. In this unit, you learn how to run the sample Java app in the same way that it runs on the Heroku platform. Then you make changes to the app in your local environment and deploy them to Heroku.

Heroku recognizes a Java app by the existence of a pom.xml file in the root directory. For your own apps, you can create one by using the mvn archetype:create command. The demo app you deployed already has a pom.xml ( see it on GitHub). Here’s an excerpt:

<dependencies>    
   <dependency>      
    <groupId>org.springframework.boot</groupId>      
    <artifactId>spring-boot-starter-actuator</artifactId>    
   </dependency>    
   <dependency>      
    <groupId>org.springframework.boot</groupId>      
    <artifactId>spring-boot-starter-web</artifactId>    
   </dependency>    
   ...
</dependencies>

The pom.xml file specifies dependencies that should be installed with your application. When an app is deployed, Heroku reads this file and installs the dependencies using the mvnw clean install command.

Another file, system.properties, determines the version of Java to use (Heroku supports many different versions). The contents of this file, which is optional, is quite straightforward:

java.runtime.version=1.8

Run the command ./mvnw clean install in your local directory to install and run Maven (if you’re using Windows, you need to use the mvnw.cmd command instead of ./mvnw).

./mvnw clean install

It installs the dependencies, and prepares your system for running the app locally. Note that this app requires Java 8, but you can develop your own apps using a different version of Java.

Now start your application locally using the heroku local command, which was installed as part of the Heroku CLI.

heroku local

Heroku local sample output

Just like in the Heroku cloud, heroku local examines the Procfile to determine what command to run.

Leave this running, and open http://localhost:5000 with your web browser. You see your app running locally.

To stop the app from running locally, go back to your terminal window and press Ctrl+C to exit.

Push Local Changes

In this step you learn how to propagate a local change to the application through to Heroku. As an example, you modify the application to display a new HTML page.

Modify the src/main/java/com/example/Main.java file by adding the following method to the Main class.

@RequestMapping("/hello")
String hello(Map<String, Object> model) {
  model.put("message", "Welcome to my app!");
  return "hello";
}

Then create a src/main/resources/templates/hello.html file with these contents.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 
th:replace="~{fragments/layout :: layout (~{::body},'hello')}">
<body>  
  <div class="container">    
   <p th:text="${message}"/>  
  </div>
</body>
</html>

Now test locally by running these commands (remember to use mvnw.cmd if you’re using Windows).

./mvnw clean install
heroku local web

Point your browser to your application on the /hello route at http://localhost:5000/hello. You see your welcome message displayed.

Now deploy these changes to Heroku. Almost every deploy to Heroku follows the same pattern.

First, add the modified files to the local Git repository.

git add .

Then commit the changes to the repository.

git commit -m "Demo"

Push the changes to the heroku remote repository of your app.

git push heroku master

Finally, check that everything is working by opening the application on the /hello route.

heroku open hello

You see the same text in your web browser as you saw when the app was running locally.

Provision an Add-on

Add-ons are third-party cloud services that provide out-of-the-box additional services for your application, from persistence through logging to monitoring and more. Your Java app automatically comes installed with an add-on: a Postgres database. You can learn more about how to manage your Postgres database on the Heroku Postgres Dev Center. 

Here, we’re going to demonstrate how to add an additional add-on for logging.

By default, Heroku stores 1,500 lines of logs from your application. However, it makes the full log stream available as a service—and several add-on providers have written logging services that provide things such as log persistence, search, and email and SMS alerts.

In this step you provision one of these logging add-ons, Papertrail.

Provision the papertrail logging add-on.

heroku addons:create papertrail

Papertrail output

To help with abuse prevention, provisioning an add-on requires account verification. If your account has not been verified, you are directed to visit the verification site.

To see this particular add-on in action, visit your application’s Heroku URL a few times. Each visit generates more log messages, which should now get routed to the papertrail add-on. Visit the papertrail console to see the log messages.

heroku addons:open papertrail

Your browser opens a Papertrail web console, showing the latest log events. The interface lets you search and set up alerts.

list of logged events

Congratulations! Your Java app is ready for production. 

Click Verify Step to complete the project.

Resources

retargeting