问题
Project file
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<GenerateFile>DataFileList.txt</GenerateFile>
</PropertyGroup>
<ItemGroup>
<DataFiles Include="**\*.dat" />
</ItemGroup>
<ItemGroup>
<UpToDateCheckInput Include="@(DataFiles)" />
<UpToDateCheckBuilt Include="$(OutputPath)$(GenerateFile)" />
</ItemGroup>
<Target Name="TestBuild" AfterTargets="Build" Inputs="@(DataFiles)" Outputs="$(OutputPath)$(GenerateFile)">
<Message Text="**** TestBuild ****" />
<WriteLinesToFile File="$(OutputPath)$(GenerateFile)" Lines="@(DataFiles)" Overwrite="true" />
</Target>
</Project>
What I have tried
The operation sequence and results are shown below.
First of all, basic operation when rebuild and build are repeated.
Rebuild-> build run and "TestBuild" target executed 🆗Build-> build run and "TestBuild" target skipped 🆗Buildonce more -> build skipped 🆗
Next, update the dat file and build the result as shown below.
Build-> build run and "TestBuild" target executed 🆗Buildonce more -> build run and "TestBuild" target skipped 🆖Buildonce more -> build run and "TestBuild" target skipped 🆖 (Also, the build always runs after this)
The results I expect are below.
Buildonce more -> build skipped
Questions
- UpToDateCheckBuilt doesn't seem to work, but I want to know if it is.
- I want to know the correct way to write UpToDateCheckBuilt.
回答1:
I want to know the correct way to write UpToDateCheckBuilt.
Try changing the part of the content in your script from:
<ItemGroup>
<UpToDateCheckInput Include="@(DataFiles)" />
<UpToDateCheckBuilt Include="$(OutputPath)$(GenerateFile)" />
</ItemGroup>
To:
<ItemGroup>
<!--<UpToDateCheckInput Include="@(DataFiles)" />-->
<UpToDateCheckBuilt Include="$(OutputPath)$(GenerateFile)" Original="@(DataFiles)"/>
</ItemGroup>
UpToDateCheckBuilt doesn't seem to work, but I want to know if it is.
The reason of this strange behavior in step 5 and 6 is more related to <UpToDateCheckInput Include="@(DataFiles)" />. Your TestBuild target's Up-To-Date check works well, the reason why build always start after step4 is you set the xx.dat as the input file for the whole project.
Description: In VS IDE, it has a Up-To-Date check for the whole project.
During build process of .net core app, there's a CoreCompile target which works to compile the source code(xx.cs) to output Application.dll or Aplication.exe.
For this important target, its input files are source files(xx.cs) and referenced assemblies.
1.After you do some changes to xx.dat file,since this file is input file of the project after you use UpToDateCheckInput and it's newer than output files, the build will execute in step4. And your TestBuild target will also run since the xx.dat is also the input file for itself. But since this file is not the input file for CoreCompile target, the output xx.exe or xx.dll are up-to-date with respect to the input files(xx.cs, referenced dlls), this target won't run! It means, the build of the project starts in step4 while vs won't compile the source file again, so the output xx.exe or xx.dll is always the original one.
2.Then, after step4, no matter how many times you try to build the project, the xx.dat is always newer than the original output assembly, then the build will always start.
来源:https://stackoverflow.com/questions/57510376/uptodatecheckbuilt-does-not-work-and-the-build-always-works