![](/img/trans.png)
[英]Spring Boot Rest API Scheduler Not Working on Heroku
[英]Heroku deploy build failure for Spring Boot REST API
我在本地有一个 Spring 引导 REST API。
当我运行 mvn clean package 时,它在本地成功生成 .war 文件。
现在我想将它部署到 Heroku。
这些是我已经完成的步骤。
将代码上传到 github 存储库。 假设 \myrestapi。
打开终端运行 Heroku CLI 命令
登录 heroku
heroku login
Ran 命令在 heroku 上创建应用程序
heroku create myrestapi
该命令不起作用。 收到消息说名称“myrestapi”已经存在。
所以我把名字改成了别的。 让我们说“cl-myrestapi”:
heroku create cl-myrestapi
它运行成功。
Ran 命令在 heroku 上创建免费的 postgreSQL 数据库:
heroku addons:create heroku-postgresql:hobby-dev
Git推
git push heroku main
这将成功开始下载。
但随后会产生以下错误:
remote: [INFO] BUILD FAILURE
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] Total time: 14.653 s
remote: [INFO] Finished at: 2021-02-19T18:43:12Z
remote: [INFO] ------------------------------------------------------------------------
remote: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-
compile) on project myrestapi: Fatal error compiling: invalid target release: 11 -> [Help 1]
remote: [ERROR]
remote: [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
remote: [ERROR] Re-run Maven using the -X switch to enable full debug logging.
remote: [ERROR]
remote: [ERROR] For more information about the errors and possible solutions, please read the following artic
les:
remote: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
remote:
remote: ! ERROR: Failed to build app with Maven
remote: We're sorry this build is failing! If you can't find the issue in application code,
remote: please submit a ticket so we can help: https://help.heroku.com/
remote:
remote: ! Push rejected, failed to compile Java app.
remote:
remote: ! Push failed
remote: !
remote: ! ## Warning - The same version of this code has already been built: e11a85238783f54ee30765ba4f8a55354930be
09
remote: !
remote: ! We have detected that you have triggered a build from source code with version e11a85238783f54ee30765ba4f
8a55354930be09
remote: ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch.
remote: !
remote: ! If you are developing on a branch and deploying via git you must run:
remote: !
remote: ! git push heroku <branchname>:main
remote: !
remote: ! This article goes into details on the behavior:
remote: ! https://devcenter.heroku.com/articles/duplicate-build-version
remote:
remote: Verifying deploy...
remote:
remote: ! Push rejected to cl-myrestapi.
remote:
To https://git.heroku.com/cl-myrestapi.git
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/cl-myrestapi.git'
为什么我会收到此错误,因为它在本地成功构建?
我只能看到两个不同之处:
需要在 heroku 上有不同的项目名称(参见上面的步骤 4)
Application.properties 是不同的。
这是我原来的 application.properties(在本地运行)。
spring.datasource.url=jdbc:postgresql://localhost:5432/myrestapi
spring.datasource.username=xxx
spring.datasource.password=yyy
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
这是更新的 application.properties(正在上传到 heroku)
spring.datasource.url=${JDBC_DATASOURCE_URL}
spring.jpa.hibernate.ddl-auto=update
更新:注意到当git push heroku main
命令被执行时,一开始它说“安装 jdk 1.8”,而我使用的是版本 11。
所以这似乎是问题所在。
为了解决这个问题,我尝试使用以下行在根目录中添加一个 System.properties 文件:
java.runtime.version=11
但没有解决。
pom.xml
中添加这个<properties>
<java.version>11</java.version>
</properties>
检查设置中的Java compiler
并确保project-byte-code-version
和target-byte-code-version
设置为 11。
pom.xml
文件的底部尝试此操作。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.