Heroku DATABASE_URL作为Maven的JDBC Url [英] Heroku DATABASE_URL as a JDBC Url for Maven

查看:140
本文介绍了Heroku DATABASE_URL作为Maven的JDBC Url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heroku上的我的应用程序使用DATABASE_URL。使用用户名和密码将Java解析到JDBC URL中很简单。那里没有问题。但是,我有一个JOOQ生成器和Flyway迁移器,它们都有maven插件,我无法弄清楚如何获得这些插件需要的JDBC URL,用户名和密码。所以目前我在应用程序启动时这样做并不理想。当我的应用程序启动时,我得到DATABASE_URL,解析它,然后执行飞路迁移和jOOQ代码生成。但我希望在实际构建过程中发生这种情况,而不是在应用程序启动过程中发生。



基本上我需要Heroku环境变量,格式如(postgres:// user:pass @ ec2-host:1234 / path-to-db)可以在Maven中作为这样的属性访问(jdbc:postgresql:// ec2-host:1234?user = user& password = pass)。 b
$ b

我认为这个解决方案可能与maven构建助手插件有关,但我无法获得正则表达式属性规范。

谢谢

解决方案

由于支持heroku,我已经解决了这个问题。 Lukas和Axel你可能想要为使用heroku的任何用户记录这一点,想要在他们的构建中运行你的工具(vs代码启动),并且不想手动维护不同的环境变量。



在运行flyway migration或jOOQ代码生成之前,我们将使用GMavin Plus插件运行一些代码来将DATABASE_URL环境变量解析为属性。



首先,您需要添加Groovy作为依赖项:

 < dependency> 
< groupId> org.codehaus.groovy< / groupId>
< artifactId> groovy-all< / artifactId>
< version> 2.3.9< / version>
< scope>编译< / scope>
< /依赖关系>

然后GMaven plus插件:

 <插件> 
< groupId> org.codehaus.gmavenplus< / groupId>
< artifactId> gmavenplus-plugin< / artifactId>
< version> 1.2< / version>
<执行次数>
<执行>
<阶段>初始化< /阶段>
<目标>
< goal>执行< / goal>
< /目标>
< /执行>
< /执行次数>
<配置>
< scripts>
< script><![CDATA [
URI dbUri = new URI(System.getenv(DATABASE_URL));

字符串username = dbUri.getUserInfo()。split(:)[0];
String password = dbUri.getUserInfo()。split(:)[1];
int port = dbUri.getPort();

String dbUrl =jdbc:postgresql://+ dbUri.getHost()+:+ port + dbUri.getPath();

project.properties ['database.jdbcUrl'] = dbUrl
project.properties ['database.username'] = username
project.properties ['database.password'] =密码
]]>< / script>
< / scripts>
< / configuration>
< / plugin>

现在您可以使用$ {database.jdbcUrl},$ {database.username}和$ { database.password}在你的flyway,jOOQ或其他配置中。

My apps on Heroku use a DATABASE_URL. This is simple to parse with Java into a JDBC URL with a user name and password. There's no issue there. However, I have a JOOQ generator and Flyway migrator that have maven plugins and I can't figure out how to get the JDBC URL, User Name, and Password that these plugins require into maven. So currently I do it on app startup which is not ideal. When my app starts I get the DATABASE_URL, parse it, then do the flyway migration and jOOQ code generation. But I would like this to happen during the actual build process, not during application startup.

Basically I need the Heroku Environment variable formatted like (postgres://user:pass@ec2-host:1234/path-to-db) to be accessible in maven as a property like this (jdbc:postgresql://ec2-host:1234?user=user&password=pass).

I think the solution may lie with the maven build helper plugin but I can't get the regex properties specification quite right.

Thanks

解决方案

I have solved this thanks to heroku support. Lukas and Axel you may want to document this for any of your users that are using heroku, want to run your tools in their build (vs code startup), and do not want to hand maintain different environment variables.

We will be using the GMavin Plus Plugin to run some code to parse the DATABASE_URL environment variable into properties prior to running the flyway migration or jOOQ code generation.

First off you need to add Groovy as a dependency:

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>2.3.9</version>
  <scope>compile</scope>
</dependency>

Then the GMaven plus plugin:

<plugin>
  <groupId>org.codehaus.gmavenplus</groupId>
  <artifactId>gmavenplus-plugin</artifactId>
  <version>1.2</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>execute</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <scripts>
      <script><![CDATA[
        URI dbUri = new URI(System.getenv("DATABASE_URL"));

        String username = dbUri.getUserInfo().split(":")[0];
        String password = dbUri.getUserInfo().split(":")[1];
        int port = dbUri.getPort();

        String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ":" + port + dbUri.getPath();

        project.properties['database.jdbcUrl']=dbUrl
        project.properties['database.username']=username
        project.properties['database.password']=password
        ]]></script>
      </scripts>
    </configuration>
</plugin>

Now you can use ${database.jdbcUrl}, ${database.username}, and ${database.password} in your flyway, jOOQ, or whatever else configurations.

这篇关于Heroku DATABASE_URL作为Maven的JDBC Url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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