如何将构建后事件限制为仅为一种类型的构建运行?
我正在使用事件将DLL文件复制到本地IIS虚拟目录,但我不希望在发布模式下的构建服务器上发生这种情况。
您可以将配置名称传递给构建后脚本,并在那里检查它是否应该运行。
传递配置名$(ConfigurationName)。
$(ConfigurationName)
检查它取决于您如何实现构建后步骤——它将是一个命令行参数。
像往常一样添加您的post build事件。然后保存项目,在记事本(或您喜欢的编辑器)中打开它,并向PostBuildEvent属性组添加条件。这里有一个例子:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PostBuildEvent>start gpedit</PostBuildEvent> </PropertyGroup>
生成前和生成后事件作为批处理脚本运行。您可以在$(ConfigurationName)上执行条件语句。
例如
if $(ConfigurationName) == Debug xcopy something somewhere
与任何项目设置一样,构建事件可以在每个Configuration中配置。只需在“属性页”对话框的下拉菜单中选择您想要更改的配置,并编辑后构建步骤。
或者(因为事件被放入一个批处理文件中,然后被调用),使用以下(在构建事件框中,而不是在批处理文件中):
if $(ConfigurationName) == Debug goto :debug :release signtool.exe .... xcopy ... goto :exit :debug ' Debug items in here :exit
通过这种方式,您可以拥有任何配置的事件,并且仍然使用宏来管理它,而不必将它们传递到批处理文件中,请记住%1是$(OutputPath),等等。
%1
$(OutputPath)
供你参考,你不需要使用goto。shell 如果命令可以与圆括号一起使用:
if $(ConfigurationName) == Debug ( copy "$(TargetDir)myapp.dll" "c:\delivery\bin" /y copy "$(TargetDir)myapp.dll.config" "c:\delivery\bin" /y ) ELSE ( echo "why, Microsoft, why". )
在Visual Studio 2012中,你必须使用(我想在Visual Studio 2010中也是如此)
if $(Configuration) == Debug xcopy
$(ConfigurationName)被列为宏,但它没有被分配。
比较:# EYZ0
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 2015中为我工作。
我将所有DLL文件从位于与我的解决方案文件夹同一级别的库文件夹中的文件夹复制到正在构建的项目的目标目录。
使用项目目录中的相对路径,用..\..\lib两步往上走文件夹结构
if $(ConfigurationName) == Debug ( xcopy /Y "$(ProjectDir)..\..\lib\*.dll" "$(TargetDir)" ) ELSE (echo "Not Debug mode, no file copy from lib")
从Visual Studio 2019开始,现代的.csproj格式支持直接在Target元素上添加条件:
.csproj
Target
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Debug'"> <Exec Command="nswag run nswag.json" /> </Target>
UI没有提供设置这个的方法,但是如果您通过UI进行更改,它似乎可以安全地将Configuration属性保留在适当的位置。
Configuration
我发现我可以在项目文件中添加多个条件,就像这样:
<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>
截至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>
我更喜欢2选择<强> < / >强。