Transforming multiple config files for multiple projects via MsBuildProj file

寵の児 提交于 2019-12-04 17:54:34

Found the solution:

Create one target for creating the item group:

  <Target Name="ListWebConfigs" AfterTargets="BuildSolution">

    <ItemGroup>
      <ConfigFiles Include="$(PublishFolder)\**\Web.config;$(PublishFolder)\**\NLog.config"
                   Exclude="$(PublishFolder)\**\Packages.config;$(PublishFolder)\**\*.*.config;$(PublishFolder)\**\bin\*.config" />
    </ItemGroup>

    <Message Text="$(PublishFolder)" />
    <Message Text="@(ConfigFiles)" />

  </Target>

Then another for the actual transforms:

  <!-- \x to prevent MSBuild from skipping "because all output files are up-to-date" -->
  <Target Name="TransformWebConfig" AfterTargets="ListWebConfigs" Inputs="@(ConfigFiles)" Outputs="%(ConfigFiles.Identity)\x">

    <PropertyGroup>
      <ConfigFile>%(ConfigFiles.Identity)</ConfigFile>
      <BackupFile>$([System.IO.Path]::ChangeExtension($(ConfigFile),&quot;.Source.config&quot;))</BackupFile>
      <TransformFilePrefix>$([System.String]::Concat($(TransformConfiguration), &quot;.config&quot;))</TransformFilePrefix>
      <TransformFile>$([System.IO.Path]::ChangeExtension($(ConfigFile), $(TransformFilePrefix)))</TransformFile>
    </PropertyGroup>

    <Message Text="$(PublishFolder)" />
    <Message Text="@(ConfigFiles)" />

    <Message Text="$(ConfigFile)" />
    <Message Text="$(BackupFile)" />
    <Message Text="$(TransformFile)" />

    <Copy SourceFiles="$(ConfigFile)" DestinationFiles="$(BackupFile)" />

    <Exec Command="attrib -r $(ConfigFile)" />

    <TransformXml Source="$(BackupFile)"
                  Transform="$(TransformFile)"
                  Destination="$(ConfigFile)"
                  StackTrace="false" />

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