无法在 heroku 中基于 运行 gradle 的 webapp

Not able to run gradle based webapp in heroku

下面是我试过的。

下面是我的build.gradle

/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * user guide available at https://docs.gradle.org/3.4/userguide/java_library_plugin.html
 */
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'

repositories {
    mavenLocal()
    mavenCentral()
}

sourceSets {
    main {
        resources.srcDirs = ["src"]
        java {
            srcDir 'src'
        }
    }
}

task stage {
    dependsOn build
}
stage.mustRunAfter(clean)

dependencies {
    compile fileTree(dir: './webapp/WEB-INF/lib', include: ['*.jar'])

    compile 'org.springframework:spring-webmvc:4.2.5.RELEASE'

    compile 'org.apache.poi:poi:3.14'
    compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
    compile 'org.slf4j:slf4j-api:1.7.21'
    compile 'org.apache.logging.log4j:log4j-api:2.5'
    compile 'org.apache.logging.log4j:log4j-core:2.5'

    compile group: 'javax.validation', name: 'validation-api', version: '1.1.0.Final'
    compile group: 'org.json', name: 'json', version: '20141113'

    compile group: 'mysql', name:'mysql-connector-java', version:'5.1.36'
    compile group:  'org.mybatis',name:'mybatis-spring',version:'1.2.3'
    compile group:  'org.mybatis',name:'mybatis',version:'3.3.0'

    compile group: 'org.springframework', name:'spring-orm', version:'3.1.2.RELEASE'
    compile group: 'org.springframework', name:'spring-jdbc', version:'3.1.2.RELEASE'
    compile group: 'org.springframework', name:'spring-webmvc', version:'3.1.2.RELEASE'
    compile group: 'commons-dbcp', name:'commons-dbcp', version:'1.2.2'
    compile group: 'org.activiti', name: 'activiti-engine', version: '5.18.0'

    compile group: 'javax.mail', name:'mail', version:'1.4.3'

    compile group: 'commons-fileupload', name:'commons-fileupload', version:'1.3.2'
    compile group: 'commons-io', name:'commons-io', version:'1.3.2'
    compile group: 'org.apache.axis', name:'axis', version:'1.4'
    compile group: 'commons-discovery', name: 'commons-discovery', version: '0.2'
    compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
    compile group: 'javax.xml', name: 'jaxrpc-api', version: '1.1'
    compile group: 'javax.xml.soap', name: 'saaj-api', version: '1.3'
    compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2'
    compile group: 'org.twitter4j', name: 'twitter4j', version: '4.0.4'
    compile group: 'org.twitter4j', name: 'twitter4j-stream', version: '3.0.5'

    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
    testCompile 'junit:junit:3.8.1'
}

Procfile

web: build/install/twitter-api9/bin/TwitterApi

当我从 postman 调用我的 api 时,出现以下错误。

错误日志:

2017-04-10T19:11:21.820853+00:00 heroku[web.1]: State changed from crashed to starting
2017-04-10T19:11:26.250394+00:00 heroku[web.1]: Starting process with command `build/install/twitter-api9/bin/TwitterApi`
2017-04-10T19:11:28.283776+00:00 heroku[web.1]: Process exited with status 127
2017-04-10T19:11:28.198888+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2017-04-10T19:11:28.199032+00:00 app[web.1]: bash: build/install/twitter-api9/bin/TwitterApi: No such file or directory
2017-04-10T19:11:28.324886+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-10T19:11:50.592336+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/main/twitter_data" host=twitter-api9.herokuapp.com request_id=69ce91fa-9677-4b51-9190-dc40bf0f1648 fwd="49.205.228.96" dyno= connect= service= status=503 bytes= protocol=http

我认为 procfile 中有问题,因为我的构建是成功的,但唯一的问题是当我尝试访问我的 application/api 时。该应用程序是一个 spring mvc application,它在 war 部署在本地 tomcat 中运行良好。

编辑:

将我的 Procfile 更改为低于我的 war 所在位置

web: sh build/libs

现在的错误是:

虽然进程以状态 0 退出,但我猜这意味着一切都很好。但应用程序仍然崩溃:

2017-04-11T14:37:32.333327+00:00 heroku[web.1]: State changed from crashed to starting
2017-04-11T14:37:37.394992+00:00 heroku[web.1]: Starting process with command sh build/libs
2017-04-11T14:37:39.356372+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2017-04-11T14:37:39.441553+00:00 heroku[web.1]: Process exited with status 0
2017-04-11T14:37:39.458917+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-11T15:17:01.974207+00:00 heroku[run.7895]: Awaiting client
2017-04-11T15:17:02.034572+00:00 heroku[run.7895]: Starting process with command bash
2017-04-11T15:17:02.290336+00:00 heroku[run.7895]: State changed from starting to up
2017-04-11T14:38:47.445653+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/main/twitter_data" host=twitter-api9.herokuapp.com request_id=1673e258-3fc2-4630-ac22-4869b8e68b13 fwd="49.205.228.96" dyno= connect= service= status=503 bytes= protocol=https

Step by step on how to address this issue:

  1. 首先检查 .war 文件的部署位置,具体操作如下:

    运行 $ heroku run bash -a twitter-api9 来自 cli.

  2. 现在终端已打开,请使用 ls 检查存在的目录。如果不仅仅是 .war 文件的 grep,我相信构建将是其中之一。

  3. 一旦你知道 war 位置,将你的 Procfile 更改为以下(对我来说 war 位于 build/libs/ 其他人可能不同):

web: java $JAVA_OPTS -DTG_ENV=prod -jar webapp-runner.jar --port $PORT build/libs/*.war

上面将把请求重定向到这些位置,因为这是一个网络应用程序,你需要一个 webapp-runner.jar /或 jetty-运行ner 或等价物 到 运行 如果是罐子则不需要 war。

  1. 现在应用程序已 运行ning 并准备接受请求。但请注意,如果构建成功并且 webapp-runner.jar 位于根目录中,则以上内容有效,否则请提及 jar 的完整路径。