1

After creating a new .NET Standard class library project I'd like override the BeforeBuild msbuild task. Previously, I'd unload and edit the .csproj file and simply follow the instructions To modify your build process, add your task inside one of the targets below and uncomment it. and add my logic to the template there. The new .NET Standard project contains no such instructions.

Simply adding the task where it used to be has no effect. The reason is has no effect can be seen by inspecting the output of dotnet msbuild /pp run in the project directory. The task I added is redeclared later in the unified output which, I assume, overrides my task as no checks seem to be made to see if the task already exists.

The documentation I found here and here do not cover msbuild extensions of the project file.

Christopher King
  • 1,034
  • 1
  • 8
  • 21
  • 1
    is this any use to you? https://stackoverflow.com/questions/43921992/how-can-i-use-beforebuild-and-afterbuild-targets-with-visual-studio-2017 – Michael Coxon Aug 29 '17 at 16:34
  • Yes it does! Thank you very much and thanks to SO! – Christopher King Aug 29 '17 at 16:40
  • Are you interesting in creating a target or running command like tasks? (before/after build scripts). The 15.3 update for VS 2017 has a new editor that adds new targets automatically that are wired up correctly. – Martin Ullrich Aug 29 '17 at 17:32
  • @ChristopherKing, what about this issue? Would you please let me know the latest information about this issue? – Leo Liu Sep 01 '17 at 01:17
  • @Leo-MSFT, last I tried the fix I got a msbuild warning say that I double imported the `sdk.props` and `sdk.targets` and that it was likely an `authoring error`. So I guess MS still preforms the implicit imports even after I add the explicit ones. Wan't going to accept the answer until I was able to import without warnings. Do you see the warnings? – Christopher King Sep 06 '17 at 15:22

1 Answers1

2

The behaviour changed.

<Project Sdk="Microsoft.NET.Sdk">
    ...
</Project>

is expanded to

<Project>
    <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
    ...
    <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

If you want to override BeforeBuild, you will have to manually import the Sdk props and targets, but this is not recommended. The recommended solution is creating a target with BeforeTargets="Build".

EDIT: There's a discussion about BeforeBuild and AfterBuild on Sdk based projects here: https://github.com/Microsoft/msbuild/issues/1680

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
José Pedro
  • 1,097
  • 3
  • 14
  • 24