如何让MSBuild任务使用外部xml参数文件中的参数?
示例:使用外部xml文件中的“MyConnectionStringParameter”作为我的MSBuild任务。
MSBuild文件:
<?xml version="1.0"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Migrate">
<UsingTask TaskName="FluentMigrator.MSBuild.Migrate"
AssemblyFile="../bin/FluentMigrator.MSBuild.dll"/>
<PropertyGroup>
<TargetPath>../bin/Target.dll</TargetPath>
</PropertyGroup>
<Target Name="Migrate" >
<Message Text="Starting FluentMigrator Migration"/>
<Migrate Database="sqlserver2008"
Connection="$(MyConnectionStringParameter)"
Target="$(TargetPath)"
Verbose="True"
Output="True"
OutputFilename="generated.sql">
</Migrate>
</Target>
</Project>
参数文件:
<?xml version="1.0" encoding="utf-8"?>
<parameters>
<setParameter name="MyConnectionStringParameter" value="ParameterValue" />
</parameters>
答案 0 :(得分:5)
如果您正在使用MSBuild 4.0或更高版本......请使用(内置)Xml Peek。
如果您使用的是pre 4.0,请使用Ms Build社区任务中的XmlRead。
https://github.com/loresoft/msbuildtasks
Peek示例:
<Target Name="ReadXmlPeekValue">
<!-- you do not need a namespace for this example, but I left it in for future reference -->
<XmlPeek Namespaces="<Namespace Prefix='peanutNamespace' Uri='http://schemas.microsoft.com/developer/msbuild/2003'/>"
XmlInputPath=".\Parameters.xml"
Query="/parameters/setParameter[@name='MyConnectionStringParameter']/@value">
<Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>
<Message Text="@(Peeked)"/>
<XmlPeek Namespaces="<Namespace Prefix='peanutNamespace' Uri='http://schemas.microsoft.com/developer/msbuild/2003'/>"
XmlInputPath=".\Parameters.xml"
Query="/parameters/setParameter[@name='MyConnectionStringParameter']/@value">
<Output TaskParameter="Result" PropertyName="PeekedSingle" />
</XmlPeek>
<Message Text="$(PeekedSingle)"/>
</Target>
基于这个简单的示例:((也显示XmlPoke))
http://yentran.org/blog/2012/05/11/modifying-config-file-in-an-msbuild-project/
使用“Peek”时,即使使用msbuild.exe(来自%WINDIR%\ Microsoft.NET \ Framework \ v4.0.30319),也必须指定 ToolsVersion
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >