MSBuild Targets - How to detect changes to .csproj

2019-09-18 03:35发布

问题:

What I'm trying to accomplish is detecting changes to projects during build so I can set a flag indicating that the project was changed. I've already figured out how to detect when a C# component has changed by utilizing incremental builds and CoreCompileDependsOn. But this doesn't seem to trigger when a content file of a project has changed. From what I understand, there would be a Copy task somewhere specifying SkipUnchangedFiles="true" depending on the setting of the content file within the project, Copy to Output Directory (Do not copy, Copy Always, Copy if Newer). I'm not entirely sure where this process happens, possibly the Microsoft.CSharp.targets file? Could someone please advise as to how I would add a dependent task that executes only if an updated file was copied to the bin output? A very simple example would suffice.

回答1:

I figured it out. This will write a text file with the name of the project ONLY if any content files that have 'Copy If Newer' set have been changed during a build:

      <Target Name="ContentFilesUpdated" AfterTargets="_CopyOutOfDateSourceItemsToOutputDirectory" Condition="'$(IsDesktopBuild)' == 'False'">
    <WriteLinesToFile  
        File="@(ProductChangeFile)"  
        Lines="$(ProjectName)"  
        Overwrite="false"
        Encoding="Unicode" Condition="!Exists('@(ProductChangeFile)')"/>