简体   繁体   中英

Multiple level web.config transform

Is there any way to apply a web.config transform on more than one level? 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 . 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.

Create a .csproj file:

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. I used the following powershell commands.

.\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. 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.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

<?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

<?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

<?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.

  <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. When you build the project in the Release configuration, it will apply both the Release and Prod transformations. Obviously you will need to tweak it for your needs given prod1, prod2, etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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