scala - sbt 获取 RootProject 的构建文件夹

标签 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 及其父类(super class) 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 的回答)

在 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.

最佳答案

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

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"))
}

关于scala - sbt 获取 RootProject 的构建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32272836/

相关文章:

scala - java.lang.String 不能转换为 scala.Serializable

scala - 为什么 SBT 给我 "not found: object akka"和 "import akka._"?

scala - sbt:在编译测试时设置特定的 scalacOptions 选项

scala - 提供 Scala.js Assets

scala - 如何在 ubuntu 上构建和运行 zinc(scala 增量编译器)

java - scala、IntelliJ : stackoverflow exception

scala - 类型安全的生成器 : How to set compiler error message

docker - Docker容器在运行sbt/play应用程序时停止运行而没有任何错误

scala - Akka框架支持查找重复消息

scala - 惰性求值解释