I have the following Targets file that is imported into my .csproj
file, One of the targets (AfterAddPostAction) never fires. Why not?
(Sorry it's so verbose but MSBuild is shit at abstraction and CallTask doesn't see Property values set inside the Target containing the CallTask element.)
<?xml version="1.0" encoding="Windows-1252"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="EstablishLog">
<MakeDir Condition="!Exists('$(MSBuildProjectDirectory)\Logs')" Directories=".\Logs"/>
<PropertyGroup>
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellExe>
<ScriptPath Condition=" '$(ScriptPath)'=='' ">C:\Users\Admin\Documents\GitHub\powershell-scripts\</ScriptPath>
<LogState>$(ScriptPath)ProjectSnapShot.ps1</LogState>
<DoPostAction>$(ScriptPath)postAction-BeforePublish.ps1</DoPostAction>
<Switches>-NonInteractive -executionpolicy Unrestricted</Switches>
<Arguments>"& { &'$(ScriptPath)' } "</Arguments>
</PropertyGroup>
</Target>
<Target Name="AfterClean" DependsOnTargets="EstablishLog">
<PropertyGroup>
<LogFile >AfterClean$(ApplicationVersion).log</LogFile>
<LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs\$(LogFile)</LogFile>
<Arguments>"& { &'$(LogState)' } "</Arguments>
</PropertyGroup>
<Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) > $(LogFile)" />
</Target>
<Target Name="BeforeBuild" DependsOnTargets="EstablishLog">
<PropertyGroup>
<LogFile >BeforeBuild$(ApplicationVersion).log</LogFile>
<LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs\$(LogFile)</LogFile>
<Arguments>"& { &'$(LogState)' } "</Arguments>
</PropertyGroup>
<Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) > $(LogFile)" />
</Target>
<Target Name="AfterBuild" DependsOnTargets="EstablishLog">
<PropertyGroup>
<LogFile >AfterBuild$(ApplicationVersion).log</LogFile>
<LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs\$(LogFile)</LogFile>
<Arguments>"& { &'$(LogState)' } "</Arguments>
</PropertyGroup>
<Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) > $(LogFile)" />
</Target>
<Target Name="BeforePublish" DependsOnTargets="EstablishLog">
<PropertyGroup>
<LogFile >BeforePublish$(ApplicationVersion).log</LogFile>
<LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs\$(LogFile)</LogFile>
<Arguments>"& { &'$(LogState)' } "</Arguments>
</PropertyGroup>
<Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) > $(LogFile)" />
</Target>
<Target Name="AddPostAction" AfterTargets="BeforePublish" DependsOnTargets="EstablishLog">
<PropertyGroup>
<PostAction>FileCopyPDA.FileCopyPDA</PostAction>
<Arguments>"& { &'$(DoPostAction)' '$(PostAction)' $(Configuration)} "</Arguments>
<LogFile >AddPostAction$(ApplicationVersion).log</LogFile>
<LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs\$(LogFile)</LogFile>
</PropertyGroup>
<Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) > $(LogFile)" />
</Target>
<!--This one is never called-->
<Target Name="AfterAddPostAction" DependsOnTargets="EstablishLog;AddPostAction">
<PropertyGroup>
<LogFile >AfterAddPostAction$(ApplicationVersion).log</LogFile>
<LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs\$(LogFile)</LogFile>
<Arguments>"& { &'$(LogState)' } "</Arguments>
</PropertyGroup>
<Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) > $(LogFile)" />
</Target>
<Target Name="AfterPublish" DependsOnTargets="EstablishLog">
<PropertyGroup>
<LogFile >AfterPublish$(ApplicationVersion).log</LogFile>
<LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs\$(LogFile)</LogFile>
<Arguments>"& { &'$(LogState)' } "</Arguments>
</PropertyGroup>
<Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) > $(LogFile)" />
</Target>
</Project>
DependsOnTargets
is the primary way to chain tasks into the sequence. But if you have sequence A->B->C
implemented via DependsOnTargets
(B
depends on A
) and call target A
, then B
and C
will not be executed. But if you call C
, then both A
and B
are executed.
On the contrary the target that has target A
mentioned in AfterTargets
attribute will be executed after A
is executed.
That's why in your case if you want to use DependsOnTargets
it is important which target you execute.