如何在构建后事件中使用用户定义的变量?

10

我想在PostBuild事件中使用局部变量,但我不知道如何在其中使用。这是我的Post-Build事件命令(param是通过msbuild /p开关传递的命名参数):

set fold=$(TargetDir)
if defined param (set fold=$(TargetDir)$(param)\)
if not exist "%fold%" md "%fold%"
copy /y "$(TargetPath)" "%fold%"

构建解决方案时,我遇到了以下问题:

msbuild PrePostBuildEvents.sln /p:param=ext

...

PostBuildEvent:
  set fold=G:\prj\work\PrePostBuildEvents\bin\Debug\
  if defined param (set fold=G:\prj\work\PrePostBuildEvents\bin\Debug\ext\)
  if not exist "%fold%" md "%fold%"
  copy /y "G:\prj\work\PrePostBuildEvents\bin\Debug\PrePostBuildEvents.dll" "%fold%"
  The file cannot be copied onto itself.
          0 file(s) copied.

如果我将%fold%更改为$(fold),我会得到另一个结果,但它也是错误的:
PostBuildEvent:
  set fold=G:\prj\work\PrePostBuildEvents\bin\Debug\
  if defined param (set fold=G:\prj\work\PrePostBuildEvents\bin\Debug\ext\)
  if not exist "" md ""
  copy /y "G:\prj\work\PrePostBuildEvents\bin\Debug\PrePostBuildEvents.dll" ""
  The filename, directory name, or volume label syntax is incorrect.
          0 file(s) copied.

我做错了什么?

我离我的Windows机器有些远,但在VS中有一个资源选项卡,在项目属性中定义。你可以检查一下,或者等我接近我的Win机器时再看看。 - Mzf
我假设我只能在那里定义一个常量字符串,但我需要一个可计算的字符串,它取决于命名参数。 - stukselbax
1个回答

1
首先,使用AfterBuild msbuild目标而不是PostBuild事件。这将为msbuild提供更多关于您正在尝试执行的操作的信息,并且正确完成应该意味着更快的增量编译。
可以在AfterBuild事件中使用环境变量: http://msdn.microsoft.com/en-us/library/ms171459.aspx 理想情况下,一旦运行了msbuild一次,第二次运行时它应该跳过编译并不再复制文件,因为文件已经存在。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接