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

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


当前回答

或者(因为事件被放入一个批处理文件中,然后被调用),使用以下(在构建事件框中,而不是在批处理文件中):

if $(ConfigurationName) == Debug goto :debug

:release
signtool.exe ....
xcopy ...

goto :exit

:debug
' Debug items in here

:exit

通过这种方式,您可以拥有任何配置的事件,并且仍然使用宏来管理它,而不必将它们传递到批处理文件中,记住%1是$(OutputPath)等。

其他回答

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错误。

在Visual Studio 2012中,你必须使用(我认为在Visual Studio 2010中也是如此)

if $(Configuration) == Debug xcopy

$(ConfigurationName)被列为宏,但没有分配。

比较:构建命令和属性的宏

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

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

生成前和生成后事件作为批处理脚本运行。您可以在$(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>

我更喜欢第二种选择。