将 MSBuild 与外部 xml 参数文件结合使用

2024-06-19

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

示例:将外部 xml 文件中的“MyConnectionStringParameter”用于我的 MSBuild 任务。

MS构建文件:

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

如何使用XmlPeek任务? https://stackoverflow.com/questions/2688239/how-to-use-xmlpeek-task

如果您使用的是 4.0 之前的版本,请使用 Msbuild 社区任务中的 XmlReader。

https://github.com/loresoft/msbuildtasks 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="&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 编码的。

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

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

使用“Peek”时,即使使用 msbuild.exe(来自 %WINDIR%\Microsoft.NET\Framework\v4.0.30319),您必须指定 the 工具版本.

<Project  ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 MSBuild 与外部 xml 参数文件结合使用 的相关文章

随机推荐