java-在Heroku上使用gradle构建的Running Spring应用程序

我用Gradle构建的Spring应用程序有问题.应用程序包括MongoDB(来自Heroku的MongoHQ).

我设法将所有内容推送到heroku上,已将此代码添加到我的项目中:

@Configuration
public class MongoHQConfiguration {

    public @Bean MongoDbFactory mongoDbFactory() throws MongoException, UnknownHostException {
        return new SimpleMongoDbFactory(new MongoURI(System.getenv("MONGOHQ_URL")));
    }

    public @Bean MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }
}

将构建包更改为gradle之后,我使用MongoHQ Sandbox将所有内容放到了Heroku免费帐户中.

但是,尝试通过网络浏览器运行我的应用程序后,出现此错误:

An error occurred in the application and your page could not be served. Please try again in a few moments.

If you are the application owner, check your logs for details.

heroku日志给了我这个输出:

2014-10-15T18:19:30.293964+00:00 heroku[web.1]: Starting process with command java -Xmx384m -Xss512k -XX:+UseCompressedOops -jar build/libs/*.jar

2014-10-15T18:19:30.797673+00:00 app[web.1]: Error: Unable to access jarfile build/libs/*.jar

2014-10-15T18:19:31.474525+00:00 heroku[web.1]: State changed from starting to crashed

2014-10-15T18:19:31.464753+00:00 heroku[web.1]: Process exited with status 1

2014-10-15T18:19:32.577398+00:00 heroku[router]: at=error code=H10 desc=”App crashed” method=GET path=”/” host=tvmaniac.herokuap
p.com request_id=7e8bfe6c-2669-405e-9bce-59fde09f71ef fwd=”89.129.247.185″ dyno= connect= service= status=503 bytes=

2014-10-15T18:19:34.016281+00:00 heroku[router]: at=error code=H10 desc=”App crashed” method=GET path=”/favicon.ico” host=tvmani
ac.herokuapp.com request_id=4300386e-dc5c-47ed-9878-0bee87128fc9 fwd=”89.129.247.185″ dyno= connect= service= status=503 bytes=

2014-10-15T18:19:41.988204+00:00 heroku[router]: at=error code=H10 desc=”App crashed” method=GET path=”/” host=tvmaniac.herokuap
p.com request_id=436db871-ea8c-428f-8cc7-160c3cb96a2d fwd=”50.16.146.194″ dyno= connect= service= status=503 bytes=

我认为问题出在Procfile中,但我不知道该在其中添加什么,这是我当前的代码:

default_process_types:

web: java $JAVA_OPTS -jar build/libs/*.jar

EDITED

这是我的build.gradle:

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-release" }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE")
    }
}    

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

mainClassName = "com.shalastra.tvmaniac.TVManiacConfiguration"

jar {
    baseName = 'tvmaniac'
    version = '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-release" }
    maven { url "http://m2.neo4j.org" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")

    compile("org.springframework.boot:spring-boot-starter-actuator")
    testCompile("junit:junit")

    compile("org.springframework.data:spring-data-rest-webmvc")
    compile("org.springframework.data:spring-data-mongodb")

    compile("com.google.guava:guava:17.0")
    compile("org.apache.commons:commons-lang3:3.3.2")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.0'
}

task stage(dependsOn: ['clean', 'build', 'installApp'])

在此先感谢您的帮助.

最佳答案
在注释中进行简短讨论之后(根据this说明),您需要将以下任务添加到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的内容将是:

web: java $JAVA_OPTS -jar app.jar

现在应该可以正常工作了.

说明:

任务阶段准备要运行的jar文件.完成后,将输出jar复制到project.rootDir并重命名为app.jar.它保证它始终具有相同的名称,并且可以使用Procfile中的命令轻松运行.阶段任务还取决于清理(具有删除app.jar的附加操作)和构建(生成应用)的步骤.设置stage.mustRunAfter(clean)很重要,因为否则将无法确定任务的运行顺序(这可能现在发生-在本地检查).我的意思是,如果仅指定了dependsOn,则可以运行构建,然后进行清理,最后进入阶段,并且不会创建任何东西.我希望这很清楚.

转载注明原文:java-在Heroku上使用gradle构建的Running Spring应用程序 - 代码日志