Sbt 如何将项目传递给任务?
我正在编写一个Sbt 如何将项目传递给任务?,sbt,Sbt,我正在编写一个任务,它需要访问项目——它需要迭代依赖项,以执行特定于构建的一些副作用。我需要能够计算出此项目的子模块的可传递的内部和外部依赖项(即模块和JAR) 我目前正在做类似的事情来传递我需要的其他东西(name和通过libraryDependencies)由常春藤管理的deps): 我还需要另一个参数,比如 (project in Compile) 但这样一把钥匙并不存在 我该如何得到这个项目 注意:我意识到这可能是不可能的,因为项目/任务/阶段轴,如果没有一个邪恶的黑客,包括从手动维护
任务
,它需要访问项目
——它需要迭代依赖项
,以执行特定于构建的一些副作用。我需要能够计算出此项目的子模块的可传递的内部和外部依赖项(即模块和JAR)
我目前正在做类似的事情来传递我需要的其他东西(name
和通过libraryDependencies
)由常春藤管理的deps):
我还需要另一个参数,比如
(project in Compile)
但这样一把钥匙并不存在
我该如何得到这个项目
注意:我意识到这可能是不可能的,因为项目/任务/阶段轴,如果没有一个邪恶的黑客,包括从手动维护的hashmap中命名查找项目,但无论如何都值得一问,以防有一个干净的解决方案。使用
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
还有一个configurations
字段,用于保存项目可用配置的列表。如果需要跨配置查询设置值(例如libraryDependencies
),请使用它
scala> thisProject.eval.configurations
res3: Seq[sbt.Configuration] = List(compile, runtime, test, provided, optional)
您可能还想阅读“从多个作用域获取值”中的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
}
)
这是一种非常冗长的方式,表示编译中的
thisProject
返回;-)每个阶段可用的标签列表在哪里?对不起,我有点草率地接受了这个答案。不幸的是,ResolvedProject
不允许我获取其依赖项的已解析版本:我需要能够从项目依赖项树中删除。您能否显示我理解失败的代码?如果有一个工作示例,需要填写一些部分才能得到完整的解决方案,那么就容易多了。可行吗?你是对的,依赖关系在这里不感兴趣。我被这个名字吸引住了:(w00t!来回答我自己的评论:Project.getProjectForReference(Project.dependencies(0.Project,struct).get
如果你只需要在这个项目中编译libraryDependencies,那么你为什么需要一个项目实例呢?@JacekLaskowski我需要能够解决可传递的内部和外部依赖关系(即模块和JAR)thisProject
的子模块。啊,但这就是update
任务提供的。请参阅。您还需要更多吗?当调用update
的项目具有聚合或项目依赖项时,它们也将被包括在内。您还需要什么?酷!真正的信息隐藏在名称模糊的配置中地址
字段。请添加此作为答案,我将接受:-)如果您有更多sbt foo要分发,请查看
scala> thisProject.eval.configurations
res3: Seq[sbt.Configuration] = List(compile, runtime, test, provided, optional)
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
}
)