Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 720
tastydb Avatar asked Mar 19 '19 09:03

tastydb


People also ask

What is the MSBuild version for Visual Studio 2017?

Visual Studio 2013 uses a ToolsVersion of 12.0. Visual Studio 2015 uses ToolsVersion 14.0, and Visual Studio 2017 uses ToolsVersion 15.0.

How do I get MSBuild without Visual Studio?

To install MSBuild on a system that doesn't have Visual Studio, go to Build Tools for Visual Studio 2022 on the downloads page. Another way of getting MSBuild is to install the . NET SDK.

Where is MSBuildExtensionsPath defined?

This file is installed by Visual Studio to the standard $(MSBuildExtensionsPath) location (C:\Program Files\MSBuild).


2 Answers

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.

like image 195
tastydb Avatar answered Oct 03 '22 17:10

tastydb


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
like image 36
sandyiit Avatar answered Oct 03 '22 18:10

sandyiit