Reduce the execution time of VisualStudio solution Build using msbuild - visual-studio-2010

I have an MSbuild project used for building Visual studio solution.Before using msbuild it was using devenv.exe for building the solution.It took only 1 hour to build the solution.
After using msbuild it is taking 3 hours
<Target Name ="Buildsln">
<MSBuild Projects="$(Solutionpath)\Test.sln" Targets="Rebuild"
Properties="Configuration=Release;Platform=Win32" BuildInParallel="True">
</MSBuild>
</Target>
How can i increase the speed
If i use msbuild.exe from an EXEC task with /m switch it is taking only 1 hour. But i couldn't use that. Please provide any suggestions

I notice your MSBuild is doing a "Rebuild" instead of a Build. When you build within VS are you doing a full Rebuild or a regular Build. Rebuild will typically be much slower and could explain the behavior you're seeing.

Related

How to prioritize a project in a VisualStudio-solution?

We have a solution with around 70 projects. One of them takes relatively long (~10min) but does not use system resources. We also employ parallel build to speed things up.
When I (re)add this project to the solution, it is at the end of the build order. The machine is 100% busy when compilong 69 projects and then 10min idle when compiling the 70th. When I manually edit the .sln-file so that the project comes first in all lists, it is somewhere in the middle. How can I move it to the beginning?
This is not about dependencies. This project A has only one to another project B and I am fine if B is first as long as A is second. Also, no other projects depends on project A.
It sounds like you have already tried editing project dependencies in visual studio. If you have already edited it to make the project first, but it still takes a while then you probably should just take it out of the solution file. Then put the building of it into your own msbuild script where you can use the MSBuildExtensions parallel tasks to make it build at the same time as everything else:
See https://mikefourie.wordpress.com/2012/02/29/executing-msbuild-targets-in-parallel-part-1/
And I'm pretty sure the MSBuildExtensions library is a nuget package now as well.
VisualStudios sln-files are very limited and are written in a format defined decades ago. In fact, it is converted to a msbuild-script before doing anything useful.
To have more flexibility, I added an msbuild-script (master.msbuild) with something similar to this (untested but proper documentation is available)
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectsToBuild Include="longrunningproject.proj" />
<ProjectToBuild
Include="SolutionWithTheOther69Projects.sln"
Properties="Configuration=Debug;Platform=x86"/>
</ItemGroup>
<Target Name="Build" >
<MSBuild
Projects="#(ProjectsToBuild)"
Targets="Build"
BuildInParallel="true"
ContinueOnError="false"
Properties="VeloxVersion=$(VeloxVersion);RootDir=$(RootDir)"
/>
</Target>
</Project>
Projects are executed in order they are defined. Still no control what happens in the solution but I can put projects in front or behind it and can so influence the build order.
If the long-running task is called by an msbuild-exec-task, it is important to set the YieldDuringToolExecution-flag of this task. E.g.
<Exec
Command="..."
YieldDuringToolExecution="true"
/>
Otherwise, things are starting in parallel and then slowly dying off until the exec-task is done. I could not decode the logic behind that but honestly, I do not care.
After several days of try and error, the build machine screams at 100%-cpu-load, slowly come down to the one long-running task and then is done. Speedup-factor 2.5 :D
How can I move it to the beginning?
You can create a MSBuild project file named "before.<SolutionName>.sln.targets" in the same folder as your solution.
Then build the solution with command line (Visual Studio will ignore this file.), the before.<SolutionName>.sln.targets will be built before all of the Visual Studio projects in the solution.
In this case, we just need to build that special project in the before.<SolutionName>.sln.targets file, that special project will be built before all of projects in the solution.
The content of before.<SolutionName>.sln.targets like:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="BuildSpecialProject" BeforeTargets="Build">
<Message Text="Build My Specify Project" />
<MSBuild Projects="Path\YouSpecialName.csproj"/>
</Target>
</Project>
Then build the solution file with command line with MSBuild or dotnet:
msbuild /t:build "<SolutionPath>\<SolutionName>.sln"
dotnet build "<SolutionPath>\<SolutionName>.sln"
Check this thread for some more details.
Hope this helps.

msdeploy `AfterTargets="MSDeployPublish"` not hit when publishing from within VS

I have a target to execute after publication in my csproj:
<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish">
<Message Text="After publish" Importance="high" />
<Exec Command="git branch -f $(PublishProfile)" />
</Target>
This indeed get called when I publish from the command line like so:
msbuild /p:DeployOnBuild=true /p:PublishProfile=Staging /p:Password=*** /p:AllowUntrustedCertificate=true
It does not get hit when publishing from Visual Studio, however. Neither do I see the message, nor does git do any moving of the branch pointer.
Any ideas what I could try?
EDIT:
For those who care, the problem here was that PublishProfile was not set while publishing from VS. The property that works for both VS and command line is PublishProfileName.
Turn on "detailed" build debugging in VS (Tools|Options|Projects and Solutions|Build and Run)
Also set your MSBuild verbosity at the commandline as well.
/v:detailed
Then compare the output from each to find the differences.
FYI - I couldn't reproduce your situation but this should help you debug further.

Team foundation server auto build

We have a large non Visual Studio C++ project which is build via a batch file. I have integrated this up in Visual Studio as a makefile project with customised 'Build Command Line' in the project properties, this works well and has done for over a year.
I am now looking to introduce Team Foundation Server and configure automated builds.
If I build the project in Visual Studio then the project build as normal.
If I use the automated build sever to build the project the build fails every time
The option to view the log file in the build report is greyed out. The report given is
The command "Autobuild.cmd" exited with code 1.
Where autobuild is the cmd file specified in the solution's 'Build Command Line'.
Can anyone think of a reason why this might be the case, I assumed that the build sever would just run the Build command with the project specified in the 'Items to Build' box. If this is the case then I cannot see why it would fail.
There are loads of possible reasons; unfortunately you've not given enough information to answer that questions.
The first thing to try is changing your build definition logging level to Diagnostic. (you can also do this when you queue a new build).
What I also recommend is actually wrapping your makefile in an msbuild.proj file.
Something simple like:
<Project ToolsVersion="4.0" DefaultTargets="build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="build">
<Exec Command="autobuild.cmd" />
</Target>
</Project>
The Exec task has loads of options to customize how the cmd is called and should help you diagnose your error.
In general if you want to take advantaged of TFS builds I'd recommend migrating as much of your autobuild.cmd to msbuild as possible. MSBuild is TFS's "makefile" language, so-to-speak.
In your build definition you have an option of logging, there you can change that to Verbose logging. Maybe you get some information in your log file then.
And for the issue of exited with code 1 I think you will need to add some extra informational lines to be printed in the cmd program and when your logging in TFS Build is active you maybe see more information on it.

MSBuild task to Build other solution projects fails in VS but works with MSBuild.exe command line

I have a custom MSBuild task which among other things adds embedded resources to other projects in the solution. After adding the resources I'd like to then build those projects, but found I can't get this working within Visual Studio.
To test, I stripped out the custom task entirely and redefined a simple AfterBuild target in the web project of a Silverlight solution. The target uses the MSBuild task to build the Silverlight application project in the solution, and looks like this:
<Target Name="AfterBuild">
<PropertyGroup>
<LinkedProject>..\SilverlightApplication1\SilverlightApplication1.csproj</LinkedProject>
</PropertyGroup>
<MSBuild Condition="'$(LinkedProject)' != '' "
Projects="$(LinkedProject)"
Targets="Build"
Properties="CustomFlag=true" >
</MSBuild>
</Target>
The odd thing is that this works perfectly when using MSBuild from the command line, yet does not work in Visual Studio when building the web project. I thought this might be some sort of Silverlight problem, and had the task build a .NET class library project instead, but the result was the same - it worked from the command line but not within VS. In VS there's no actual error - it's just that the Csc task does not compile the assembly and generates no output.
What do I need to do to get this working within Visual Studio?
Pass the 'UseHostCompilerIfAvailable=false' property to the MSBuild task.
It looks like Visual Studio breaks badly if csc is invoked from a MSBuild task as it reuses the initial project build settings for its in-process host compiler. In my case, I was building the same project twice - default build was using target framework v3.5, with a AfterBuild MSBuild task specifying v4.0. I ended up with the same issue - csc appeared to run but produced no output. I think what was happening was that with the UseHostCompilerIfAvailable property set to true, csc was calling the hosted compiler which reused my initial project settings, so even though the command line showed csc "building" my v4.0 assembly, the host compiler was simply overwriting the v3.5 one I had just built!
Change Visual Studio verbosity to detailed and check build log. I think that CoreBuild is not executed if your files have not changed, so you could try to use AfterCompile instead of AfterBuild.

How can I build a SharePoint 2010 package using command line?

I have a Visual Studio 2010 SharePoint project. If I choose 'Package' from the project menu, a .wsp file is generated. How can I invoke the same build from command line (i.e. what /target is required for MSBuild)?
I got it to work, finally. The tricky part is the fact that the SharePoint targets do not exist when MSBuild loads the .sln file, you have to load the individual .csproj files.
set msbuild="C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
set config=Debug
set outdir="C:\out\"
%msbuild% /p:Configuration=%config% /m ../My.SharePoint.Project/My.SharePoint.Projectcsproj /t:Package /p:BasePackagePath=%outdir%
This is also a useful document here: http://msdn.microsoft.com/en-us/ff622991.aspx
"To generate packages when building in
TFS 2010, set the parameter
/p:IsPackaging=True on MSBuild"
Also to package project with msbuild you can use target Package:
Define new target "BuildAndPackage"
<Target Name="BuildAndPackage">
<CallTarget Targets="Build"/>
<CallTarget Targets="Package"/>
</Target>
Use new target in build process:
<Project ToolsVersion="4.0" DefaultTargets="BuildAndPackage">
But this approach not recommended because it may cause errors in TFS Build process..
Set the MSBuild's verbosity to 'maximum' and you should see what is called from the build console.
In VS2010 of course :)

Resources