VS2010:后期构建事件中可以有多个if吗?

20

我们可以有类似这样的东西吗:

if "Debug"=="$(ConfigurationName)"
(
  goto :nocopy
)
else if "Release"=="$(ConfigurationName)"
(
  del "$(TargetPath).config"
  copy "$(ProjectDir)\App.Release.config" "$(TargetPath).config"
)
else if "ReleaseBeta"=="$(ConfigurationName)"
(
  del "$(TargetPath).config"
  copy "$(ProjectDir)\App.ReleaseBeta.config" "$(TargetPath).config"
)
else if "ReleaseProduction"=="$(ConfigurationName)"
(
  del "$(TargetPath).config"
  copy "$(ProjectDir)\App.ReleaseProduction.config" "$(TargetPath).config"
)
    :nocopy

我已经尝试过了,但它不能工作。错误代码是255。

2个回答

35
你可以有任意数量的条件语句,只需用换行符分隔它们,并省略else关键字。
所以修改为:
if "Debug"=="$(ConfigurationName)"
(
  goto :nocopy
)
else if...

To

:(待补充具体上下文)
if "Debug" == "$(ConfigurationName)" (goto :nocopy)
if "Release" ==" $(ConfigurationName)" (
    del "$(TargetPath).config"
    copy "$(ProjectDir)\App.Release.config" "$(TargetPath).config" )
if ...

它会编译并可以正常运行。

注意:命令将逐行解释,就像DOS批处理文件一样,因此将左括号“(”放在if语句的同一行,并将右括号“)”放在块中最后一个命令的同一行非常重要。


7
如果您的后构建逻辑变得复杂,我建议将其移动到一个外部文件中。例如,下面是一个后构建事件:
CALL "$(ProjectDir)PostBuild.cmd" $(ConfigurationName)

在项目目录中执行一个批处理文件PostBuild.cmd,并将$(ConfigurationName)作为参数传递。您还可以传递其他参数,例如$(TargetPath)。

然后,您可以实现任何想要的内容,包括多个if语句,并且更重要的是,在不运行Visual Studio构建的情况下进行调试。


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