如何将我的“后生成事件”限制为仅针对一种类型的生成运行?
我正在使用这些事件将DLL文件复制到本地IIS虚拟目录,但我不希望在发布模式下在构建服务器上发生这种情况。
如何将我的“后生成事件”限制为仅针对一种类型的生成运行?
我正在使用这些事件将DLL文件复制到本地IIS虚拟目录,但我不希望在发布模式下在构建服务器上发生这种情况。
预构建和后构建事件作为批处理脚本运行。您可以在$(ConfigurationName)
上进行条件语句。
例如
if $(ConfigurationName) == Debug xcopy something somewhere
提醒一下,您不需要使用goto语句。在shell中可以使用IF命令和圆括号:
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".
)
"$(ConfigurationName)"
(注意引号) - jgauffin$(ConfigurationName)
是空的(后期生成事件命令行)。if "$(Configuration)" == "Debug"
对我有用。顺便说一句,如果您想在所有其他配置中执行某些操作,请使用 if NOT "$(Configuration)" == "Debug"
。 - Ralf Hundewadt按照常规方式添加您的构建后事件。然后保存您的项目,在記事本(或您喜欢的编辑器)中打开,并将条件添加到PostBuildEvent属性组中。下面是一个示例:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PostBuildEvent>start gpedit</PostBuildEvent>
</PropertyGroup>
cd "$(ProjectDir)"
扩展为 cd ""
。 - Darryl<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="$(ConfigurationName) == Debug"> <Exec Command="your command"/></Target>
实现这一点。宏变量和所有功能都像平常一样。 - S.C.或者(因为事件被放置在批处理文件中然后调用),使用以下内容(在“生成事件”框中,而不是在批处理文件中):
if $(ConfigurationName) == Debug goto :debug
:release
signtool.exe ....
xcopy ...
goto :exit
:debug
' Debug items in here
:exit
这种方式可以为任何配置生成事件,并仍然使用宏进行管理,而不必将它们传递到批处理文件中。请记住,%1
是$(OutputPath)
等等。
if
并使用 goto :$(ConfigurationName)
。 (注:该句话是关于编程的,为了保持准确性和简洁性,没有进行更多的解释) - Calimero100582从Visual Studio 2019开始,现代的.csproj
格式支持在Target
元素上直接添加条件:
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Debug'">
<Exec Command="nswag run nswag.json" />
</Target>
用户界面没有提供设置此项的方法,但如果您通过用户界面进行更改,则似乎安全地保留Configuration
属性。
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.
$(ConfigurationName)
传递配置名称。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。
我发现可以像这样在项目文件中放置多个条件:
<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>
这在我的Visual Studio 2015中有效。
我将位于库文件夹中的一个文件夹中的所有DLL文件复制到正在构建的项目的目标目录中,该文件夹与我的解决方案文件夹处于同一级别。
使用相对路径从我的项目目录开始,并向上两步进入文件夹结构..\..\lib
我的解决方案文件夹
....我的项目
Lib
if $(ConfigurationName) == Debug (
xcopy /Y "$(ProjectDir)..\..\lib\*.dll" "$(TargetDir)"
) ELSE (echo "Not Debug mode, no file copy from lib")