Peter Schneider
Peter Schneider

Reputation: 1701

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.

Upvotes: 1

Views: 384

Answers (3)

Peter Schneider
Peter Schneider

Reputation: 1701

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

Upvotes: 1

Leo Liu
Leo Liu

Reputation: 76760

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.

Upvotes: 0

C.J.
C.J.

Reputation: 16081

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.

Upvotes: 1

Related Questions