How do I have an MSBuild task use a parameter from an external xml parameter file?

Example: Use the 'MyConnectionStringParameter' from an external xml file for my MSBuild task.

MSBuild File:

<?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>

Parameter File:

<?xml version="1.0" encoding="utf-8"?>
<parameters>
  <setParameter name="MyConnectionStringParameter" value="ParameterValue" />
</parameters>
有帮助吗?

解决方案

If you're using MSBuild 4.0 or above...... use (built in) Xml Peek.

How to use XmlPeek task?

If you're using pre 4.0, use XmlRead from Ms Build community task.

https://github.com/loresoft/msbuildtasks

Here is a ~Peek Example:

<Target Name="ReadXmlPeekValue">
    <!-- you do not need a namespace for this example, but I left it in for future reference -->
    <XmlPeek Namespaces="&lt;Namespace Prefix='peanutNamespace' Uri='http://schemas.microsoft.com/developer/msbuild/2003'/&gt;"
         XmlInputPath=".\Parameters.xml" 
         Query="/parameters/setParameter[@name='MyConnectionStringParameter']/@value">
        <Output TaskParameter="Result" ItemName="Peeked" />
    </XmlPeek>

    <Message Text="@(Peeked)"/>



    <XmlPeek Namespaces="&lt;Namespace Prefix='peanutNamespace' Uri='http://schemas.microsoft.com/developer/msbuild/2003'/&gt;"
         XmlInputPath=".\Parameters.xml" 
         Query="/parameters/setParameter[@name='MyConnectionStringParameter']/@value">
        <Output TaskParameter="Result" PropertyName="PeekedSingle" />
    </XmlPeek>

    <Message Text="$(PeekedSingle)"/>


</Target>

If you need a namespace, note the XML namespace must be HTML-encoded.

Example based on this simple one: ((also shows XmlPoke))

http://yentran.org/blog/2012/05/11/modifying-config-file-in-an-msbuild-project/

When using "Peek", even with msbuild.exe (from %WINDIR%\Microsoft.NET\Framework\v4.0.30319), you must specify the ToolsVersion.

<Project  ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top