简体   繁体   English

.NET Core 2.1:如何在Visual Studio 2017中进行调试期间触发在项目文件中定义的复制任务?

[英].NET Core 2.1 : How to trigger Copy task defined in the project file during debugging in the Visual Studio 2017?

There are some files residing in other directories that, I would like to copy to project folder automatically before build and publishing. 有些文件位于其他目录中,我想在构建和发布之前自动将其复制到项目文件夹中。

After some research and experimentation, I have come up with the following .csproj file. 经过一些研究和实验,我得出了以下.csproj文件。

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeFrameworkVersion>2.1.4</RuntimeFrameworkVersion>
    <TieredCompilation>true</TieredCompilation>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

  <ItemGroup>
    <APIDefinition Include="D:\SomePlace\*.API.*.yaml" />
  </ItemGroup>

  <Target Name="CopyFiles" BeforeTargets="Compile;Build;Publish">
    <Copy SourceFiles="@(APIDefinition)" DestinationFolder="wwwroot" />
    <Copy SourceFiles="D:\SomePlaceElse\BaseAPISettings.json" DestinationFolder="$(MSBuildProjectDirectory)" />
  </Target>

  <ItemGroup>
    <Compile Remove="wwwroot\**\*;node_modules;bower_components" />
    <None Update="**.user;**.vspscc">
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.4" />
  </ItemGroup>

</Project>

Here I have defined CopyFiles target, which should be run before the targets I've placed there. 在这里,我定义了CopyFiles目标,该目标应该在放置目标之前运行。 This target uses Copy task to copy YAML format API definition files and base API settings to the project directory. 该目标使用复制任务将YAML格式的API定义文件和基本API设置复制到项目目录。

This works well during build, publish etc. Also, if I delete the local file in the IDE, it instantly recopies it from the source. 这在构建,发布等过程中效果很好。此外,如果我在IDE中删除本地文件,它会立即从源文件中将其重新复制。

Sometimes I make changes to these files between debugging sessions. 有时,我会在调试会话之间对这些文件进行更改。 Then, when I start debugging from Visual Studio, since the project files aren't changed, obviously the already built project is run. 然后,当我从Visual Studio开始调试时,由于项目文件没有更改,因此显然已运行的项目正在运行。

Since the project is not built, my copy tasks are not triggered, and I end up with stale files during debuging. 由于未构建项目,因此不会触发我的复制任务,并且最终在调试过程中出现陈旧文件。

Is there anything I can do to have my Copy tasks triggered, when I do "Start Debugging F5" in the IDE, regardless of the project build state ? 当我在IDE中执行“开始调试F5”时,无论项目构建状态如何,都可以做些什么来触发我的复制任务?

PS : I'm using Visual Studio 2017 15.8.5 and targeting .NET Core 2.1.4 runtime, if it makes any difference. PS:我正在使用Visual Studio 2017 15.8.5,并以.NET Core 2.1.4运行时为目标(如果有任何区别)。

To integrate fully into the up-to-date check of the project system inside Visual Studio, I susggest the following changes: 为了完全集成到Visual Studio中项目系统的最新检查中,我建议进行以下更改:

  1. Make the items' source and target paths known before 事先知道项目的源路径和目标路径
  2. Register them to the up-to-date check system. 将它们注册到最新的检查系统。 (Also needs a hack to make sure the project source code is recompiled so that the output will have a newer time stamp) (还需要黑客以确保重新编译项目源代码,以便输出具有新的时间戳记)
  3. Make the MSBuild target itself incremental. 使MSBuild目标本身递增。 This also helps for command-line builds when the files don't have to be copied. 当不必复制文件时,这也有助于命令行构建。

The complete changes look like this: 完整的更改如下所示:

<ItemGroup>
  <CustomCopyFile Include="..\TestFiles\*.API.*.yaml"
                  TargetPath="wwwroot\%(Filename)%(Extension)" />
  <CustomCopyFile Include="..\TestFiles\BaseAPISettings.json"
                  TargetPath="%(Filename)%(Extension)" />
  <UpToDateCheckInput Include="@(CustomCopyFile)" />
  <UpToDateCheckBuild Include="@(CustomCopyFile->'%(TargetPath)')"
                      Original="@(CustomCopyFile)" />
  <CustomAdditionalCompileInputs Include="@(CustomCopyFile->'%(TargetPath)')" />
</ItemGroup>

<Target Name="CopyFiles" 
        BeforeTargets="BeforeBuild;BeforePublish"
        Inputs="@(CustomCopyFile)" 
        Outputs="@(CustomCopyFile->'%(TargetPath)')">
  <Copy SourceFiles="@(CustomCopyFile)"
        DestinationFiles="@(CustomCopyFile->'%(TargetPath)')" />
</Target>

CustomCopyFile now collects all the source files and we put the expected destination file name into the TargetPath metadata. 现在, CustomCopyFile收集所有源文件,并将预期的目标文件名放入TargetPath元数据中。

UpToDateCheckInput items tell Visual Studio to rebuild the project if one of these items change. 如果其中一项更改, UpToDateCheckInput项告诉Visual Studio重建项目。

UpToDateCheckBuild items instruct Visual Studio to only check these items against special source items. UpToDateCheckBuild项目指示Visual Studio仅对照特殊源项目检查这些项目。 This is redundant for this example project but may be helpful if the target path wasn't inside the project directory but some intermediate output (obj..) folder and no re-evaluation would see these new files. 这对于此示例项目是多余的,但如果目标路径不在项目目录中,而是在某些中间输出(obj ..)文件夹中,并且没有重新评估将看到这些新文件,则可能会有所帮助。 It would also be helpful if the files were also modified as part of processing (eg replacing variables inside the files). 如果在处理过程中还修改了文件(例如替换文件中的变量),这也将很有帮助。

CustomAdditionalCompileInputs is a hack here since the items are copied to the project folder and are considered to be "inputs to the output" automatically.. So we force the project to recompile if our source files change. CustomAdditionalCompileInputs很容易受到攻击,因为这些项目已复制到项目文件夹,并被视为自动“输入到输出”。因此,如果源文件发生更改,我们将强制重新编译项目。 If we don't do so, it would never consider the project up-to-date after a change to the source yaml files since they would be newer than the compiled app.dll file. 如果我们不这样做,则在更改源yaml文件后,它将永远不会考虑该项目是最新的,因为它们比已编译的app.dll文件要新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加在Visual Studio 2017 RC中构建.NET Core项目后运行的msbuild任务 - Add a msbuild task that runs after building a .NET Core project in Visual Studio 2017 RC 在 Visual Studio 2017 中通过 postbuild 事件构建 .net 核心项目 - Build .net core project by postbuild event in visual studio 2017 如何让Visual Studio 2017在类库项目中构建TypeScript(新的.net核心样式.csproj) - How to get Visual Studio 2017 to compile TypeScript on build in class library project (new .net core style .csproj) 如何使用 BuildManager 在 Visual Studio 2017 (MsBuild 15) 上构建 .Net Core 项目或解决方案 - How to use BuildManager to build .Net Core project or solution on Visual Studio 2017 (MsBuild 15) 如何从Visual Studio 2017 RC中的.NET Core MSBuild项目创建NuGet包? - How do you create a NuGet package from a .NET Core MSBuild project in Visual Studio 2017 RC? Azure DevOps中的Visual Studio Build任务失败 - 我的.Net Framework引用的项目是.Net Core? - Visual Studio Build task fails in Azure DevOps - my .Net Framework referenced project is .Net Core? 如何在Visual Studio 2017中生成非.NET Core库NuGet程序包 - How to generate a non-.NET Core Library NuGet package within Visual Studio 2017 如何自动将Visual Studio 2017 Web项目发布到文件夹 - How to automate Visual Studio 2017 web project publish to folder 如何在Visual Studio中复制引用项目的输出 - How to copy output of referenced project in Visual Studio 符号文件未加载用于在 Visual Studio 2012 中调试自定义项目 - Symbol file not loading for debugging custom project in Visual Studio 2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM