如何将构建后事件限制为仅为一种类型的构建运行?

我正在使用事件将DLL文件复制到本地IIS虚拟目录,但我不希望在发布模式下的构建服务器上发生这种情况。


当前回答

生成前和生成后事件作为批处理脚本运行。您可以在$(ConfigurationName)上执行条件语句。

例如

if $(ConfigurationName) == Debug xcopy something somewhere

其他回答

截至2022年,我已经找到了两个解决方案。在我的特定情况下,我想打包到不同的目录,这取决于Configuration。

选项1

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="if $(Configuration) == Debug (dotnet pack --no-build -o ~/../../../../../nuget-repo/debug -p:PackageVersion=$(VersionInfo)) else (dotnet pack --no-build -o ~/../../../../../nuget-repo -p:PackageVersion=$(VersionInfo))" />
</Target>

选项2

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Condition="'$(Configuration)' == 'Debug'" Command="dotnet pack --no-build -o ~/../../../../../nuget-repo/debug -p:PackageVersion=$(VersionInfo)" />
    <Exec Condition="'$(Configuration)' == 'Release'" Command="dotnet pack --no-build -o ~/../../../../../nuget-repo -p:PackageVersion=$(VersionInfo)" />
</Target>

我更喜欢第二种选择。

生成前和生成后事件作为批处理脚本运行。您可以在$(ConfigurationName)上执行条件语句。

例如

if $(ConfigurationName) == Debug xcopy something somewhere

我发现我可以在项目文件中添加多个条件,就像这样:

  <Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' != 'Debug' AND '$(Configuration)' != 'Release' ">
      <Exec Command="powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File $(ProjectDir)postBuild.ps1 -ProjectPath $(ProjectPath) -Build $(Configuration)" />
  </Target>

像往常一样添加您的post build事件。然后保存项目,在记事本(或您喜欢的编辑器)中打开它,并向PostBuildEvent属性组添加条件。这里有一个例子:

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <PostBuildEvent>start gpedit</PostBuildEvent>
</PropertyGroup>

Visual Studio 2015:正确的语法是(保持在一行上):

if "$(ConfigurationName)"=="My Debug CFG" ( xcopy "$(TargetDir)test1.tmp" "$(TargetDir)test.xml" /y) else ( xcopy "$(TargetDir)test2.tmp" "$(TargetDir)test.xml" /y)

这里没有255错误。