Search code examples
javaspring-bootgradleherokuprocfile

Building a Heroku Procfile for Java Spring Boot Gradle project


My app runs perfectly fine locally and is deployed with Heroku, but I am struggling with building a Procfile that declares what command should be executed to start the application. I'm a new developer and could use some help with how the Linux process model works.

The Heroku documentation provides two Procfile examples for Java Spring Boot projects:

web: java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/demo-0.0.1-SNAPSHOT.jar

web: java $JAVA_OPTS -cp target/classes:target/dependency/* com.example.HelloWorld

I decided to try following the -cp example because I do not have any .jar files.

This is my current Procfile:

web: java $JAVA_OPTS -cp gradlew java/com/deepdraft/deep_draft/DeepDraftApplication.java procfile

During a developer interview recently I asked briefly about the Procfile and he suggested I refer to my "gradlew" file when writing the Procfile, but I didn't get a chance to ask if I was copying a command from this file or directing the Procfile to the command within the file. I chose the "gradlew" file to copy into the main class in my current Procfile as a guess. Here are the collected arguments at the bottom of the gradlew file. gradlew

I have tried putting the main class in the Procfile by reference (com.deepdraft.deep_draft.DeepDraftApplication) and by the path (java/com/deepdraft/deep_draft/DeepDraftApplication.java). Both Procfiles resulted in a log that said "Error: Could not find or load main class." Here is a photo of the full log. logs

But honestly I'm not sure why the Procfile needs me to write a script that copies files from one directory to the other. I am just trying this out because the Heroku documentation used this Procfile as an example for SpringBoot projects.

Sorry if this information is all over the place, but I am a new developer who is very excited to deploy my first app. How do I build a Procfile?

(Edit) Ok actually I have found a jar file! I was using ".jar" in my terminal instead of "jar" and have found a gradlewrapper.jar file. I'm going to try using this file to follow the first Procfile example above, using the -jar command. gradlewrapper.jar

(Edit 2) Wow I feel like I am so close. It's exciting to get a new error message. Haha. It says "no main manifest attribute" is in the .jar file. new logs I found this stack overflow conversation about adding a manifest attribute, but I am unable to edit this zipped up file, and the file I have that resembles that developer's file the most (containing all the dependencies) is data that is held in my build.gradle file. build.gradle file I will try to edit the .jar file for now but any comments would be appreciated!


Solution

  • The jar file to which heroku refers is the executable jar file of your project. There are two cases now:

    1. for maven springboot project we use "mvn package" in the project directory. After that you should be able to find the jar file in target folder.
    2. For gradle springboot project you should be able to do the same.