mill 中指向 build.sc 文件所在目录的首选方法是什么?

What is the preferred way in mill to point to the directory where build.sc file is located?

mill 中指向 build.sc 文件所在目录的首选方式是什么?

在工厂文档中使用 os.pwd,例如here,但如果可以从其他地方 start/run 铣削,则 os.pwd 指向不正确的位置。

import mill._, mill.modules.Jvm

def sourceRoot = T.sources { os.pwd / "src" }

def resourceRoot = T.sources { os.pwd / "resources" }

每个已定义模块的根位置在def millSourcePath: os.Path中定义,这是访问模块路径的首选方式。

示例:

import mill._, mill.scalalib._

object mymodule extends JavaModule {
  def sources = T.sources(millSourcePath / "src")
}
$ mill show __.sources
No mill version specified.
You should provide a version via '.mill-version' file or --mill-version option.
Using mill version 0.9.9
Compiling /tmp/Whosebug/build.sc
[1/1] show 
[1/1] show > [1/1] mymodule.sources 
[
    "ref:c984eca8:/tmp/Whosebug/mymodule/src"
]

如果您需要访问项目本身的路径,请注意,它本身是一个 mill.define.Module,您可以通过变量 build 访问它。因此可以通过 build.millSourcePath.

访问项目目录

示例:

import mill._, mill.scalalib._

val baseDir = build.millSourcePath

object mymodule extends JavaModule {
  def sources = T.sources {
    super.sources() ++ Seq(PathRef(baseDir / "src"))
  }
}
$ mill show __.sources
No mill version specified.
You should provide a version via '.mill-version' file or --mill-version option.
Using mill version 0.9.9
Compiling /tmp/Whosebug/build.sc
[1/1] show 
[1/1] show > [2/2] mymodule.sources 
[
    "ref:c984eca8:/tmp/Whosebug/mymodule/src",
    "ref:c984eca8:/tmp/Whosebug/src"
]