简体   繁体   English

MSBuild - 根据构建配置复制一个或另一个文件

[英]MSBuild - Copy one file or another depending on the Build Configuration

I have a file inside my solution that is currently copied to the build output on this path: Core\Bootstrap.txt我的解决方案中有一个文件,当前复制到此路径上的构建 output: Core\Bootstrap.txt

(My application uses it when it runs, but I would like it to have different contents depending on the Build Configuration) (我的应用程序在运行时使用它,但我希望它根据构建配置具有不同的内容)

I suspect this is done modifying the csproj and playing with MSBuild, but I don't have any clue.我怀疑这是修改 csproj 并使用 MSBuild 完成的,但我没有任何线索。

The only function is that you should create wo other txt files called Bootstrap.Debug.txt and Bootstrap.Release.txt .唯一的 function 是您应该创建两个名为Bootstrap.Debug.txtBootstrap.Release.txt的其他 txt 文件。

First , set Copy to Output Directory of these two other files to No not copy .首先,将Copy to Output这两个文件的目录设置为No not copy

And remember to clear Bootstrap.txt and then make your all content into Bootstrap.Debug.txt or Bootstrap.Release.txt .记得清除Bootstrap.txt ,然后将所有内容放入Bootstrap.Debug.txtBootstrap.Release.txt If you want to use Debug mode, make changes on the Bootstrap.Debug.txt while make changes on the Bootstrap.Release.txt under Release mode.如果要使用Debug模式,请在Bootstrap.Debug.txt上进行更改,同时在Release模式下在Bootstrap.Release.txt上进行更改。

Second , unload your project, add this in the csproj file:其次,卸载您的项目,将其添加到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>

在此处输入图像描述

Then , change your build configuration, then build your project.然后,更改您的构建配置,然后构建您的项目。 After that, you can check the Bootstrap.txt under the output folder, and it already contains the related content based on your configuration.之后,您可以查看output文件夹下的Bootstrap.txt ,根据您的配置已经包含相关内容。

Update更新

If you did not want to use copy task, then you can directly copy the related Bootstrap.Debug or Release.txt into the output folder.如果不想使用复制任务,可以直接将相关的 Bootstrap.Debug 或 Release.txt 复制到 output 文件夹中。 And then you can directly use Bootstrap.Debug.txt or Bootstrap.Release.txt based on the configuration.然后就可以根据配置直接使用Bootstrap.Debug.txt或者Bootstrap.Release.txt了。 That is an easier way.这是一种更简单的方法。

Not sure whether you need the fixed name file Bootstrap.txt , if you do not need that, you can directly use Bootstrap.Debug.txt or Bootstrap.Release.txt .不确定是否需要固定名称文件Bootstrap.txt ,如果不需要,可以直接使用Bootstrap.Debug.txtBootstrap.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>

If you still want the fixed name file Bootstrap.txt , you have to use Copy task to make the related txt file overwrite the main Bootstrap.txt file.如果您仍然想要固定名称文件Bootstrap.txt ,则必须使用 Copy 任务使相关 txt 文件覆盖主Bootstrap.txt文件。 And MSBuild is not flexible enough to write some info based on the并且 MSBuild 不够灵活,无法根据
configuration condition on the txt file and then it will add corresponding information to the corresponding build file according to the build configuration. txt文件上的配置条件,然后它会根据构建配置将相应的信息添加到相应的构建文件中。

MSBuild does not have such a function, so you can only write logic manually, and overwrite the main file according to different configuration-based files. MSBuild没有这样的function,所以只能手动写逻辑,根据不同的配置文件覆盖主文件。

If you do not want xcopy command, you can use the msbuild copy task , and it belongs to MSBuild.如果不需要xcopy命令,可以使用msbuild 复制任务,它属于 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>

Otherwise, there is no other way.否则,没有其他办法。

A ton of the time I see people do this I see them have a separate file named Bootstrap.Development.txt and when you load the file you can check the Environment type in dotnet and load $"Bootstrap.{envName}.json" and just always load Bootstrap.json but don't use it if your envName is 'Development'很多时候我看到人们这样做,我看到他们有一个名为 Bootstrap.Development.txt 的单独文件,当您加载文件时,您可以检查 dotnet 中的环境类型并加载 $"Bootstrap.{envName}.json" 和总是加载 Bootstrap.json 但如果您的 envName 是“开发”,请不要使用它

edit: just remember in the properties for both files to always copy to output dir if newer编辑:只要记住两个文件的属性,如果更新,总是复制到 output 目录

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

相关问题 如何从要构建的多个文件中选择一个.resx文件(取决于解决方案配置)? - How to choose one .resx file out of many to build(depending on solution configuration)? TFS Build 2015 - 如何在构建过程中将文件的内容复制到另一个文件? - TFS Build 2015 - How to copy a file's content to another one during the build? 在MSBuild配置文件中指定记录器 - Specify a logger in MSBuild configuration file MSBuild自定义任务取决于另一个项目 - MSBuild custom task depending on another project msbuild不会在发布配置中进行构建 - msbuild won't build in release configuration 将构建副本配置文件发布到另一个项目 - Post build copy config file to another project 使用post build event将文件从一个项目复制到另一个项目... VS2010 - Copy file(s) from one project to another using post build event…VS2010 如何使用 MSBuild 构建文件 - How to build the file using MSBuild MSBuild任务构建加载我的程序集并构建一个序列化的NHibernate配置 - MSBuild task to build load my assembly and build a serialised NHibernate Configuration 配置MSBuild以构建解决方案,并为每个项目将构建的dll放入一个目录,将外部库放入另一个目录 - Configure MSBuild to build a solution and for each project put the built dlls into one directory and external libraries into another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM