How can I limit my post-build events to running only for one type of build?
I'm using the events to copy DLL files to a local IIS virtual directory, but I don't want this happening on the build server in release mode.
FYI, you do not need to use goto. The shell IF command can be used with round brackets:
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".
)
Add your post build event like normal. Then save your project, open it in Notepad (or your favorite editor), and add condition to the PostBuildEvent property group. Here's an example:
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PostBuildEvent>start gpedit</PostBuildEvent>
</PropertyGroup>
Alternatively (since the events are put into a batch file and then called), use the following (in the Build event box, not in a batch file):
if $(ConfigurationName) == Debug goto :debug
:release
signtool.exe ....
xcopy ...
goto :exit
:debug
' Debug items in here
:exit
This way you can have events for any configuration, and still manage it with the macros rather than having to pass them into a batch file, remember that %1
is $(OutputPath)
, etc.
As of Visual Studio 2019, the modern .csproj
format supports adding a condition directly on the Target
element:
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Debug'">
<Exec Command="nswag run nswag.json" />
</Target>
The UI doesn't provide a way to set this up, but it does appear to safely leave the Configuration
attribute in place if you make changes via the UI.
This works for me in Visual Studio 2015.
I copy all DLL files from a folder located in a library folder on the same level as my solution folder into the targetdirectory of the project being built.
Using a relative path from my project directory and going up the folder structure two steps with..\..\lib
MySolutionFolder
....MyProject
Lib
if $(ConfigurationName) == Debug (
xcopy /Y "$(ProjectDir)..\..\lib\*.dll" "$(TargetDir)"
) ELSE (echo "Not Debug mode, no file copy from lib")