简体   繁体   English

Heroku 上的 Java Web 应用程序:无法访问 jarfile

[英]Java web app on Heroku: Unable to access jarfile

I'm trying to deploy my Java Spring Boot web application to Heroku.我正在尝试将我的 Java Spring Boot Web 应用程序部署到 Heroku。

To launch it locally I run:要在本地启动它,我运行:

mvn install

and then进而

java $JAVA_OPTS -jar target/*.war

So for Heroku I've created the Procfile: web: java $JAVA_OPTS -jar target/*.war所以对于 Heroku,我创建了 Procfile: web: java $JAVA_OPTS -jar target/*.war

I use Heroku Github integration and the application is deployed to Heroku from Github.我使用 Heroku Github 集成,并将应用程序从 Github 部署到 Heroku。 So I just push it there.所以我只是把它推到那里。

But the application doesn't launch.但是应用程序没有启动。

heroku logs --app myapp gives me: heroku logs --app myapp给我:

2015-09-09T21:53:25.581128+00:00 heroku[web.1]: Starting process with command `java $JAVA_OPTS -jar target/*.war`
2015-09-09T21:53:27.110820+00:00 app[web.1]: Error: Unable to access jarfile target/*.war`

heroku run bash --app myapp with ls -a doesn't show target directory. heroku run bash --app myapp with ls -a不显示target目录。

I think Heroku doesn't build the app.我认为 Heroku 不会构建应用程序。 But what am I doing wrong?但我做错了什么? Thanks in advance for you advices!在此先感谢您的建议!

Instead of mvn install do mvn package to know the difference check http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html而不是mvn installmvn package以了解差异检查http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Deploying a WAR file with the Heroku Maven plugin allows you to integrate the deployment process with your existing Maven process.使用 Heroku Maven 插件部署 WAR 文件允许您将部署过程与现有的 Maven 过程集成。 The Heroku plugin uses the artifacts generate by your mvn package phase, and builds them into a Slug file that is uploaded to the Heroku servers. Heroku 插件使用 mvn 包阶段生成的工件,并将它们构建到上传到 Heroku 服务器的 Slug 文件中。

In this way, the Heroku Maven plugin avoids the overhead of recompiling your project remotely.通过这种方式,Heroku Maven 插件避免了远程重新编译项目的开销。 This is often the preferred approach when deploying from a CI server, which may already have built your WAR file and tested it.从 CI 服务器进行部署时,这通常是首选方法,它可能已经构建了您的 WAR 文件并对其进行了测试。

Source: https://devcenter.heroku.com/articles/war-deployment#deployment-with-the-heroku-maven-plugin来源: https : //devcenter.heroku.com/articles/war-deployment#deployment-with-the-heroku-maven-plugin

Regarding deploying Spring Boot, there are some steps to be taken:关于部署Spring Boot,有一些步骤需要采取:

  • Configure port配置端口

    web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar

  • Specify JDK, default is 1.8 (No need to change if 1.8 is specified in Maven)指定JDK,默认为1.8(如果Maven指定为1.8则无需更改)

Check http://docs.spring.io/spring-boot/docs/current/reference/html/cloud-deployment-heroku.html检查http://docs.spring.io/spring-boot/docs/current/reference/html/cloud-deployment-heroku.html

To execute Procfile use heroku local要执行 Procfile 使用heroku local

I found the cause of the error.我找到了错误的原因。 Here is a part of build log:这是构建日志的一部分:

-----> Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
       Detected buildpacks: Node.js, Java
       See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
-----> Node.js app detected
-----> Creating runtime environment

According to Heroku build order the application was considered as NodeJS one.根据 Heroku 构建顺序,该应用程序被视为 NodeJS 之一。 So the solution was to set java build pack:所以解决方案是设置java build pack:

heroku buildpacks:set https://github.com/heroku/heroku-buildpack-java

And then push any change to application to make it rebuilt.然后将任何更改推送到应用程序以使其重建。

  1. Packaging part in pom.xml should be always war, not jar pom.xml 中的打包部分应该总是war,而不是jar
  2. Putting java buildpack with priority 1 and nodejs with priority 2 also helped将优先级为 1 的 java buildpack 和优先级为 2 的 nodejs 放入也有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM