如何让msbuild任务对一组文件进行配置转换?

时间:2012-04-30 12:08:36

标签: msbuild msbuild-task web-config-transform slowcheetah

我正在尝试转换我所拥有的项目中的所有web.config文件,这是我的树结构:

  • Transform.bat
  • 变换
    • ConfigTransform.proj
    • Web.Transform.config
  • 网站
    • 的web.config
      • 的web.config

还有更多web.config文件,但我们的想法是,它会找到所有这些文件并对它们应用相同的配置转换。

我从a blog post I found那里得到了一些提示,但我在最后一步,实际的转变中陷入困境。中间还有一些我不喜欢的粗略部分(我不太明白我在做什么,我显然做错了)。这是我到目前为止的地方:

<Project ToolsVersion="4.0" DefaultTargets="Transform" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="TransformXml" AssemblyFile="Tools\Microsoft.Web.Publishing.Tasks.dll"/>

    <PropertyGroup>
        <SitePath>..\..\Website</SitePath>
        <WebConfigTransformInputFile>$(SitePath)\Web.config</WebConfigTransformInputFile>
        <WebConfigTransformFile>Web.Transform.config</WebConfigTransformFile>
        <OutDir>..\N\N\</OutDir>

    </PropertyGroup>

    <ItemGroup>
        <_FilesToTransform Include="$(SitePath)\**\web.config"/>
    </ItemGroup>

    <Target Name="Transform">

    <MakeDir Directories="@(_FilesToTransform->'$(OutDir)%(RelativeDir)')" />

    <TransformXml Source="@(_FilesToTransform->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')"
                  Transform="$(WebConfigTransformFile)"
                  Destination="@(_FilesToTransform->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')" />
    </Target>
</Project>

我的Transform.bat看起来像这样:

%systemroot%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe %CD%\Transforms\ConfigTransform.proj

因此,当我调用批处理时,会创建相应的目录。然而,正如你所看到的,我必须对OutDir有点创意,使它成为.. \ N \ N。出于某种原因,如果我不这样做,OutDir路径将与输入目录完全相同。所以我显然需要在MakeDir中更改一些内容,但我不确定是什么。

真正的问题出现在它开始进行变换时。我试图像这样保留TransformXml Source参数,或者像这样:

@(_FilesToTransformNotAppConfig->'%(FullPath)')

后者给我一个错误“无法打开源文件:不支持给定路径的格式。”而前者给了我这个输出:

Build started 30-4-2012 14:02:48.
Project "D:\Dev\transform\DoTransforms\Transforms\ConfigTransform.proj" on node 1 (default targets).
Transform:
  Creating directory "..\N\N\..\..\Website\Views\".
  Transforming Source File: ..\N\N\..\..\Website\Views\Web.config;..\N\N\..\..\Website\Web.config
D:\Dev\transform\DoTransforms\Transforms\ConfigTransform.proj(32,2): error : Could not open Source file: Could not find a part of the path 'D:\Dev\transform\DoTransforms\Website\Views\Web.config;\Website\Web.config'.
  Transformation failed
Done Building Project "D:\Dev\transform\DoTransforms\Transforms\ConfigTransform.proj" (default targets) -- FAILED.

Build FAILED.

总结我的问题:

  1. 如何避免OutDir的路径问题?我已经摆弄了多条路径,但我无法做到。
  2. 如何让TransformXml任务接受Source属性中的多个文件?

1 个答案:

答案 0 :(得分:10)

我觉得你很亲密。我在下面粘贴了一个示例,说明了如何执行此操作。

在我的示例中,我发现了位于web.config文件本身旁边的转换。对于您的场景,您可以使用指向特定文件的MSBuild属性。

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="TransformAll" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)'=='' ">Release</Configuration>
    <OutputFolder Condition=" '$(OutputFolder)'=='' ">C:\temp\transformed-files\</OutputFolder>
  </PropertyGroup>

  <!--
  This target shows how to transform web.config with a specific transform file associated to that specific web.config file.
  -->
  <Target Name="TransformAll">

    <!-- discover the files to transform -->
    <ItemGroup>
      <FilesToTransofm Include="$(MSBuildProjectDirectory)\**\web.config"/>
    </ItemGroup>

    <!-- Ensure all target directories exist -->
    <MakeDir Directories="@(FilesToTransofm->'$(OutputFolder)%(RecursiveDir)')"/>

    <!-- TransformXml only supports single values for source/transform/destination so use %(FilesToTransofm.Identity)
         to sned only 1 value to it -->
    <TransformXml Source="%(FilesToTransofm.Identity)"
                  Transform="@(FilesToTransofm->'%(RecursiveDir)web.$(Configuration).config')"
                  Destination="@(FilesToTransofm->'$(OutputFolder)%(RecursiveDir)web.config')" />    
  </Target>

</Project>

仅供参考,您可以在https://github.com/sayedihashimi/sayed-samples/tree/master/TransformMultipleWebConfigs下载完整的样本。