如何获得msbuild任务对文件集合进行配置转换? [英] How do I get an msbuild task to do config transforms on a collection of files?
问题描述
我正在尝试转换我拥有的项目中的所有web.config文件,这是我的树状结构:
I am trying to transform all of the web.config files in a project I have, here's a my tree structure:
- Transform.bat
- 转换
- ConfigTransform.proj
- Web.Transform.config
- Transform.bat
- Transforms
- ConfigTransform.proj
- Web.Transform.config
- web.config
- 观看次数
- web.config
- web.config
- Views
- web.config
还有更多的web.config文件,但想法是,这将找到所有文件并对其应用相同的配置转换.
There's more web.config files, but the idea is that this will find all of them and apply the same config transform on them.
我从发现的博客帖子中得到了一些提示,但我陷入了困境最后一步,实际的改造.此外,中间还有一个我不太喜欢的粗糙部分(我不太了解自己在做什么,显然我做错了).这是我到目前为止的位置:
I've taken a few hints from a blog post I found but I get stuck in the last step, the actual transformation. Also there's a bit of a rough part in the middle that I don't really like (I don't quite understand what I'm doing and I'm obviously doing it wrong). Here's where I am so far:
<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看起来像这样:
My Transform.bat looks like this:
%systemroot%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe %CD%\Transforms\ConfigTransform.proj
因此,当我调用批处理时,将创建适当的目录.但是,如您所见,我必须对OutDir有所创造,使其成为.. \ N \ N.由于某些原因,如果我不这样做,OutDir路径将与输入目录完全相同.因此,显然我需要在MakeDir中进行一些更改,但不确定如何.
So when I call the batch, the appropriate directories get created. However, as you can see I've had to be a little creative with the OutDir, making it ..\N\N. For some reason, if I don't do this the OutDir path will be exactly the same as the input directory. So I obviously need to change something in MakeDir but I'm not sure what.
真正的问题出在开始进行转换时.我试图保持TransformXml Source参数像这样或这样:
The real problem comes when it starts to do the transforms. I've tried to keep the TransformXml Source parameter like this or like so:
@(_FilesToTransformNotAppConfig->'%(FullPath)')
后者给我一个错误无法打开源文件:不支持给定路径的格式."前者给我这样的输出:
The latter gives me an error "Could not open Source file: The given path's format is not supported." and the former gives me this output:
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.
总结我的问题:
- 如何避免OutDir出现路径问题?我摆弄了多条路径,但我做错了.
- 如何使TransformXml任务在Source属性中接受多个文件?
推荐答案
我认为您已经很接近了.我在下面粘贴了一个示例,该示例显示了如何执行此操作.
I think you were pretty close. I have pasted a sample below which shows how to do this.
在我的示例中,我发现位于web.config文件本身旁边的转换.对于您的方案,您可以只使用指向特定文件的MSBuild属性.
In my sample I discover the transform sitting next to the web.config file itself. For your scenario you can just use an MSBuild property pointing to a specific file.
<?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 .
这篇关于如何获得msbuild任务对文件集合进行配置转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!