具有新 csproj 格式的 TransformXml?

TransformXml with new csproj format?

我最近升级了一个解决方案,从使用旧的 xml 格式的 csproj 文件到 xproj 被弃用后出现的新格式。 即看起来像这样的格式:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <AssemblyName>MyProject</AssemblyName>
    <PackageId>MyProject</PackageId>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
  </PropertyGroup>
</Project>

问题是此解决方案包含许多已停止工作的 xml 转换。 据我了解,这些转换是由 MyProject.wpp.targets 文件启动的。我自己还没有设置它,我也不太了解它,但由于这个文件的格式与旧的 csproj 文件相同,我猜这可能是它不起作用的原因,但我不不知道。 非常感谢有关如何使它再次工作的任何帮助。

这就是 wpp.target 文件今天的样子

<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="[MSBuild]\CommonConfigTransforms.xml"/>
    <Import Project="[MSBuild]\BuildSpecificConfigTransforms.xml"/>
    <Import Project="[MSBuild]\DeleteTempConfigFiles.xml"/>

    <PropertyGroup>
        <PrepareForBuildDependsOn>
            $(PrepareForBuildDependsOn);
            CommonConfigTransforms;
            BuildSpecificConfigTransforms;
        </PrepareForBuildDependsOn>
        <BuildDependsOn>
            $(BuildDependsOn);
            DeleteTempConfigFiles
        </BuildDependsOn>
    </PropertyGroup>
</Project>

来自 Migrating tasks from old csproj to new csproj format #2746 GitHub MSBuild 存储库问题:

You have to change Target to be suitable with new approach (in this situation AfterTargets="PrepareForBuild") so the new part of csproj should looks like this:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="ApplyConfigurationConfigFile" AfterTargets="PrepareForBuild" Condition="Exists('App.$(Configuration).config')">
  <ItemGroup>
    <AppConfigWithTargetPath Remove="App.config" />
    <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
      <TargetPath>$(TargetFileName).config</TargetPath>
    </AppConfigWithTargetPath>
  </ItemGroup>
  <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" />
</Target>

几年后,由于 Microsoft.VisualStudio.SlowCheetah NuGet 包,现在执行 App.config 转换变得更加容易。以下是修改 csproj 以启用转换的方法:添加 Microsoft.VisualStudio.SlowCheetah 包并使用 TransformOnBuildIsTransformFile 分别更新 App.config 原始文件和 configuration-specific 文件元数据。 =15=]

<ItemGroup>
  <PackageReference Include="Microsoft.VisualStudio.SlowCheetah" Version="4.0.8" PrivateAssets="all" />
  <None Update="App.config">
    <TransformOnBuild>true</TransformOnBuild>
  </None>
  <None Update="App.*.config">
    <DependentUpon>App.config</DependentUpon>
    <IsTransformFile>true</IsTransformFile>
  </None>
</ItemGroup>