Search code examples
visual-studiomsbuildvisual-studio-2017msbuild-task

Import targets file to extend build targets in VS2017


I am trying to extend the build functionality with VS2017. I've added following code to the bottom of the proj file, just before </Project> tag:

  <Target Name="PrintA" BeforeTargets="Build">
    <Message Text="AAAAAAAAAAAAAa" Importance="high" />
  </Target>

That works as expected. Now I would like to create separated targets file - build.targets and add the build logic there. So, I've created build.targets file in the solution root folder with following code:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="PrintB" BeforeTargets="Build">
    <Message Text="BBBBBBBBBBBBBBBBBBBBB" Importance="high"/>
  </Target>
</Project>

And then added import to the proj file also:

Then I build the project the AAAAAA is printed and BBBBB is not. What do I miss?

UPDATED: This is the build log(I am unable to paste full log, I think that only this info can be relevant):

    Task "WriteLinesToFile" skipped, due to false condition; ('@(_CleanUnfilteredPriorFileWrites)'!='@(_CleanUniqueRemainingFileWritesAfterIncrementalClean)') was evaluated as ('C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\Database2.dacpac;C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\Database2.dll;C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\Database2.pdb;C:\Users\kultasev\source\repos\Database2\Database2\obj\Debug\Database2.dll;C:\Users\kultasev\source\repos\Database2\Database2\obj\Debug\Database2.pdb'!='C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\Database2.dacpac;C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\Database2.dll;C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\Database2.pdb;C:\Users\kultasev\source\repos\Database2\Database2\obj\Debug\Database2.dll;C:\Users\kultasev\source\repos\Database2\Database2\obj\Debug\Database2.pdb').
Target "PostBuildEvent" skipped, due to false condition; ('$(PostBuildEvent)' != '' and ('$(RunPostBuildEvent)' != 'OnOutputUpdated' or '$(_AssemblyTimestampBeforeCompile)' != '$(_AssemblyTimestampAfterCompile)')) was evaluated as ('' != '' and ('' != 'OnOutputUpdated' or '' != '')).
Target "AfterBuild" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets" from project "C:\Users\kultasev\source\repos\Database2\Database2\Database2.sqlproj" (target "Build" depends on it):
Target "_CheckForInvalidConfigurationAndPlatform" skipped. Previously built successfully.
Target "_CheckForInvalidConfigurationAndPlatform" skipped. Previously built successfully.
Target "PrintA" in project "C:\Users\kultasev\source\repos\Database2\Database2\Database2.sqlproj" (target "Build" depends on it):
    Task "Message"
        Task Parameter:Text=AAAAAAAAAAAAAa
        Task Parameter:Importance=high
        AAAAAAAAAAAAAa
Target "PrintB" in file "C:\Users\kultasev\source\repos\Database2\Database2\build.targets" from project "C:\Users\kultasev\source\repos\Database2\Database2\Database2.sqlproj" (target "Build" depends on it):
    Task "Message"
        Task Parameter:Text=BBBBBBBBBBBBBBBBBBBBB
        BBBBBBBBBBBBBBBBBBBBB
Target "Build" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v15.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" from project "C:\Users\kultasev\source\repos\Database2\Database2\Database2.sqlproj" (entry point):
    Task "CallTarget" skipped, due to false condition; ('$(UnloadProjectsOnCompletion)'=='true') was evaluated as (''=='true').
    Task "CallTarget" skipped, due to false condition; ('$(UnloadProjectsOnCompletion)'=='true') was evaluated as (''=='true').
    Task "CallTarget" skipped, due to false condition; ('$(UnloadProjectsOnCompletion)'=='true') was evaluated as (''=='true').
Target "CleanupEmptyRefsFolder" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\Managed.Web\Microsoft.Web.IISSupport.targets" from project "C:\Users\kultasev\source\repos\Database2\Database2\Database2.sqlproj" (entry point):
    Set Property: _RefsFolderFullPath=C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\
    Added Item(s): 
    _FilesInRefsFolder=
        C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\\Database2.dacpac
        C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\\Database2.dll
        C:\Users\kultasev\source\repos\Database2\Database2\bin\Debug\\Database2.pdb
    Task "RemoveDir" skipped, due to false condition; (@(_FilesInRefsFolder ->Count()) == 0) was evaluated as (3 == 0).
Done building project "Database2.sqlproj".

Solution

  • Note that the classic project system in VS 2017 doesn't reload imported files during builds even if they change. You need to at least close and re-open the solution to make it work.

    For "SDK-style" projects (.NET Standard/Core, ASP.NET Core) in VS 2017 15.3+, this is handled by the new project system (GitHub issue).