简体   繁体   English

多级web.config转换

[英]Multiple level web.config transform

Is there any way to apply a web.config transform on more than one level? 有什么方法可以在多个级别上应用web.config转换吗? Eg: 例如:

web.config
 - web.release.config
   - web.prod1.config
   - web.prod2.config

When targeting prod1 , I would like to do a 3 way merge web.config < web.release.config < web.prod1.config . 当目标prod1 ,我愿做一个3路合并web.config < web.release.config < web.prod1.config Is this possible? 这可能吗?

There is a way to accomplish this. 有一种方法可以完成此任务。 As you don't specify too much, I'm not sure this will satisfy your requirements though. 由于您没有指定太多,因此我不确定这是否可以满足您的要求。 The following is how it could be accomplished from scratch, but you could just pull the bits that you need directly into the csproj you already have. 以下是如何从头开始实现的方法,但是您可以将所需的位直接拉到已经拥有的csproj中。

Create a .csproj file: 创建一个.csproj文件:

Transform.csproj

<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="Web.config" />
    <None Include="Web.Debug.config">
      <DependentUpon>Web.config</DependentUpon>
    </None>
    <None Include="Web.Prod.config">
      <DependentUpon>Web.config</DependentUpon>
    </None>
    <None Include="Web.Release.config">
      <DependentUpon>Web.config</DependentUpon>
    </None>
  </ItemGroup>
  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll"/>
  <Target Name="TransformRelease">
    <TransformXml Source="Web.config"
                  Transform="Web.Release.config"
                  Destination="Web.New.config"/>
  </Target>
  <Target Name="TransformProd">
    <TransformXml Source="Web.New.config"
                  Transform="Web.Prod.config"
                  Destination="Web.New.config"/>
  </Target>
</Project>

Then you can execute your two transforms through invoking an msbuild command from the command line. 然后,您可以通过从命令行调用msbuild命令来执行两个转换。 I used the following powershell commands. 我使用了以下powershell命令。

.\msbuild.exe "PATH_TO_YOUR_CSPROJ\Transform.csproj" /t:TransformRelease
.\msbuild.exe "PATH_TO_YOUR_CSPROJ\Transform.csproj" /t:TransformProd

This will transform your web.config using the transforms in the web.release.config and create a new file with the result of that transform web.new.config. 这将使用web.release.config中的转换来转换web.config并使用转换后的结果web.new.config创建一个新文件。 Then the second command will transform the web.new.config using the transforms in web.prod.config and update the web.new.config with that transformed value. 然后,第二个命令将使用web.prod.config中的转换对web.new.config进行转换,并使用该转换后的值更新web.new.config。

Web.config Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="web" value="web" />
    <add key="release" value="web" />
    <add key="prod" value="web" />
    <add key="release:prod" value="web" />
  </appSettings>
</configuration>

Web.Release.config Web.Release.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="release" value="release" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    <add key="release:prod" value="release" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

Web.Prod.config Web.Prod.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="prod" value="prod" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    <add key="release:prod" value="prod" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

Running the above commands produced Web.New.config 运行以上命令产生的Web.New.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="web" value="web" />
    <add key="release" value="release" />
    <add key="prod" value="prod" />
    <add key="release:prod" value="prod" />
  </appSettings>
</configuration>

UPDATE 更新

While the above works, I wouldn't want to use it in that manner. 尽管上述方法有效,但我不想以这种方式使用它。 After tinkering around with the .csproj a bit, I came up with this which will do the transformation for you in the BeforeBuild task. 稍微修改一下.csproj之后,我想到了它,它将在BeforeBuild任务中为您完成转换。

  <Target Name="TransformRelease">
    <TransformXml Source="Web.config" Transform="Web.Release.config" Destination="Web.New.config" />
  </Target>
  <Target Name="TransformProd" Condition="'$(Configuration)' == 'Release'">
    <TransformXml Source="Web.New.config" Transform="Web.Prod.config" Destination="Web.New.config" />
  </Target>
  <Target Name="BeforeBuild">
    <MSBuild Projects="WebApplication1.csproj" Targets="TransformRelease;TransformProd"/>
  </Target>

With these defined in your .csproj file, when you build the project as is, it will apply the Release transform alone. 在.csproj文件中定义了这些内容后,按原样构建项目时,它将单独应用Release转换。 When you build the project in the Release configuration, it will apply both the Release and Prod transformations. 当您在Release配置中构建项目时,它将同时应用Release和Prod转换。 Obviously you will need to tweak it for your needs given prod1, prod2, etc. 显然,根据prod1,prod2等的需要,您需要对其进行调整。

Is not possible out of the box with simple commands, but you can do custom transformation and string replacement using build tasks 使用简单的命令无法立即使用,但是您可以使用构建任务进行自定义转换和字符串替换

A while ago I asked a similar questions and I got a really nice answer using build tasks transformation. 不久前,我问了类似的问题,并且使用构建任务转换得到了一个非常好的答案。 Instead of copying it here, take a look in the solution and adapt to your needs.: 与其在此处复制,不如查看解决方案并适应您的需求。:

Service Fabric Default Publish Profile other than Local.xml Service Fabric默认发布配置文件(不是Local.xml)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM