Using MSBuild with external xml parameter file

牧云@^-^@ 提交于 2019-12-23 03:21:34

问题


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>

回答1:


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


来源:https://stackoverflow.com/questions/18874195/using-msbuild-with-external-xml-parameter-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!