如何将项目传递给任务?

时间:2014-08-05 10:32:36

标签: sbt

我正在编写需要访问Task的{​​{1}} - 它需要遍历Project以执行特定于我们构建的一些副作用。我需要能够计算出dependencies的子模块的传递内部和外部依赖关系(即模块和jar)。

我目前正在做这样的事情来传递我需要的其他东西(thisProject和常春藤管理的代表通过name):

libraryDependencies

我还需要另一个参数,比如

myTask := runMyTask(
  (name in Compile).value,
  (libraryDependencies in Compile).value
)

但是这样的密钥不存在。

我该如何获得该项目?

NB我意识到这可能是不可能的 - 如果没有涉及从手动维护的hashmap命名查找项目的邪恶黑客 - 因为Project / Task / Phase轴,但是如果有一个干净的解决方案,无论如何都值得询问。 / p>

1 个答案:

答案 0 :(得分:1)

使用thisProject或您在构建中定义的任何其他lazy val

> help thisProject
Provides the current project for the referencing scope.
> inspect thisProject
[info] Setting: sbt.ResolvedProject = Project(id runtime-assembly, base: C:\dev\sandbox\runtime-assembly, configurations: List(compile, runtime, test, provided, optional), plugins: List(<none>), autoPlugins: List(sbt.plugins.CorePlugin, sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.JUnitXmlReportPlugin))
[info] Description:
[info]  Provides the current project for the referencing scope.
[info] Provided by:
[info]  {file:/C:/dev/sandbox/runtime-assembly/}runtime-assembly/*:thisProject
[info] Defined at:
[info]  (sbt.Load) Load.scala:210
[info] Reverse dependencies:
[info]  *:ivyConfigurations
[info]  *:name
[info]  *:organization
[info]  *:cacheDirectory
[info]  *:baseDirectory
[info] Delegates:
[info]  *:thisProject
[info]  {.}/*:thisProject
[info]  */*:thisProject

试试consoleProject如下:

> consoleProject
[info] Starting scala interpreter...
[info]
import sbt._
import Keys._
import dsl._
import _root_.org.sbtidea.SbtIdeaPlugin._
import _root_.de.johoop.jacoco4sbt.JacocoPlugin._
import _root_.com.timushev.sbt.updates.UpdatesPlugin._
import _root_.sbtassembly.Plugin._
import _root_.sbt.plugins.IvyPlugin
import _root_.sbt.plugins.JvmPlugin
import _root_.sbt.plugins.CorePlugin
import _root_.sbt.plugins.JUnitXmlReportPlugin
import currentState._
import extracted._
import cpHelpers._
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_60).
Type in expressions to have them evaluated.
Type :help for more information.

scala> thisProject
res0: sbt.SettingKey[sbt.ResolvedProject] = sbt.SettingKey$$anon$4@1f3eff94

scala> thisProject.eval
res1: sbt.ResolvedProject = Project(id runtime-assembly, base: C:\dev\sandbox\runtime-assembly, configurations: List(compile, runtime, test, provided, optional), plugins: List(<none>), autoPlugins: List(sbt.plugins.CorePlugin, sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.JUnitXmlReportPlugin))

scala> thisProject.eval.dependencies
res2: Seq[sbt.ClasspathDep[sbt.ProjectRef]] = List()

还有configurations字段,其中包含项目的可用配置列表。如果您需要跨配置查询设置的值(例如libraryDependencies),请使用它。

scala> thisProject.eval.configurations
res3: Seq[sbt.Configuration] = List(compile, runtime, test, provided, optional)

您可能还想了解Getting values from multiple scopes “中的ScopeFilter,它可以从多个范围中获取值”。包括页面中的样本:

lazy val core = project

lazy val util = project

lazy val root = project.settings(
   sources := {
      val filter = ScopeFilter(inProjects(core, util), inConfigurations(Compile))
      // each sources definition is of type Seq[File],
      //   giving us a Seq[Seq[File]] that we then flatten to Seq[File]
      val allSources: Seq[Seq[File]] = sources.all(filter).value
      allSources.flatten
   }
)