Reputation: 19938
I have a spring boot gradle app which I could run successfully on my PC by doing:
heroku local
It can also deploy on heroku successfully when I go:
git push heroku master
This is my result:
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 596 bytes | 0 bytes/s, done.
Total 9 (delta 3), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Fetching set buildpack https://github.com/heroku/heroku-buildpack-gradle... done
remote: -----> Gradle app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Building Gradle app...
remote: WARNING: The Gradle buildpack is currently in Beta.
remote: -----> executing ./gradlew build
remote: :compileJavaNote: Some input files use unchecked or unsafe operations.
remote: Note: Recompile with -Xlint:unchecked for details.
remote:
remote: :processResources
remote: :classes
remote: :jar
remote: :bootRepackage
remote: :assemble
remote: :compileTestJava UP-TO-DATE
remote: :processTestResources UP-TO-DATE
remote: :testClasses UP-TO-DATE
remote: :test UP-TO-DATE
remote: :check UP-TO-DATE
remote: :build
remote:
remote: BUILD SUCCESSFUL
remote:
remote: Total time: 11.935 secs
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing... done, 72.3MB
remote: -----> Launching... done, v9
remote: https://myapp.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/myapp.git
6326429..291326d master -> master
But when I try to access it on heroku, it crashes. The heroku logs says this:
2015-12-11T17:05:46.096985+00:00 heroku[api]: Deploy 291326d by [email protected]
2015-12-11T17:05:46.097021+00:00 heroku[api]: Release v9 created by [email protected]
2015-12-11T17:05:46.378258+00:00 heroku[slug-compiler]: Slug compilation started
2015-12-11T17:05:46.378269+00:00 heroku[slug-compiler]: Slug compilation finished
2015-12-11T17:05:46.755655+00:00 heroku[web.1]: State changed from crashed to starting
2015-12-11T17:05:53.121398+00:00 heroku[web.1]: Starting process with command `java -Dserver.port=5000 -jar build/libs/myapp.jar`
2015-12-11T17:05:54.260741+00:00 app[web.1]: Error: Unable to access jarfile build/libs/myapp.jar
2015-12-11T17:05:54.784064+00:00 heroku[web.1]: State changed from starting to crashed
2015-12-11T17:05:54.773714+00:00 heroku[web.1]: Process exited with status 1
2015-12-11T17:05:54.788248+00:00 heroku[web.1]: State changed from crashed to starting
Why is it Unable to access jarfile build/libs/myapp.jar
?
When I navigate to that folder on my local PC, the jarfile is there.
I'm running out of ideas on how to solve this.
Upvotes: 3
Views: 7224
Reputation: 747
For deploying Gradle App on Heroku. Just add below lines in build.gradle file.
task stage(type: Copy, dependsOn: [clean, build]) {
from jar.archivePath
into project.rootDir
rename {
'app.jar'
}
}
stage.mustRunAfter(clean)
This also in build.gradle file for running jar file.
jar {
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
manifest {
attributes(
'Main-Class': 'com.heroku.herokudemo.HerokuDemoApplication'
)
}
}
After this create a txt file named Procfile and add the following content.
web: java $JAVA_OPTS -Dserver.port=$PORT -jar build/libs/heroku-demo-0.0.1-SNAPSHOT.jar
Upvotes: 0
Reputation: 1420
A simpler solution:
I was also encountering the same error and it was a serious waste of time for me. Unlike the ones here, I solved the problem as follows.
First of all, we need to know this.
Heroku creates a new version after each build.
For this reason, if you use a command that contains a fixed version definition in the Procfile
file like build/libs/app-0.0.1-SNAPSHOT.jar
, it will cause an error like this.
2022-03-02T18:19:30.797673+00:00 app[web.1]: Error: Unable to access jarfile build/libs/app-0.0.1-SNAPSHOT.jar
After the build task, Gradle
creates 2 jars named app-0.0.1-SNAPSHOT.jar
and app-0.0.1-SNAPSHOT-plain.jar
under libs/
folder. app-0.0.1-SNAPSHOT.jar
is our application including all dependencies. So I first blocked the rendering of app-0.0.1-SNAPSHOT-plain.jar
as below.
jar {
enabled = false
}
Then I used the following wildcard definition in Procfile
which is not affected by version changes.
web: java $JAVA_OPTS -Dserver.port=$PORT -jar build/libs/*.jar
The problem can also be solved in this way.
Upvotes: 1
Reputation: 1874
According to the documentation:
The Gradle buildpack will automatically detect the use of the Spring Boot and Ratpack web frameworks.
The web
command should be overridden only if needed. If you're using Spring Boot, you shouldn't need to specify a Procfile
at all.
Delete it, and get your app running without further configuration.
Upvotes: 1
Reputation: 93
You don't need to modify your stage task for that.
In your gradle.build file, make sure to give a name to your built artifact like this.
jar {
baseName = 'app'
version = '0.0.1'
}
With that been done, and the following in Procfile
, it should just run fine.
web: java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/app-0.0.1.jar
I found that if name is not explicitly defined, heroku seems to name the artifact something like build-{buildNumber}.jar
and no wonder it will never find the app.jar
.
Also, if not set already, the build pack needs to be set.
heroku config:set GRADLE_TASK="build"
Upvotes: 5
Reputation: 19938
I eventually found a very helpful thread here: Running Spring app built with gradle on Heroku
Which helped me with my problem - this thread seems to be more detail than Heroku's official guide.
You must add the following code to your build.gradle file:
task stage(type: Copy, dependsOn: [clean, build]) {
from jar.archivePath
into project.rootDir
rename {
'app.jar'
}
}
stage.mustRunAfter(clean)
clean << {
project.file('app.jar').delete()
}
Then, point your Procfile to the app.jar:
web: java $JAVA_OPTS -jar app.jar
Upvotes: 4