在 Makefile 中加载 .env 文件

Load .env file in Makefile

有没有一种简单的方法可以自动将包含本地配置的文件加载到 Makefile

我有一个文件,.env,每行包含 key=value 对:

var1=val1
var2=val2

目前我正在像这样手动将每个变量导入到我的 Makefile 中:

  var1=$$(grep '^var1=' .env | cut -d= -f2-)

添加更多变量时感觉笨拙且容易出错。

我也试过向 Makefile 添加一个额外的目标来读取 .env 的每一行并将其传递给 export,但是在一个目标中导出的值不可用于其他

我希望在 make 中有执行此操作的内置功能,但我无法找到它的文档。

假设您的包含文件完全由有效的 make 分配组成,那么 include directive 可能就是您想要的。

The include directive tells make to suspend reading the current makefile and read one or more other makefiles before continuing. The directive is a line in the makefile that looks like this:

include filenames…

filenames can contain shell file name patterns.