在输出不变时构建 visual studio?

Make visual studio build when output won't change?

在我的项目中,我有一个 json 文件用于配置,我将 git 设置为忽略。首次克隆存储库时,作为项目一部分并复制到输出目录的配置文件不存在。我已经使用项目中 'BeforeBuild' 目标中的任务实现了这一点,如果示例文件不存在,它会将示例文件复制到实际配置文件中。

<Target Name="BeforeBuild">
  <ItemGroup>
    <MySourceFiles Include="Configuration.sample.json" />
  </ItemGroup>
  <ItemGroup>
    <MyDestinationFiles Include="Configuration.json" />
  </ItemGroup>
  <Message Importance="high" Condition="!Exists('@(MyDestinationFiles)')" 
           Text="Copying @(MySourceFiles) to @(MyDestinationFiles)" />
  <Copy Condition="!Exists('@(MyDestinationFiles)')" 
        SourceFiles="@(MySourceFiles)" 
        DestinationFiles="@(MyDestinationFiles)" />
</Target>

因此,如果我构建项目,然后删除配置文件并进行构建,则不会发生任何事情,因为我认为没有进行任何会改变输出的更改。有没有办法更改项目文件,以便根据需要标记构建?它不应该经常出现,我总是可以手动执行 'Clean' 或 'Rebuild',但它一直困扰着我,因为我刚刚开始学习 MSBuild 文件。

来自目标 Outputs 属性的 documentation

The files that form outputs into this target. Multiple files are separated by semicolons. The timestamps of the files will be compared with the timestamps of files in Inputs to determine whether the Target is up to date

因此,如果您将 Beforebuild 目标创建的输出文件的路径添加到它的 Outputs 属性,则在每次构建开始时,msbuild 将检查这些文件是否存在,如果不存在,它将开始构建,因为现在项目是被认为不再是最新的。实践中使用:

<Target Name="BeforeBuild" Outputs="@(MyDestinationFiles)">