问题
I'm trying to connect my application to Shared Database 5MB using grails tutorial. I'm using grails 2.1.0. On my local machine when I run heroku config I can see that DATABASE_URL is set, but after executing git push... my build stops with error:
Error packaging application: Error loading DataSource.groovy: null (Use --stacktrace to see the full trace)
Failed to build app
Heroku push rejected, failed to compile Grails app
So I've added logging to see how environment looks on heroku. I've added println System.env to Datasource.groovy and I cannot find any DATABASE_URL:
.[OLDPWD:/app/tmp/repo.git/.cache, SHELL:/bin/bash, SHLVL:2, GIT_DIR:., JAVA_HOME:/usr/lib/jvm/java-6-openjdk, PATH:/app/tmp/repo.git/.cache/.grails/bin::/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin, SSH_CLIENT:XXX, MAIL:XXX, USER:XXX, LOG_TOKEN:t.9e79a5be-dfa2-446e-9f83-f6b4ebbc4eb7, GRAILS_HOME:/app/tmp/repo.git/.cache/.grails, PWD:XXX, HOME:/app/, LOGNAME:u11846, _:/app/tmp/repo.git/.cache/.grails/bin/grails, SSH_CONNECTION:XXX, LD_LIBRARY_PATH:/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-6-openjdk/jre/lib/amd64:/usr/lib/jvm/java-6-openjdk/jre/../lib/amd64]
I have checked with heroku addons that for my application shared database is installed:
heroku addons
=== XXX Configured Add-ons
shared-database:5mb
and when I execute heroku config
heroku config
=== Config Vars for XXX
DATABASE_URL: postgres://XXX
JAVA_OPTS: -Xmx384m -Xss512k -XX:+UseCompressedOops
SHARED_DATABASE_URL: postgres://XXX
Datasource.groovy:
production {
dataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
println System.env
println System.env.DATABASE_URL
uri = new URI(System.env.DATABASE_URL)
println uri
url = "jdbc:postgresql://" + uri.host + uri.path
username = uri.userInfo.split(":")[0]
password = uri.userInfo.split(":")[1]
}
}
In BuildConfig.groovy I've added
runtime 'postgresql:postgresql:8.4-702.jdbc3'
I can launch application locally with DATABASE_URL set to local postgresql. I'm building war and running it on jetty.
Can anyone tell me what am I missing?
回答1:
The application environment variables are not available at build time. Your code needs to be able to handle that accordingly. Or you can enable the new user_env_compile Heroku Labs feature that does enable the environment variables to be there at build time: https://devcenter.heroku.com/articles/labs-user-env-compile
来源:https://stackoverflow.com/questions/11502305/heroku-grails-missing-database-url-when-building-app-on-heroku