如何将文件复制并重命名为输出文件夹作为构建的一部分 [英] How to copy and rename file to output folder as part of build
问题描述
我认为这更多是与msbuild有关的问题. 有一个.net核心应用程序,我需要有条件地发布文件,并根据Visual Studio 2019中选择的生成配置,在发布到目标之前应重命名该文件.
I believe this is more of an msbuild-related question. Have a .net core app and I need to conditionally publish a file and based on the build config selected in Visual Studio 2019, the file should be renamed before publishing to the target.
因此,我正在考虑修改csproj文件(除了msbuild文件本身之外,什么都没有) 我在复制任务上看不到条件选项 https://docs.microsoft.com/zh-cn/visualstudio/msbuild/copy-task?view = vs-2019
So Im looking at modifying the csproj file (which is nothing but an msbuild file itself) I dont see a condition option on the copy task https://docs.microsoft.com/en-us/visualstudio/msbuild/copy-task?view=vs-2019
我追求的目标是,如果我有3个不同的文件
tester-notes.dev.json
tester-notes.debug.json
tester-notes.prod.json
The goal Im after, is if I have 3 different files
tester-notes.dev.json
tester-notes.debug.json
tester-notes.prod.json
如果选择prod作为构建配置,我希望发布的文件为tester-notes.prod.json,但重命名为tester-notes.json
If prod is selected as a build config, I want the file published to be tester-notes.prod.json, but renamed to tester-notes.json
推荐答案
假设开发时在解决方案资源管理器中有三个文件(生成操作=无):
Assuming you have three files(build action = None) in Solution Explorer when developing:
如果您使用的是FileSystem
发布模式,则可以使用与此脚本类似的方法来重命名并复制到发布文件夹:
You can use something similar to this script to rename and copy to publish folder if you're using FileSystem
publish mode:
<ItemGroup Condition="$(Configuration)=='Dev'">
<FileToRename Include="$(ProjectDir)\tester-notes.dev.json" />
</ItemGroup>
<ItemGroup Condition="$(Configuration)=='Debug'">
<FileToRename Include="$(ProjectDir)\tester-notes.debug.json" />
</ItemGroup>
<ItemGroup Condition="$(Configuration)=='Prof'">
<FileToRename Include="$(ProjectDir)\tester-notes.prof.json" />
</ItemGroup>
<Target Name="DoSthAfterPublish1" AfterTargets="Publish" Condition="$(Configuration)=='Dev'">
<Copy SourceFiles="@(FileToRename)" DestinationFiles="@(FileToRename->Replace('.dev.json','.json'))"/>
<Move SourceFiles="$(ProjectDir)\tester-notes.json" DestinationFolder="$(PublishDir)" OverwriteReadOnlyFiles="true"/>
</Target>
<Target Name="DoSthAfterPublish2" AfterTargets="Publish" Condition="$(Configuration)=='Debug'">
<Copy SourceFiles="@(FileToRename)" DestinationFiles="@(FileToRename->Replace('.debug.json','.json'))"/>
<Move SourceFiles="$(ProjectDir)\tester-notes.json" DestinationFolder="$(PublishDir)" OverwriteReadOnlyFiles="true"/>
</Target>
<Target Name="DoSthAfterPublish3" AfterTargets="Publish" Condition="$(Configuration)=='Prof'">
<Copy SourceFiles="@(FileToRename)" DestinationFiles="@(FileToRename->Replace('.prof.json','.json'))"/>
<Move SourceFiles="$(ProjectDir)\tester-notes.json" DestinationFolder="$(PublishDir)" OverwriteReadOnlyFiles="true"/>
</Target>
如果将tester-notes.debug.json
重置为tester-notes.Debug.json
,则可以使用DestinationFiles="@(FileToRename->Replace('.$(Configuration).json','.json'))"
将三个目标组合为一个.希望它能有所帮助:)
And if you can reset tester-notes.debug.json
to tester-notes.Debug.json
,, then we may combine the three targets into one by using DestinationFiles="@(FileToRename->Replace('.$(Configuration).json','.json'))"
. Hope it makes some help :)
此外:
根据Intellisense,我们可以找到Copy task
支持Condition
:
According to the Intellisense we can find the Copy task
supports Condition
:
这篇关于如何将文件复制并重命名为输出文件夹作为构建的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!