1

我想使用 MSBuild 中的 XmlMassUpdate 任务来更新所有 app.configs 中的 appSettings。我遇到的问题是某些 app.configs 没有 appSettings 元素,我无法让 XmlMassUpdate 跳过这些元素。

这是我到目前为止所得到的

<ProjectExtensions>
  <appSettings xmlns:xmu="urn:msbuildcommunitytasks-xmlmassupdate">
    <add xmu:key="key" key="SettingName" value="newSetting" xmu:action="update" />
  </appSettings>
</ProjectExtensions>
<Target Name="Change">
   <ItemGroup>
     <AppConfigFiles Include="$(SourceRoot)\**\App.config" />
   </ItemGroup>
   <XmlMassUpdate 
      ContentFile="$(SourceRoot)\%(AppConfigFiles.RecursiveDir)App.config"
      ContentRoot="/configuration/appSettings"
      NamespaceDefinitions="msb=http://schemas.microsoft.com/developer/msbuild/2003"
      SubstitutionsFile="$(MSBuildProjectDirectory)\SetConfig.proj"
      SubstitutionsRoot="/msb:Project/msb:ProjectExtensions/msb:appSettings" />
</Target>

它在其中一个 App.configs 上失败,说 Unable to locate '/configuration/appSettings'

4

1 回答 1

1

您需要分两步执行此操作:

  • 查询 app.config 以检查标签是否/configuration/appSettings存在
  • 仅在带有标记的文件上调用 XmlMassUpdate

<ProjectExtensions>
  <appSettings xmlns:xmu="urn:msbuildcommunitytasks-xmlmassupdate">
    <add xmu:key="key" key="SettingName" value="newSetting" xmu:action="update" />
  </appSettings>
</ProjectExtensions>

<Target Name="UpdateIfNecessary">
  <!-- Check if appSettings exists-->
  <XmlQuery XmlFileName="$(AppConfigFile)"
           XPath = "/configuration/appSettings">
   <Output TaskParameter="Values" ItemName="appSettings" />
  </XmlQuery>

  <!-- Replace if appSettings exists -->
  <XmlMassUpdate Condition="%(appSettings._innerXml) != ''"
    ContentFile="$(AppConfigFile)"
    ContentRoot="/configuration/appSettings"
    NamespaceDefinitions="msb=http://schemas.microsoft.com/developer/msbuild/2003"
    SubstitutionsFile="$(MSBuildProjectDirectory)\SetConfig.proj"
    SubstitutionsRoot="/msb:Project/msb:ProjectExtensions/msb:appSettings" />
</Target>


<Target Name="MassUpdate">
  <ItemGroup>
    <AppConfigFiles Include="$(SourceRoot)\**\App.config" />
  </ItemGroup>

  <!-- Execute UpdateIfNecessary for each app.config file -->
  <MSBuild Projects="$(MSBuildProjectFile)" 
           Targets="UpdateIfNecessary"
           Properties="AppConfigFile=%(AppConfigFiles.Fullpath)"/> 
</Target>
于 2011-08-31T11:59:34.110 回答