I have a NuGet package that adds to my C# projects a targets file with that content for the post build event.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="ThisIsMyTarget" AfterTargets="Build">
<Exec Command="ThisIsMyCommand.exe"/>
</Target>
</Project>
In less than 1% of the projects, I don't need this command, I need another command to be executed. Is it possible with the targets files to suspend a target from another targets file?
Something like this:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="ThisIsMyRareTarget" Overwrite="ThisIsMyTarget" AfterTargets="Build">
<Exec Command="ThisIsMyRareCommand.exe"/>
</Target>
</Project>
I don't want to split my NuGet package only for the 1% of the project.
Overwrite targets post build event of another targets file
Actually, if you want to use multiple target files into nuget package and then switch between different target files depending on your needs, you cannot get what you want.
And as this document said, when you want to add a target file into a project by nuget, you should make sure the name of the target file is the same as the name of the nuget package so that it will work.
So you can only use one target file named <package_id>.targets
and set a condidtion
in it to distinguish which project environment to use.
Solution
1) please put these all in your <package_id>.targets
file:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Flag></Flag>
</PropertyGroup>
<Target Name="ThisIsMyTarget" AfterTargets="Build" Condition="'$(Flag)'!='true'">
<Exec Command="ThisIsMyCommand.exe"/>
</Target>
<Target Name="ThisIsMyRareTarget" AfterTargets="Build" Condition="'$(Flag)'=='true'">
<Exec Command="ThisIsMyRareCommand.exe"/>
</Target>
</Project>
2) then you pack your nuget project and when you install your nuget in other projects(less than 1% of the projects), you should define the property Flag to true
on the end of the xxx.csproj
file like this:
And less than 1% projects can use ThisIsMyRareTarget.exe
. In other 99% projects, you should not define Flag property in xxx.csproj
and it will automatic capture ThisIsMyCommand.exe
.