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

2

14

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?

Asleyaslope answered 19/3, 2019 at 9:37 Comment(1)
Thanks for your sharing solution. You can mark it as answer. This can be beneficial to other community members and makes it easier to search the solution.Vining
A
15

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.

Asleyaslope answered 19/3, 2019 at 13:6 Comment(0)
M
1

On Visual Studio 2017, instead of edits the .csproj file, you can simple go to Visual Studio Package Manager Console (Tools -> NuGet Package Manager -> Package Manager Console) and install Web.Targets:

Install-Package MSBuild.Microsoft.VisualStudio.Web.targets
Mulberry answered 25/7, 2020 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.