如何使用MSBuild的"afterbuild"任务来编辑.config文件? [英] How can I use MSBuild 'afterbuild' tasks to edit a .config file?
问题描述
我在目标项目中有一个.config,我需要通过MSBuild任务以编程方式向其中添加一行.
I have a .config in a target project and I need to add a line to it programmatically via an MSBuild task.
像这样的Pseduo操作:
Pseduo operations like:
- 找到目标.config文件
- 确定新节点的属性值(例如,"package"节点的"id"和"version")
- 在正确的父节点中插入新节点
- 保存更改
位于$ TargetProjectDir \ Config \ packages.config的.config文件:
The .config file at $TargetProjectDir\Config\packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ABC" version="1.1.0.4" />
<package id="XYZ" version="2.0.0.0" />
</packages>
之后需要像这样:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ABC" version="1.1.0.4" />
<package id="XYZ" version="2.0.0.0" />
<package id="CarDataWidget" version="3.0.0.0" />
</packages>
到目前为止,我已经考虑过使用内联任务","EXEC"任务和"XmlPoke"任务,但尚未设法使它们中的任何一个起作用.
So far i've considered using 'inline tasks', the 'EXEC' task and 'XmlPoke' task but haven't managed to get any of them working.
这是我尝试使用XmlPoke和XmlPeek的方法:
Here is my attempt with XmlPoke and XmlPeek:
我以下面的文章作为启发,介绍了如何将节点添加到packages.config文件: http://weblogs .asp.net/bsimser/appending-nodes-in-xml-files-with-xmlpeek-and-xmlpoke-using-nant
I used the following article as an inspiration on how to add nodes to the packages.config file: http://weblogs.asp.net/bsimser/appending-nodes-in-xml-files-with-xmlpeek-and-xmlpoke-using-nant
<Target Name="AfterBuild" DependsOnTargets="AddPackage">
</Target>
<Target Name="AddPackage">
<!-- Load existing nodes into a Property -->
<XmlPeek XmlInputPath="config/packages.config" Query="/packages/package" >
<Output TaskParameter="Result" PropertyName="Peeked" />
</XmlPeek>
<Message Text="From Peek: $(Peeked)"></Message>
<!-- Load new node into Property -->
<PropertyGroup>
<WidgetName>CarDataWidget</WidgetName>
<WidgetVersion>2.0.0.0</WidgetVersion>
<NewNode><package id="$(WidgetName)" version="$(WidgetVersion)" /></NewNode>
<!-- Concatenate existing and new node into a Property -->
<ConcatenatedNodes>$(Peeked)$(NewNode)</ConcatenatedNodes>
</PropertyGroup>
<Message Text="New pacakges: $(ConcatenatedNodes)"></Message>
<!-- Replace existing nodes with concatenated nodes -->
<XmlPoke Value="$(ConcatenatedNodes)" XmlInputPath="config/packages.config" Query="/packages">
</XmlPoke>
</Target>
上述构建的输出为:
1>AddPackage:
1> From Peek: <package id="ABC" version="1.1.0.4" />;<package id="XYZ" version="2.0.0.0" />
1> New pacakges: <package id="ABC" version="1.1.0.4" />;<package id="XYZ" version="2.0.0.0" /><package id="CarDataWidget" version="2.0.0.0" />
1> C:\_dev\CarDataWidget.csproj(184,14):
error MSB4094: "<package id="ABC" version="1.1.0.4" />;<package id="XYZ" version="2.0.0.0" /><package id="CarDataWidget" version="2.0.0.0" />"
is an invalid value for the "Value" parameter of the "XmlPoke" task.
Multiple items cannot be passed into a parameter of type "Microsoft.Build.Framework.ITaskItem".
1>
1>Build FAILED.
问题: 如何将其添加到具有现有包节点的.config文件中??
THE QUESTION: How can get it to add to a .config file with existing package nodes???
推荐答案
我遇到了同样的问题.我在此处找到了解决方案.
I had the same problem. I found the solution here.
问题在于XmlPoke认为分号是一个值分隔符.
The problem is than XmlPoke considers semicolon as a value separator.
应替换为:
<NewNode><package id="$(WidgetName)" version="$(WidgetVersion)" /></NewNode>
使用:
<NewNode><%3Bpackage id=%3B"%3B$(WidgetName)"%3B version=%3"%3$(WidgetVersion)"%3 />%3</NewNode>
必须用安全性%3B
这篇关于如何使用MSBuild的"afterbuild"任务来编辑.config文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!