【问题标题】:Makefile for web project: how to use placeholders properly?Web 项目的 Makefile:如何正确使用占位符?
【发布时间】:2026-01-12 12:15:01
【问题描述】:

应该很简单。我有一个 web 项目的 makefile(不要问我为什么:):

solution = solution_name
common = app css js/*.js js/app js/locale #...
private = app css image js template #...

all:
  @echo Solution '${solution}'
  @tar cvfz ${solution}.tgz --exclude=.svn \
  $(addprefix ../../, ${common}) \
  ${private}

我的目标是在 app.js 文件中包含 make-variable %VERSION%,我可以将其替换并包含到存档中,而不是更改原始文件(以便将来使用此变量)。目前我发现的只是

sed -i -r "s/%VERSION%/`svnversion`/" js/app.js

这段代码完成了一半的工作。处理后修改线路,第二次无法使用set。我怎样才能实现我的目标?谢谢。

【问题讨论】:

    标签: sed makefile


    【解决方案1】:

    我使用以下技巧来解决此类问题:

    sed -i 's# *=.*//.*VERSION.*# = "`get_version`" // comment with word VERSION inside it#' myfile.ext
    

    在 myfile.ext 我有:

    some_variable = '1.2.3.45-r67' // comment with word VERSION inside it
    

    所以每次我运行 sed 时,它都会获得实际版本(日期、时间、分支等),并且可以根据需要经常重复

    【讨论】: