mill 任务如何更新环境变量,以便稍后 运行 任务将看到更新后的值?

How can a mill task update environment variables so that later running tasks will see the updated value?

mill v0.9.9 Task Context API documentation据说

Mill keeps a long-lived JVM server to avoid paying the cost of recurrent classloading. Because of this, running System.getenv in a task might not yield up to date environment variables, since it will be initialised when the server starts, rather than when the client executes. To circumvent this, mill’s client sends the environment variables to the server as it sees them, and the server makes them available as a Map[String, String] via the Ctx API.

所以读取最新的环境变量很简单:

def envVar = T.input { T.ctx.env.get("ENV_VAR") }

但是任务如何做到这一点:“mill 的客户端将环境变量发送到服务器”以便下一个 运行 任务将看到更新的环境变量?

说的差不多了。但更清楚的是:每次您 运行 一个 mill 命令时,mill 客户端都会启动,因此它也可以看到当前的 OS 环境变量。正是这个客户端捕获了这些变量,并在执行任务图之前将它们发送到已经 运行ning Mill 服务器。

此外,要使其工作,必须使用上下文 API 通过 T.ctx.env 访问系统环境变量。由于此上下文是我的 Mill 提供的,Mill 服务器可以访问最新的环境变量。相反,如果您使用 Javas System.getenv API,您可能会看到可能已过时的变量(恰好是服务器启动时的最新变量)。