将MSBuild与外部xml参数文件一起使用 [英] Using MSBuild with external xml parameter file

查看:207
本文介绍了将MSBuild与外部xml参数文件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何让MSBuild任务使用外部xml参数文件中的参数?

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

示例:将外部xml文件中的"MyConnectionStringParameter"用于我的MSBuild任务.

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

MSBuild文件:

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>

参数文件:

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

推荐答案

如果您使用的是MSBuild 4.0或更高版本,请使用Xml Peek(内置).

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

如何使用XmlPeek任务?

如果您使用的是4.0之前的版本,请使用Ms Build社区任务中的XmlRead.

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

https://github.com/loresoft/msbuildtasks

这是一个〜Peek示例:

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>

如果需要命名空间,请注意XML命名空间必须经过HTML编码.

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

基于此简单示例的示例:((还显示XmlPoke))

Example based on this simple one: ((also shows 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

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

这篇关于将MSBuild与外部xml参数文件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆