如何在play/sbt下更改自定义配置的设置值? [英] How to change value of setting for a custom configuration under play/sbt?

查看:79
本文介绍了如何在play/sbt下更改自定义配置的设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个play项目,我想添加一个sbt任务,该任务使用给定的文件夹作为资源来运行应用程序.但是,我不希望在正常"运行期间将该文件夹放在类路径中.

I have a play project, and I want to add an sbt task that runs the application with a given folder available as a resource. However, I don't want that folder to be on the classpath during "normal" runs.

我创建了一个配置,将资源添加到该配置中,但是当我在该配置中运行时,文件没有被拾取

I created a configuration, added the resources to that configuration, but when I run in that configuration, the files aren't being picked up

例如,我有:

val Mock = config("mock") extend Compile
val mock = inputKey[Unit]("run in mock mode")

val project = Project("my project", file("src/"))
.configs(Mock)
.settings(
      unmanagedResourceDirectories in Mock ++= Seq(baseDirectory.value / "mock-resources")
      mock <<= run in Mock
)

我想要它,这样当我键入mock时,mock-resources在类路径上,而当我键入run时,它不在类路径上.

I want it so that when I type mock the mock-resources is on the classpath, and when i type run it isn't.

我正在使用播放2.2.0和sbt 0.13.1

I'm using play 2.2.0 with sbt 0.13.1

推荐答案

您需要将Compile配置下的适当设置和任务设置为新定义的Mock配置.原因是这样的:

You need to set the appropriate settings and tasks that are under Compile configuration to the newly-defined Mock configuration. The reason for this is this:

lazy val Mock = config("mock") extend Compile

Mock下没有设置或任务时,sbt会继续在Compile中搜索,其中确实定义了run但使用Compile值.

When there's no setting or task under Mock sbt keeps searching in Compile where run is indeed defined but uses Compile values.

执行以下操作即可正常工作-注意Seq中的Classpaths.configSettingsrun:

Do the following and it's going to work - note Classpaths.configSettings and run in Seq:

lazy val Mock = config("mock") extend Compile

lazy val mock = inputKey[Unit]("run in mock mode")

lazy val mockSettings = inConfig(Mock) {
  Classpaths.configSettings ++
  Seq(
    unmanagedClasspath += baseDirectory.value / "mock-resources",
    mock <<= run in Mock,
    run <<= Defaults.runTask(fullClasspath in Mock, mainClass in Mock, runner in Mock)
  )
}

lazy val p = (project in file("src/")).configs(Mock).settings(
  mockSettings: _*
)

注意:我不确定为什么需要以下行:

NOTE I'm unsure why I needed the following line:

run <<= Defaults.runTask(fullClasspath in Mock, mainClass in Mock, runner in Mock)

我的猜测是,由于run使用默认为Compile范围的fullClasspath,因此在Mock中看不到该值. sbt让我赞叹不已!

My guess is that because run uses fullClasspath that defaults to Compile scope it doesn't see the value in Mock. sbt keeps amazing me!

我已经在>默认运行任务为何不选择自定义配置中的设置的情况下对此进行了询问?

我一直在使用src目录下的以下hello.scala运行构建:

I've been running the build with the following hello.scala under src directory:

object Hello extends App {
    val r = getClass.getResource("/a.properties")
    println(s"resource: $r")
}

p/mock:mock上,它给了我

> p/mock:mock
[info] Running Hello
resource: file:/Users/jacek/sandbox/mock-config/src/mock-resources/a.properties

p/mock:run相同:

> p/mock:run
[info] Running Hello
resource: file:/Users/jacek/sandbox/mock-config/src/mock-resources/a.properties

mock没什么不同:

> mock
[info] Running Hello
resource: file:/Users/jacek/sandbox/mock-config/src/mock-resources/a.properties

这篇关于如何在play/sbt下更改自定义配置的设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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