【问题标题】:Incremental build in .NET Core & Visual Studio.NET Core 和 Visual Studio 中的增量构建
【发布时间】:2019-02-22 00:58:44
【问题描述】:

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

<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) 项目,但结果相同。

问题:

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 2022-10-31
      • 1970-01-01
      相关资源
      最近更新 更多