Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

msbuild transform one config few times

I have next config in my Web.config file

<Target Name="UpdateWebConfigForProjectsBeforeRun">
    <ItemGroup>
      <FilesToTransofm Include="ProjectsDeployBin\Web.*.$(Configuration).config"/>      
    </ItemGroup>    
    <Message Text="Transform file: %(FilesToTransofm.Identity)" />
    <TransformXml Source="web.config"
                  Transform="%(FilesToTransofm.Identity)"
                  Destination="web.config" />
  </Target>

What i am trying to do its get all configs from ProjectsDeployBin directory and apply each file to main web.config.

After first transformation main web.config locked by msbuild.

So how can i fix this issue? Is there any other way to transform my web.config by collection of files? Thanks.

like image 384
Sanja Melnichuk Avatar asked Oct 18 '12 10:10

Sanja Melnichuk


1 Answers

As you've noticed, the TransformXml task shipped with Visual Studio 2010 has a bug that leaves the source file locked.

To work around that, you can make a temporary copy of your source file before each transformation. Since you'll then be executing multiple tasks for each transform file (copy and transform), you'll need to switch to Target Batching instead of Task Batching.

Example:

<ItemGroup>
  <FilesToTransofm Include="ProjectsDeployBin\Web.*.$(Configuration).config"/>      
</ItemGroup>

<Target Name="UpdateWebConfigForProjectsBeforeRun"
        Inputs="@(FilesToTransofm)"
        Outputs="%(Identity).AlwaysRun">
  <Message Text="Transform file: %(FilesToTransofm.Identity)" />
  <Copy SourceFiles="web.config"
        DestinationFiles="web.pre-%(FilesToTransofm.Filename).temp.config" />
  <TransformXml Source="web.pre-%(FilesToTransofm.Filename).temp.config"
                Transform="%(FilesToTransofm.Identity)"
                Destination="web.config" />
</Target>

From a quick test, it looks like this bug is fixed in Visual Studio 2012, but I'm not able to find a reference / source that documents that, and the original Connect bug isn't viewable anymore.

like image 148
Bilal Avatar answered Oct 30 '22 03:10

Bilal