sbt获取RootProject的构建文件夹

时间:2015-08-28 13:59:00

标签: scala sbt

我想在我的SBT中包含一个外部项目,例如:

lazy val leonProject = RootProject(uri("https://github.com/epfl-lara/leon.git"))

现在我有一个自定义任务,需要获取此RootProject的一些文件 如何找到RootProject已下载的路径?

我尝试了什么

这不存在。

leonProject.baseDirectory // Does not exist. There is no such field.

我试着查看sbt的文档,看看我们可以用RootProject及其超类ProjectReference做什么,但我找不到任何东西(我甚至写了一个脚本来从他们的网站上找到相关的信息)。

我尝试模仿sbt eclipse plugin的行为,即关注thisthis。但它只会回归"#footest"如果我运行sbt footest,则永远不执行内部命令,事件。它仅列出激活此命令的项目。

lazy val root = (project in file(".")).settings(
  Keys.commands <+= ("footest")((str: String) => {
    println("#"+str)
    lazy val key = Keys.baseDirectory in ThisBuild
    def structure(state: State): BuildStructure = Project.extract(state).structure
    Command.single("footest")((state, args) => {
        println("executed")
        key.get(structure(state).data) match {
          case Some(a) => println("The project file "+a)
          case None => "Undefined setting '%s'!"
        }
        state
      }
    )
  }
))
// and call sbt foobar.

我的解决方案(感谢@thirstycrow&#39;答案)

在build.sbt文件中,在顶层(对我来说,项目的设置不起作用),请写下以下内容:

lazy val leonLocalBase = SettingKey[File]("leonLocalBase",  "local base for leon project")
leonLocalBase := {
  val build = loadedBuild.value
  val leonUnit = build.units(leonProject.build)
  leonUnit.localBase
}

// Now you can use leonLocalBase.value.getAbsolutePath() to retrieve the full path where leon has been downloaded.

1 个答案:

答案 0 :(得分:1)

您可以从加载的项目结构中获取它。

import sbt._
import Keys._

object MyBuild extends Build {

  val leonLocalBase = SettingKey[File]("leonLocalBase", "local base for leon project")

  lazy val root = Project("root", file("."))
    .dependsOn(leonProject)
    .settings(
      leonLocalBase := {
        val build = loadedBuild.value
        val leonUnit = build.units(leonProject.build)
        leonUnit.localBase
      }
    )

  lazy val leonProject = RootProject(uri("https://github.com/epfl-lara/leon.git"))
}