.NET Core 中的增量构建 & Visual Studio

Incremental build in .NET Core & Visual Studio

这个玩具项目没有有效的增量构建:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <Timestamp>$([System.DateTime]::Now.ToString("yyyy-MM-dd\THHmmss"))</Timestamp>
  </PropertyGroup>

  <ItemGroup>
    <MyInputs Include="myinput.txt" />
    <MyOutput Include="myoutput.txt" />
  </ItemGroup>

  <Target
    Name="test_BeforeTargets_BeforeBuild"
    BeforeTargets="BeforeBuild"
    Inputs="@(MyInputs)"
    Outputs="@(MyOutput)"
  >
    <Message Text="test_BeforeTargets_BeforeBuild" Importance="high" />
    <WriteLinestoFile File="myoutput.txt" Lines="$(Timestamp)" Overwrite="true" />
  </Target>
</Project>

我在 Visual Studio 中注意到了 3 个构建结果:

  1. myinput.txt.csproj 文件被修改时,我得到了我想要的结果:

    1>Target "test_BeforeTargets_BeforeBuild" in file "C:\Users\dan\source\repos\TestMSBuild\TestMSBuild.csproj":
    1>  Building target "test_BeforeTargets_BeforeBuild" completely.
    1>  Input file "myinput.txt" is newer than output file "myoutput.txt".
    1>  Task "Message"
    1>    Task Parameter:Text=test_BeforeTargets_BeforeBuild
    1>    Task Parameter:Importance=high
    1>    test_BeforeTargets_BeforeBuild
    1>  Done executing task "Message".
    1>  Task "WriteLinestoFile"
    1>    Task Parameter:File=myoutput.txt
    1>    Task Parameter:Lines=2019-02-21T163909
    1>    Task Parameter:Overwrite=True
    1>  Done executing task "WriteLinestoFile".
    1>Done building target "test_BeforeTargets_BeforeBuild" in project "TestMSBuild.csproj".
    
  2. myinput.txt没有被修改,但是.csproj被修改了。我得到了预期的目标跳过:

    1>Target "test_BeforeTargets_BeforeBuild" in file "C:\Users\dan\source\repos\TestMSBuild\TestMSBuild.csproj":
    1>  Skipping target "test_BeforeTargets_BeforeBuild" because all output files are up-to-date with respect to the input files.
    1>  Input files: myinput.txt
    1>  Output files: myoutput.txt
    1>Done building target "test_BeforeTargets_BeforeBuild" in project "TestMSBuild.csproj".
    
  3. 如果我在不修改任何东西的情况下构建两次,然后只修改 myinput.txt,无论我尝试构建多少次,我都会得到一个单行输出:

    ========== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========
    

我预计当我只修改myinput.txt时,目标不会被跳过。目前,只有在我同时修改 .csproj 文件时才会出现这种情况。

尝试了 BeforeTargets 和 AfterTargets 的以下变体但无济于事:

BeforeTargets="BeforeBuild" (as above)
BeforeTargets="Build"
AfterTargets="BeforeBuild"
AfterTargets="Build"

我只尝试过 .NET-Core (Microsoft.NET.Sdk) 和 ASP.NET-Core (Microsoft.NET.Sdk.Razor & Microsoft.NET.Sdk.Web) 项目,但结果相同。

问题:

我怎样才能让这个玩具项目发挥作用?

虽然基于 MSBuild 的最新检查按预期工作,但还执行了额外的最新检查 Visual Studio 以确定它是否应调用 MSBuild。由于它不知道您的输入文件,因此它确定不需要 运行 该项目上的 MSBuild。

这可以通过使用项目系统用于此检查的 UpToDateCheckInputUpToDateCheckOutput 项告诉 VS 来更改:

<ItemGroup>
  <UpToDateCheckInput Include="@(MyInputs)" />
  <UpToDateCheckOutput Include="@(MyOutputs)" />
</ItemGroup>