[英].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中项目系统的最新检查中,我建议进行以下更改:
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.