通过 SBT 中的系统属性指定 flywayUrl

Posted

技术标签:

【中文标题】通过 SBT 中的系统属性指定 flywayUrl【英文标题】:Specifying flywayUrl through system property in SBT 【发布时间】:2014-03-05 06:04:30 【问题描述】:

使用Flyway 和sbt 如何通过系统属性(-D)而不是通过build.sbt 指定flywayUrl

我想通过命令行运行迁移,指定所有参数(驱动程序、url、用户、密码)而不在 build.sbt 中定义它们。

插件documentation page 似乎表明这应该是可能的:

优先顺序

系统属性>插件配置

我试过这样运行它:

sbt -Dflyway.url=jdbc:h2:file:target/foobar -Dflyway.user=SA flywayMigrate

flyway.url 属性似乎被忽略了,取而代之的是build.sbt 中定义的flywayUrl 属性。

考虑一个包含这些文件的项目:

build.sbt

libraryDependencies ++= Seq(
    "com.h2database" % "h2" % "1.3.174"
)

seq(flywaySettings: _*)

flywayUrl := "something that should be overriden"

项目/plugins.sbt

addSbtPlugin("com.googlecode.flyway" % "flyway-sbt" % "2.3")

resolvers += "Flyway" at "http://flywaydb.org/repo"

src/main/resources/db/migration/V1__Create_person_table.sql

create table PERSON (
    ID int not null,
    NAME varchar(100) not null
);

运行这个命令:

$ sbt -Dflyway.url=jdbc:h2:file:target/foobar -Dflyway.user=SA flywayMigrate

产生这个错误:

Loading /usr/share/sbt/bin/sbt-launch-lib.bash
[info] Loading project definition from /home/fernando/work/scratch/flyway-sbt/foobar/project
[info] Updating file:/home/fernando/work/scratch/flyway-sbt/foobar/project/foobar-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to foobar (in build file:/home/fernando/work/scratch/flyway-sbt/foobar/)
[info] Updating file:/home/fernando/work/scratch/flyway-sbt/foobar/foobar...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
com.googlecode.flyway.core.api.FlywayException: Invalid JDBC URL (should start with jdbc:) : something that should be overriden
    at com.googlecode.flyway.core.util.jdbc.DriverDataSource.<init>(DriverDataSource.java:82)
    at com.googlecode.flyway.sbt.FlywayPlugin$FlywayOps$.configure$extension1(FlywayPlugin.scala:214)
    at com.googlecode.flyway.sbt.FlywayPlugin$FlywayOps$.configure$extension0(FlywayPlugin.scala:207)
    at com.googlecode.flyway.sbt.FlywayPlugin$Flyway$.apply(FlywayPlugin.scala:193)
    at com.googlecode.flyway.sbt.FlywayPlugin$$anonfun$flywaySettings$26$$anonfun$apply$1.apply$mcI$sp(FlywayPlugin.scala:145)
    at com.googlecode.flyway.sbt.FlywayPlugin$$anonfun$flywaySettings$26$$anonfun$apply$1.apply(FlywayPlugin.scala:145)
    at com.googlecode.flyway.sbt.FlywayPlugin$$anonfun$flywaySettings$26$$anonfun$apply$1.apply(FlywayPlugin.scala:145)
    at com.googlecode.flyway.sbt.FlywayPlugin$.withContextClassLoader(FlywayPlugin.scala:184)
    at com.googlecode.flyway.sbt.FlywayPlugin$.com$googlecode$flyway$sbt$FlywayPlugin$$withPrepared(FlywayPlugin.scala:167)
    at com.googlecode.flyway.sbt.FlywayPlugin$$anonfun$flywaySettings$26.apply(FlywayPlugin.scala:145)
    at com.googlecode.flyway.sbt.FlywayPlugin$$anonfun$flywaySettings$26.apply(FlywayPlugin.scala:145)
    at scala.Function3$$anonfun$tupled$1.apply(Function3.scala:35)
    at scala.Function3$$anonfun$tupled$1.apply(Function3.scala:34)
    at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
    at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:42)
    at sbt.std.Transform$$anon$4.work(System.scala:64)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
    at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
    at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
    at sbt.Execute.work(Execute.scala:244)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
    at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
    at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
    at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
[error] (*:flywayMigrate) com.googlecode.flyway.core.api.FlywayException: Invalid JDBC URL (should start with jdbc:) : something that should be overriden
[error] Total time: 0 s, completed Feb 7, 2014 3:32:39 PM

【问题讨论】:

【参考方案1】:

The following is the definition of flywayUrl:

val flywayUrl = settingKey[String]("The jdbc url to use to connect to the database.")

我似乎无法找到如何通过系统属性设置设置。该插件似乎不支持它。

话虽如此,您应该找到以下build.sbt 解决方案,并能够通过适当的系统属性设置其值:

flywayUrl := System.getProperty("flyway.url", "[default]")

sbt 在没有设置 flyway.url 的情况下执行时:

$ sbt 'show flywayUrl'
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to flyway (in build file:/Users/jacek/sandbox/so/flyway/)
[info] [default]

当它在命令行上设置时:

$ sbt -Dflyway.url=command-line 'show flywayUrl'
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to flyway (in build file:/Users/jacek/sandbox/so/flyway/)
[info] command-line

您可能还会发现另一个问题Setting value of setting on command line when no default value defined in build? 很有用。

【讨论】:

谢谢,这似乎是有道理的。我会验证的。 谢谢,@Jacek。使用getProperty 设置值解决了我的问题。现在我对 sbt 有了更好的理解。但我相信以lazy val flywayUrl 开头的行不应该在我的build.sbt 中。它会导致错误:对 flywayUrl 的引用不明确 - 它在同一范围内被导入两次。除非我误解了什么,否则我认为最好将其排除在答案之外。 我很高兴它对你有用,因为它也提高了我对 SBT 的了解!至于lazy val flywayUrl,你是对的,你应该删除它,因为它已经在插件中定义了。 SBTing 快乐! :) 好的,我已经编辑了答案并删除了多余的行。谢谢。

以上是关于通过 SBT 中的系统属性指定 flywayUrl的主要内容,如果未能解决你的问题,请参考以下文章

为多项目中的子项目指定不同版本的 sbt?

是否有一个sbt插件来获取像Gemfile中的依赖项?

如何解决具有不同包装常春藤类型的sbt中的依赖关系?

如何为 sbt-native-package 指定 RPM_BUILD_ROOT

如何为 sbt/play 禁用彩色终端输出?

SBT 任务将属性文件从项目文件夹复制到 webapp