运行Spring应用程序与Heroku上的毕业生 [英] Running Spring app built with gradle on Heroku

查看:258
本文介绍了运行Spring应用程序与Heroku上的毕业生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用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());
}
}

将buildpacks更改为具有毕业生的包装后,我推送了一切在Heroku免费帐户与MongoHQ沙箱。



但是,尝试通过网络浏览器运行我的应用程序后,我收到这个错误:


应用程序中发生错误,无法提供您的页面。请稍后重试。



如果您是应用程序所有者,请查看日志以了解详情。


英雄日志给我这个输出:


2014-10-15T18:19:30.293964 + 00:00 heroku [web.1]:使用命令开始进程 java -Xmx384m -Xss512k -XX:+ UseCompressedOops -jar build / libs / *。jar



2014-10-15T18:19:30.797673 + 00:00 app [web.1]:错误:无法访问jarfile build / libs / *。jar



2014-10-15T18:19:31.474525 + 00:00 heroku [web.1]:状态从开始变为崩溃



2014-10-15T18:19:31.464753 + 00:00 heroku [web.1]:进程退出状态1



2014-10-15T18:19:32.577398 + 00:00 heroku [路由器]:at =错误代码= H10 desc =应用崩溃方法= GET路径=/host = tvmaniac.herokuap
p。 com request_id = 7e8bfe6c-2669-405e-9bce-59fde09f71ef fwd =89.129.247.185dyno = connect = service = status = 503 bytes = / p>

2014-10-15T18:19:34.016281 + 00:00 heroku [路由器]:at =错误代码= H10 desc =App crashedmethod = GET path = /favicon.icohost = tvmani
ac.herokuapp.com request_id = 4300386e-dc5c-47ed-9878-0bee87128fc9 fwd =89.129.247.185dyno = connect = service = status = 503 bytes =



2014-10-15T18:19:41.988204 + 00:00 heroku [路由器]:at =错误代码= H10 desc =应用崩溃方法= GET路径=/主机= tvmaniac.herokuap
p.com request_id = 436db871-ea8c-428f-8cc7-160c3cb96a2d fwd =50.16.146.194dyno = connect = service = status = 503 bytes =


< blockquote>

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



< blockquote>

default_process_types:



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


< blockquote>

EDITED



这是我的build.gradle:

  buildscript {
repositories {
maven {urlhttp://repo.spring.io/libs-release}
mavenCentral()
}
依赖关系{
classpath(org.springframework .boot:spring-boot-gradle-plugin:1.1.8.RELEASE)
}
}

应用插件:'java'
应用插件: eclipse'
应用插件:'idea'
应用插件:'spring-boot'

mainClassName =com.shalastra.tvmaniac.TVManiacConfiguration

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

存储库{
mavenCentral()
maven {urlhttp://repo.spring.io/libs-release}
maven {urlhttp://m2.neo4j.org}
}

依赖关系{
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)
comp ile(org.springframework.data:spring-data-mongodb)

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

任务包装器(类型:Wrapper){
gradleVersion ='2.0'
}

任务阶段(dependsOn:['clean','build','installApp'])

提前感谢您的帮助。

解决方案

在评论中经过短暂的讨论后,似乎(根据说明)您需要将以下任务添加到 build.gradle file: / p>

 任务阶段(类型:复制,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 

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



说明:



任务 stage 准备jar文件要跑完成之后,输出jar被复制到 project.rootDir 并重命名为 app.jar 。它保证它始终具有相同的名称,并可以使用 Procfile 中的命令轻松运行。 阶段任务还取决于 clean (其中还有一个额外的操作来删除 app.jar )和 build (构建应用程序)。设置 stage.mustRunAfter(clean)非常重要,因为否则运行任务的顺序不会被确定(这可能发生在现在 - 在本地检查)。我的意思是,如果只指定 dependsOn 被指定 build 可能会运行,然后 clean 最后 stage - 并没有创建任何内容。我希望很清楚。


I have problem with my Spring application built with Gradle. App includes MongoDB(MongoHQ from Heroku).

I managed how to push everything on heroku, I've added this code to my project:

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

After changing buildpacks to one with gradle, I pushed everything on Heroku free account with MongoHQ Sandbox.

But after trying to run my app through web browser, I'm getting this error:

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 logs gives me this output:

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=

I think the problem is in Procfile, but I have no idea what should I add there, here's my current code:

default_process_types:

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

EDITED

Here's my 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'])

Thanks in advance for your help.

解决方案

After a short discussion in comments it seems (according to this instructions) that You need add the following task to build.gradle file:

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

And the content of Procfile will be:

web: java $JAVA_OPTS -jar app.jar

Now it should work fine.

Explanation:

Task stage prepares jar file to be run. After it's finished the output jar is copied into project.rootDir and renamed to app.jar. It guarantees that it will have always the same name and can be easily run with the command from Procfile. stage task also depends on clean (which has an additional action to remove app.jar) and build (which builds the app). It's important to set stage.mustRunAfter(clean) because otherwise the order in which tasks are run is not determined (this probably happens now - check it locally). What I mean is if just dependsOn is specified build may be run, then clean and finally stage - and nothing is created. I hope it's clear.

这篇关于运行Spring应用程序与Heroku上的毕业生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆