0

I have a common target file that has AfterBuild . The target file is at solution level. All csproj Import custom target file using <Import Project=$(SolutionDir)\custom.targets file. But the afterbuild in custom.targets never run for any of the csproj. What am I missing here ?

Custom Targets File has the below

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <OutputTargets Include="$(TargetDir)$(TargetName).???" />
    <OutputSatellites Include="$(TargetDir)\**\$(TargetName).resources.dll" />
  </ItemGroup>
  <Target Name="AfterBuild">
    <Copy SourceFiles="@(OutputTargets)" DestinationFiles="@(OutputTargets->'$(SolutionDir)Test.WebApp\Config\%(RecursiveDir)%(Filename)%(Extension)')" />
    <Copy SourceFiles="@(OutputSatellites)" DestinationFiles="@(OutputSatellites->'$(SolutionDir)Test.WebApp\Config\%(RecursiveDir)%(Filename)%(Extension)')" />
  </Target>
</Project>

Above Target file is imported into each csproj within a solution.

varun7447
  • 574
  • 1
  • 6
  • 27
  • Can you should how you've done the "AfterBuild" target? Is it via DependsOnTargets, AfterTargets, or a target named "AfterBuild"? Where is the import in the proj relative to Microsoft.CSharp.targets? – Mike Zboray May 11 '17 at 02:18
  • It is a target name AfterBuild and it is after the CShsrp.targets in order of import – varun7447 May 11 '17 at 03:26
  • That sounds like it should work. A possible work around is my suggestion [here](http://stackoverflow.com/a/41476596/517852) to use AfterTargets="Build". – Mike Zboray May 11 '17 at 03:29
  • Aftertarget attribute in my .targets file that has AfterBuild target? Note target file is imported as part of csproj and I use msbuild to compile the solution file. – varun7447 May 11 '17 at 03:34
  • Yes AfterTargets is an attribute on the custom Target element. You can name the target anything you want then. – Mike Zboray May 11 '17 at 03:40
  • That did not work. For some reason when i am building the solution file. The target in the custom.targets file is not executed completely.Note my solution compile is in an .msbuild file. The custom.targets file is primarily used for importing into my csproj file. – varun7447 May 11 '17 at 04:40
  • Which vs version are you using? If vs 2017, class csproj or new-style SDK-based csproj? – Martin Ullrich May 11 '17 at 05:16
  • vs2015 csproj with Msbuild 2015 command line – varun7447 May 11 '17 at 05:18
  • How do you build the project? do you build the solution or the project itself? Depending on `$(SolutionDir)` is quite dangerous.. – Martin Ullrich May 12 '17 at 10:20
  • I build the entire solution. I tried various things like project directory. Relative path, – varun7447 May 12 '17 at 14:02
  • can you share the csproj file? is the import performed before or target the Microsoft.Common.targets? It has to be after the common targets or else the target will be overwritten. – Martin Ullrich May 13 '17 at 11:06
  • It is after Microsoft.Common.targets. I have one target file and one msbuild file. Msbuild file to compile solution and target file is imported in all csproj to copy the output of each project to the webapp project – varun7447 May 14 '17 at 02:19

1 Answers1

1

The Targets "AfterBuild" and "BeforeBuild" are overwritten by the Microsoft.Common.targets project. If the project is not included explicitly, it's automatically added as a header and trailer.

That way projects migrated to the new csproj format (and new projects with the new csproj format) can not specify "BeforeBuild" and "AfterBuild" targets that way.

Instead it's recommended you use the BeforeTargets or AfterTargets property to register your custom "PreBuild" and "PostBuild" targets as follows:

<Target Name="DefinitelyNotBeforeBuild" BeforeTargets="PreBuildEvent">
  <!-- BeforeBuild goes here -->
</Target>
<Target Name="DefinitelyNotAfterBuild" AfterTargets="PostBuildEvent">
  <!-- AfterBuild goes here -->
</Target>

make sure to not name these targets "BeforeBuild" and "AfterBuild". If you do, they'll be overwritten and this won't work.

See also the related github issue

Vogel612
  • 5,620
  • 5
  • 48
  • 73