如何在play/sbt下更改自定义配置的设置值? [英] How to change value of setting for a custom configuration under 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.configSettings
和run
:
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屋!