MSBuild in TeamCity of Visual Studio 2012 solution
Solution 1
Actually, you don't need to install Visual Studio on your CI server. You only need to copy a few folders from a development machine to the same location on the CI server.
VS 2015:
- C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web
- C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications
VS 2013:
- C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web
- C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\WebApplications
VS 2012:
- C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web
- C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications
VS 2010:
- C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web
- C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications
.NET 4.6:
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6
.NET 4.5.2:
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2
.NET 4.5.1:
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1
.NET 4.5:
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5
.NET 4.0.1:
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0.1
.NET 4.0:
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
Or, as Matt suggested, you could copy them into a subdirectory of your project and alter the <MSBuildExtensionsPath32>
location in your MSBuild (typically .csproj
or .vbproj
) file.
Once you have done this, your project will compile. You should still set the VisualStudioVersion explicitly to the one you are using just to be sure it is set right.
NOTE: This solution works for all project types (including web projects). For a web site (that has no project file), I ended up installing the Windows SDK matching the .NET SDK version I am using, because there were missing registry keys that were causing it not to build.
Solution 2
Turns out it's really simple. To make MSBuild run VS2010 as the builder on a solution made by VS2012 in TeamCity, simply set the environment variable for the build configuration like this:
Name: env.VisualStudioVersion
Value: 10.0
Note TeamCity does not need VS2012 installed.
Solution 3
Alternatively, you can copy the build targets you need from the c:\Program Files (x86)\MSBuild to a subdirectory of your project (for example .\Build) making sure to preserve structure and add the following to your csproj:
<!-- redirect msbuild path so targets can be added to source control -->
<PropertyGroup>
<MSBuildExtensionsPath32>..\Build\</MSBuildExtensionsPath32>
</PropertyGroup>
For example, if my project root is C:\Dev\MyProjSln\MyProj
- Create Folder C:\Dev\MyProjSln\Build\Microsoft\VisualStudio\version\WebApplications\
- Copy Contents of C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\version>\WebApplications\ to created folder
- Add MSBuildExtensionsPath32 element to Property Group under Project node in csproj
- Profit!
Personally, I prefer this method of tracking build target dependencies, as it prevents build server from being dependent on having undocumented folder structure requirements, and gets your dependencies into source control
Solution 4
As described here:
- Install nuget
MSBuild.Microsoft.VisualStudio.Web.targets
- Edit the .csproj file
Replace:
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
with:
<Import Project="..\packages\MSBuild.Microsoft.VisualStudio.Web.targets.11.0.2.1\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets" Condition="true" />
Obviously you have to make sure versions match your case both on the nuget installed and path in <Import>
Solution 5
I totally disagree with changing the project files because that might affect other developers. This is what worked for me since the v11.0 folder was missing on MS build folder: 1)Create v111.
- Create v11.0 folder on C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio
- Copy Web and WebApplications folders from my development box "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0"
- Booom! work like a charm\
- Note: I installed "Microsoft Visual Studio 2012 Shell (Isolated) Redistributable Package"
Ian Vink
https://mvp.microsoft.com/en-us/PublicProfile/5002789?fullName=Ian%20Vink
Updated on July 05, 2022Comments
-
Ian Vink almost 2 years
I have a VS 2012 web project /sln that I am trying to build in TeamCity. it uses .NET 4.5 which is installed on TeamCity.
The TeamCity server has VS 2010 installed only.
I get this error when the build runs:
C:\BuildAgent\work\d5bc4e1b8005d077\CUSAAdmin.Web\CUSAAdmin.Web.csproj(799, 3): error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. Project CUSAAdmin.Web\CUSAAdmin.Web.csproj failed. Project CUSAAdmin.sln failed.
It is trying to use Visual Studio 2012 (v11.0) to build.
I have set the VisualStudioVersion to be 10 in the build.xml though??
<Target Name="BuildPackage"> <MSBuild Projects="CUSAAdmin.sln" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(Configuration); VisualStudioVersion=10.0" />
As well inside the project it is defaulting to VS2010
<PropertyGroup> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
-
Mark Carpenter almost 11 yearsThis is great! I had the opposite problem - TC was defaulting to v10 when I needed to use v11. Using
env.VisualStudioVersion
=11.0
worked. Thanks! -
Maxim V. Pavlov almost 11 yearsBut it does need at least VS2010 installed on the same machine as teamcity server, right?
-
Ian Vink almost 11 yearsNo, it doesn't. VS2012 is backward compatible with VS2010 proj and sln.s
-
Jacob Hamacher over 10 yearsDoes the license for Visual Studio allow to copy from MSBuild?
-
NightOwl888 over 10 yearsAccording to stackoverflow.com/questions/3980909/…, you do not need a license. I have not found an official document stating this, but this seems to consistently be the advice given by Microsoft employees (See answer by Sayed Ibrahim Hashimi on the same page).
-
Geovani Martinez about 10 yearsAnyone got a clue as to why teamcity attempts to use C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0 if I do not even have it installed?
-
Colin Pear almost 10 yearsFor Visual Studio 2013 change the directory v11.0 to v12.0
-
P6345uk almost 10 yearsreally nice work step 4 was the super useful one for me. For v12.0 use Microsoft Visual Studio 2013 Shell (Isolated) Redistributable Package
-
jslatts over 9 yearsThis is the way to go and works for me. Helps prevent the "It builds on my workstation" issue as well.
-
Zinov about 7 yearsTo prevent doing this, I think in my opinion is a better way, and is installing the Microsoft.Net.Compilers version and with that you get the same structure of the MSBuild folder as a nuget dependency, and there is no need to modify the csproj. The bad side of this in my opinion is that you need to do the same think for every project in your solution that depends on the same version of your MSBuild, but in your case is the same. What do you think @Matt?
-
jpmc26 about 7 yearsAlternatively, you can get these files from NuGet: nuget.org/packages/MSBuild.Microsoft.VisualStudio.Web.targets/…. Not entirely sure if any csproj modification is required or if it handles that automatically.