Heroku上的Spring Boot Gradle应用程序:无法访问jarfile

时间:2015-12-11 17:11:18

标签: java spring heroku spring-boot

我有一个spring boot gradle app,我可以在我的电脑上成功运行:

heroku local

当我去的时候,它也可以成功部署在heroku上:

git push heroku master

这是我的结果:

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

但是当我尝试在heroku上访问它时,它会崩溃。 heroku日志说:

2015-12-11T17:05:46.096985+00:00 heroku[api]: Deploy 291326d by xxx@gmail.com
2015-12-11T17:05:46.097021+00:00 heroku[api]: Release v9 created by xxx@gmail.com
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

为什么Unable to access jarfile build/libs/myapp.jar

当我导航到本地PC上的那个文件夹时,jar文件就在那里。

我对如何解决这个问题的想法不足。

3 个答案:

答案 0 :(得分:3)

我最终在这里找到了一个非常有用的帖子:Running Spring app built with gradle on Heroku

这帮助了我解决了我的问题 - 这个帖子似乎比Heroku的官方指南更详细。

您必须将以下代码添加到build.gradle文件中:

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()
}

然后,将您的Procfile指向app.jar:

web: java $JAVA_OPTS -jar app.jar

答案 1 :(得分:3)

您无需为此修改舞台任务。

在gradle.build文件中,请确保为您构建的工件命名。

jar {
    baseName = 'app'
    version = '0.0.1'
}

完成了这项工作,以及Procfile中的以下内容,它应该可以正常运行。

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

我发现如果没有明确定义名称,heroku似乎将该工件命名为build-{buildNumber}.jar,并且难怪它永远不会找到app.jar

此外,如果尚未设置,则需要设置构建包。

heroku config:set GRADLE_TASK="build"

答案 2 :(得分:1)

根据documentation

  

Gradle buildpack将自动检测Spring Boot和Ratpack Web框架的使用。

仅在需要时才应覆盖web命令。如果您使用的是Spring Boot,则根本不需要指定Procfile

删除它,让您的应用无需进一步配置即可运行。