.NET Core和Visual Studio中的增量生成

时间:2019-02-22 00:58:44

标签: asp.net-core .net-core msbuild incremental-build

此玩具项目没有有效的增量版本:

<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.RazorMicrosoft.NET.Sdk.Web)项目,但结果却相同。

问题:

我如何使这个玩具项目正常工作?

1 个答案:

答案 0 :(得分:1)

尽管基于MSBuild的最新检查可以按预期进行,但是Visual Studio还执行了另一项最新检查,以确定是否应调用MSBuild。由于它不知道您的输入文件,因此它确定不需要在该项目上运行MSBuild。

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

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