Search code examples
.nettransformationcsproj

TransformXml with new csproj format?


I've recently upgraded a solution from using the old xml format of the csproj files to the new that came after xproj was deprecated. i.e. the format which looks something like this:

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

The problem is that this solution contains alot of xml transformation which have stopped working. These transformation are initiated by the MyProject.wpp.targets file as I've understood it. I haven't set this up myself and I don't have that much knowledge about it but since this file is of the same format as the old csproj files I'm guessing it might be a reason why it's not working but I don't know. Any help is greatly appreachiated on how to get this working again.

This is how the wpp.target file looks like today

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

Solution

  • From Migrating tasks from old csproj to new csproj format #2746 GitHub issue of the MSBuild repository:

    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>