在 sbt 插件中访问依赖(非子)项目
accessing dependent (not child) projects in sbt plugin
请在下面找到使用我们插件的示例 build.sbt 文件。
在此示例 BasePlugin 中,我们想要 a/project、b/project 目录的完整路径:-
import sbt._
import Keys._
import BasePlugin._
BasePlugin.settings
lazy val root = Project("root", file(".")).dependsOn(
ProjectRef( uri("../some/where/a"), "a" ),
ProjectRef( uri("../some/where/b"), "b" )
)
enablePlugins(BasePlugin)
此外,在下面找到简化的 sbt 插件 BasePlugin.scala :-
package base
import sbt.{ThisBuild, Def, TaskKey, AutoPlugin}
import sbt._
import Keys._
/**
* Created by mogli on 4/23/2017.
*/
object BasePlugin extends AutoPlugin {
object autoImport {
lazy val customtask: TaskKey[Unit] = TaskKey("customtask")
}
import autoImport.customtask
override def projectSettings: Seq[Def.Setting[_]] = Seq(
customtask := {
//expectation: to get an iterator or collection sort of thing for dependent projects, but they are not in this variable (projectDependencies)
val deps = projectDependencies
deps map { c => println("project : " + c) }
}
)
}
如何访问sbt插件中的依赖项目。
获取项目的依赖做
val deps = thisProject.value.dependencies.map { dep => dep.project }
如果您在 projectSettings
方法体内访问 thisProject
,这将按预期工作。
请在下面找到使用我们插件的示例 build.sbt 文件。 在此示例 BasePlugin 中,我们想要 a/project、b/project 目录的完整路径:-
import sbt._
import Keys._
import BasePlugin._
BasePlugin.settings
lazy val root = Project("root", file(".")).dependsOn(
ProjectRef( uri("../some/where/a"), "a" ),
ProjectRef( uri("../some/where/b"), "b" )
)
enablePlugins(BasePlugin)
此外,在下面找到简化的 sbt 插件 BasePlugin.scala :-
package base
import sbt.{ThisBuild, Def, TaskKey, AutoPlugin}
import sbt._
import Keys._
/**
* Created by mogli on 4/23/2017.
*/
object BasePlugin extends AutoPlugin {
object autoImport {
lazy val customtask: TaskKey[Unit] = TaskKey("customtask")
}
import autoImport.customtask
override def projectSettings: Seq[Def.Setting[_]] = Seq(
customtask := {
//expectation: to get an iterator or collection sort of thing for dependent projects, but they are not in this variable (projectDependencies)
val deps = projectDependencies
deps map { c => println("project : " + c) }
}
)
}
如何访问sbt插件中的依赖项目。
获取项目的依赖做
val deps = thisProject.value.dependencies.map { dep => dep.project }
如果您在 projectSettings
方法体内访问 thisProject
,这将按预期工作。