MSBuild - 根据构建配置复制一个或另一个文件
MSBuild - Copy one file or another depending on the Build Configuration
我的解决方案中有一个文件,当前已复制到此路径上的构建输出:Core\Bootstrap.txt
(我的应用程序在运行时使用它,但我希望它根据构建配置具有不同的内容)
我怀疑这是通过修改 csproj 和使用 MSBuild 完成的,但我没有任何线索。
很多时候我看到人们这样做我看到他们有一个名为 Bootstrap.Development.txt 的单独文件,当您加载该文件时,您可以检查 dotnet 中的环境类型并加载 $"Bootstrap.{envName}.json" 并且总是加载 Bootstrap.json 但如果你的 envName 是 'Development'
不要使用它
编辑:
只要记住在两个文件的属性中总是复制到输出目录如果更新
唯一的功能是您应该创建另外两个名为 Bootstrap.Debug.txt
和 Bootstrap.Release.txt
.
的 txt 文件
首先,将这两个文件的Copy to Output Directory设置为No not copy
.
和记得清除Bootstrap.txt
然后把你所有的内容变成Bootstrap.Debug.txt
或Bootstrap.Release.txt
。如果要使用Debug模式,在Bootstrap.Debug.txt
上修改,在Release模式下修改Bootstrap.Release.txt
.
其次,卸载你的项目,在csproj
文件中添加:
<ItemGroup>
<Content Include="Core\Bootstrap.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Core\Bootstrap.Debug.txt">
<DependentUpon>Bootstrap.txt</DependentUpon>
</Content>
<Content Include="Core\Bootstrap.Release.txt">
<DependentUpon>Bootstrap.txt</DependentUpon>
</Content>
</ItemGroup>
<Target Name="CopyFiles" BeforeTargets="PrepareForBuild">
<Exec Command="xcopy /y $(ProjectDir)Core\Bootstrap.$(Configuration).txt $(ProjectDir)Core\Bootstrap.txt" />
</Target>
然后,更改构建配置,然后构建项目。之后你可以查看output文件夹下的Bootstrap.txt
,根据你的配置已经包含了相关内容
更新
如果您不想使用复制任务,那么您可以直接将相关的Bootstrap.Debug或Release.txt复制到输出文件夹中。然后你可以根据配置直接使用Bootstrap.Debug.txt
或Bootstrap.Release.txt
。这是一个更简单的方法。
不确定是否需要固定名称文件Bootstrap.txt
,如果不需要,可以直接使用Bootstrap.Debug.txt
或Bootstrap.Release.txt
。
<ItemGroup>
<Content Include="Core\Bootstrap.txt">
</Content>
<Content Include="Core\Bootstrap.Debug.txt" Condition="'$(Configuration)'=='Debug'">
<DependentUpon>Bootstrap.txt</DependentUpon>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Core\Bootstrap.Release.txt" Condition="'$(Configuration)'=='Release'">
<DependentUpon>Bootstrap.txt</DependentUpon>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
如果你还想要固定名称的文件Bootstrap.txt
,你必须使用复制任务让相关的txt文件覆盖主文件Bootstrap.txt
.而且 MSBuild 不够灵活,无法根据
编写一些信息
txt文件上的配置条件,然后会根据构建配置在相应的构建文件中添加相应的信息。
MSBuild没有这个功能,只能手动写逻辑,根据不同的配置文件覆盖主文件。
如果你不想xcopy
命令,你可以使用msbuild copy task,它属于MSBuild。
<ItemGroup>
<Content Include="Core\Bootstrap.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Core\Bootstrap.Debug.txt" >
<DependentUpon>Bootstrap.txt</DependentUpon>
</Content>
<Content Include="Core\Bootstrap.Release.txt">
<DependentUpon>Bootstrap.txt</DependentUpon>
</Content>
</ItemGroup>
<Target Name="CopyFiles" BeforeTargets="PrepareForBuild">
<Copy SourceFiles="$(ProjectDir)Core\Bootstrap.$(Configuration).txt" DestinationFiles="$(ProjectDir)Core\Bootstrap.txt"></Copy>
</Target>
不然也没有别的办法了
我的解决方案中有一个文件,当前已复制到此路径上的构建输出:Core\Bootstrap.txt
(我的应用程序在运行时使用它,但我希望它根据构建配置具有不同的内容)
我怀疑这是通过修改 csproj 和使用 MSBuild 完成的,但我没有任何线索。
很多时候我看到人们这样做我看到他们有一个名为 Bootstrap.Development.txt 的单独文件,当您加载该文件时,您可以检查 dotnet 中的环境类型并加载 $"Bootstrap.{envName}.json" 并且总是加载 Bootstrap.json 但如果你的 envName 是 'Development'
不要使用它编辑: 只要记住在两个文件的属性中总是复制到输出目录如果更新
唯一的功能是您应该创建另外两个名为 Bootstrap.Debug.txt
和 Bootstrap.Release.txt
.
首先,将这两个文件的Copy to Output Directory设置为No not copy
.
和记得清除Bootstrap.txt
然后把你所有的内容变成Bootstrap.Debug.txt
或Bootstrap.Release.txt
。如果要使用Debug模式,在Bootstrap.Debug.txt
上修改,在Release模式下修改Bootstrap.Release.txt
.
其次,卸载你的项目,在csproj
文件中添加:
<ItemGroup>
<Content Include="Core\Bootstrap.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Core\Bootstrap.Debug.txt">
<DependentUpon>Bootstrap.txt</DependentUpon>
</Content>
<Content Include="Core\Bootstrap.Release.txt">
<DependentUpon>Bootstrap.txt</DependentUpon>
</Content>
</ItemGroup>
<Target Name="CopyFiles" BeforeTargets="PrepareForBuild">
<Exec Command="xcopy /y $(ProjectDir)Core\Bootstrap.$(Configuration).txt $(ProjectDir)Core\Bootstrap.txt" />
</Target>
然后,更改构建配置,然后构建项目。之后你可以查看output文件夹下的Bootstrap.txt
,根据你的配置已经包含了相关内容
更新
如果您不想使用复制任务,那么您可以直接将相关的Bootstrap.Debug或Release.txt复制到输出文件夹中。然后你可以根据配置直接使用Bootstrap.Debug.txt
或Bootstrap.Release.txt
。这是一个更简单的方法。
不确定是否需要固定名称文件Bootstrap.txt
,如果不需要,可以直接使用Bootstrap.Debug.txt
或Bootstrap.Release.txt
。
<ItemGroup>
<Content Include="Core\Bootstrap.txt">
</Content>
<Content Include="Core\Bootstrap.Debug.txt" Condition="'$(Configuration)'=='Debug'">
<DependentUpon>Bootstrap.txt</DependentUpon>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Core\Bootstrap.Release.txt" Condition="'$(Configuration)'=='Release'">
<DependentUpon>Bootstrap.txt</DependentUpon>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
如果你还想要固定名称的文件Bootstrap.txt
,你必须使用复制任务让相关的txt文件覆盖主文件Bootstrap.txt
.而且 MSBuild 不够灵活,无法根据
编写一些信息
txt文件上的配置条件,然后会根据构建配置在相应的构建文件中添加相应的信息。
MSBuild没有这个功能,只能手动写逻辑,根据不同的配置文件覆盖主文件。
如果你不想xcopy
命令,你可以使用msbuild copy task,它属于MSBuild。
<ItemGroup>
<Content Include="Core\Bootstrap.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Core\Bootstrap.Debug.txt" >
<DependentUpon>Bootstrap.txt</DependentUpon>
</Content>
<Content Include="Core\Bootstrap.Release.txt">
<DependentUpon>Bootstrap.txt</DependentUpon>
</Content>
</ItemGroup>
<Target Name="CopyFiles" BeforeTargets="PrepareForBuild">
<Copy SourceFiles="$(ProjectDir)Core\Bootstrap.$(Configuration).txt" DestinationFiles="$(ProjectDir)Core\Bootstrap.txt"></Copy>
</Target>
不然也没有别的办法了