Get the correct $(VisualStudioVersion) for VS 2017 inside a MSBuild file

tastydb picture tastydb · Mar 19, 2019 · Viewed 8.2k times · Source

I have a MSBuild file to publish a solution (created in VS 2013 and ported to VS 2017) to a remote server. The offending line is this one:

<PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup> 

When I run the command:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\msbuild" WebSite1_Web_configs.build /p:Configuration=Integracion;Platform="AnyCPU" /p:VisualStudioVersion=15.0

After some compilation, it ends up with this error:

Error MSB4062 The "TransformXml" task could not be loaded from the assembly C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll

Of course it can't be loaded, because the version should be 15.0, not 12.0.

Things I have tried:

  • Adding the /p:VisualStudioVersion=15.0 to the command where I call MSBuild. It doesn't work.

  • Changing, inside the *.csproj file, the element:

<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

For:

<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

It doesn't work.

So, how can I get the correct VS version with $(VisualStudioVersion) inside the MSBuild file without having to hardcode it?

Answer

tastydb picture tastydb · Mar 19, 2019

Ok, the problem was that I had <Import> elements at the end of the *.build file. One of those imports had part of the path hardcoded:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\WebApplications\Microsoft.WebApplication.targets" />

I changed it to:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />

I executed MSBuild with the /p:VisualStudioVersion=15.0 parameter and it worked.

Please, if you have the same problem, check if you have imports with hardcoded paths.