如何使用自定义依赖项扩展现有的 sbt 任务? [英] How to extend an existing sbt task with a custom dependency?

查看:48
本文介绍了如何使用自定义依赖项扩展现有的 sbt 任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改依赖于新任务的现有核心任务?

How to change an existing core task with a dependency on a new task?

例如我有一个创建一些文件夹的新任务,我希望编译中编译"任务依赖于此,以便所有依赖于编译的任务(如测试/包/运行/等)也创建这些文件夹.

E.g. I have a new task creating some folders and I would like the 'compile in Compile' task to depend on this, so that all tasks which depend on compile, like test/package/run/etc, also create these folders.

重新定义编译中编译"

compile in Compile := {
  createRepositoryFolder.value
  (compile in Compile).value
}

导致循环引用错误.使用

causes a cyclic reference error. Using

compile <<= (compile in Compile) dependsOn createRepositoryFolders

似乎有效,但它似乎创建了一个新的编译"任务,该任务不是由依赖编译中编译"的任务运行的;运行测试"或运行"任务时不执行createRepositoryFolders"任务.

seems to work, but it seems to create a new 'compile' task which is not run by tasks which depend on 'compile in Compile'; the 'createRepositoryFolders' task is not performed when running the 'test' or 'run' task.

使用inspect compile"确实显示了对createRepositoryFolders"任务的依赖,但inspect test"确实显示了对compile"任务的依赖,而不是对createRepositoryFolders"任务的依赖.

Using 'inspect compile' does show the dependency on the 'createRepositoryFolders' task, but 'inspect test' does show the dependency on the 'compile' task, but not on the 'createRepositoryFolders' task.

推荐答案

我相信使用 := 操作符可以为任务分配一个新值 compile in Compile.

I believe that with the := operator you are assigning a new value to the task compile in Compile.

因为你在compile in Compile的定义中提到了compile in Compile,这就是为什么你有一个循环依赖.

Since you refer to compile in Compile in the definition of compile in Compile that is why you have a cyclic dependency.

您可能想要的是附加到任务而不是重新定义它.有很多运算符可以将值附加到诸如 +=、<+= 之类的任务中,具体取决于您分配的值的类型.

What you probably want is to append to a task rather than define it anew. There are a bunch of operators to append values to tasks such as +=, <+=, depending on the type of values you're assigning.

但是,由于您正在创建资源,因此您可能希望将您的任务添加到 resourceManaged 任务中,而不是直接进行编译.

However, since you are creating resources, you probably want to add your task to the resourceManaged task, rather than directly to compile.

假设任务 createRepositoryFolder 返回一个 Seq[File],您可以将以下内容添加到 projectSettings:

Assuming the task createRepositoryFolder returns a Seq[File] you could add the following to the projectSettings:

(resourceManged in Compile) += createRepositoryFolder.taskValue

sbt 文档中有更多信息

这篇关于如何使用自定义依赖项扩展现有的 sbt 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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