Define build order on build machine - visual-studio

I know that I can define a specific build order using dependencies in Visual Studio, but my question is do those dependencies and build order stay the same whenever I check-in my work to TFS and run a build on the TFS server? I ask because I am having some issues when it comes to having multiple projects in the same solution where project B references project A, and project C references project B, etc.
The solution builds locally just fine because I have set the dependencies such that project B always builds first, but it doesn't seem like this configuration carries over to TFS.
Is this possible to do on the TFS build side?

TF Build will build your projects in the same order, dependency order, that they are built in Visual Studio.
If you reference a project (no reference a projects output but the project) then it will all cascade correctly. Direct Binary references do not cascade correctly as both VS and MSBuild assume it is already built.

Make sure your updated solution file, with the build order, Is committed to TFS.
Ensure that your build is targeting this solution file rather than a list of projects.
As others have said Project References are the key, check all references point to the project and not the binary output.
If the references are correct then the build order will be correct automatically and manual intervention is only really required for projects such as wix projects where the installer project should always be built last after all of the other outputs have been completed.

Related

Exclude projects from Azure build pipeline

I have a solution which I have a number of unloaded projects. When I build the solution locally the unloaded projects are obviously not built. In Configuration Manager, I can't even select configuration, platform, build and deploy options for the unloaded projects.
Now I have created an Azure Pipeline which also builds the solution. But the unloaded projects are still being built.
How can I exclude building projects in my pipeline?
Now I have created an Azure Pipeline which also builds the solution.
But the unloaded projects are still being built.
Both Azure Devops Pipeline and VS IDE would call msbuild.exe to build. When building a solution using msbuild, it actually is running command msbuild xx.sln /t:build ....
Check the content of xx.sln file (solution file) we can find it defines definitions about the projects within the solution. That's why if you build the solution, all these projects will be built. This is expected behavior when building whole solution. Sample .sln file:
And it's VS IDE's magic to exclude unloaded projects when building whole solution. The IDE will check the unloaded projects and skip them when building whole solution, it has advantage that the unloading project behavior in IDE won't modify your solution file. (It's important for solution under Git version control).
To sum up: It's expected that building solution will build all projects. When we unload the projects in VS, this action won't affect our source solution file or project file. VS will automatically skip the unloaded projects, which is the unique feature of IDE. (Unload is a UI action in VS, it has no effect on .sln/.xxproj file)
To exclude projects to build in Azure Devops:
1.Specify projects to build when building whole solution. (It doesn't change any file, so that it won't affect Version control)
You can check this answer and corresponding document. My devops task setting (no need to specify Build target):
2.Exclude projects from solution configuration (Not recommended) or create a new configuration that excludes specific projects (Recommended). The solution configuration is defined in solution file (sln), so this way would makes effect on Source control.
Exclude projects from solution configuration(Not recommended):
Create a new configuration that excludes specific projects:
Related document here. I suggest you can do the changes via VS IDE, and then push the commits to remote repo. It's complex if you're not familiar with msbuild and sln file, so I doesn't suggest doing that yourself. Let IDE do the job for you.
Once the remote repo get the changes:
We can build the solution in devops with customrelease configuration which excludes ProjectA and ProjectB instead of release configuration which contains all projects.
All these workarounds had been tested on my side before posting them.

Visual Studio 2019 build order is not working

My solution structure is like this:
WiX Installer: depends on Project A and B
Project A: depends on Project B
Project B: no dependencies
It is configured like that in the build dependencies. The displayed build order (right click on the solution -> project build order) is the correct one (Project B -> Project A -> Installer). But for some reason when i try to rebuild the whole solution thats not the order in which Visual Studio actually tries to build the projects. It always starts with Project A which of course then fails cause of the missing dependency of Project B.
If i manually build the projects in the right order everything works.
In my .sln file projects are listed like this:
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectA", ProjectA\ProjectA.csproj", "{B80B7A8F-0576-41FA-BD3D-B3C6F5F8D6E7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectB", "ProjectB\ProjectB.csproj", "{D127D2C1-0F13-41F1-B4A1-218BC53ABC40}"
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Installer", "Installer\Installer.wixproj", "{1D504782-E92A-4C60-9ADC-6067E7E301AA}"
ProjectSection(ProjectDependencies) = postProject
{B80B7A8F-0576-41FA-BD3D-B3C6F5F8D6E7} = {B80B7A8F-0576-41FA-BD3D-B3C6F5F8D6E7}
{D127D2C1-0F13-41F1-B4A1-218BC53ABC40} = {D127D2C1-0F13-41F1-B4A1-218BC53ABC40}
EndProjectSection
EndProject
Any idea what might cause this issue?
Usually, the build order is saved in the solution file(.sln file) like this:
So when you use this, you should build the whole solution with that file. But
However, in vs IDE, when you build A single project, it can follow the specified build order, but in msbuild command line, msbuild projectA.xxproj does not build B first and then build A according to the specified dependencies. This is also unique to vs ides, although the build dependencies are stored in xxx.sln rather than individual xxx.proj files.
Unless the entire solution(msbuild xxx.sln) is built in MSBuild Command Line, it will be built sequentially.
This is the situation that generally causes this difference.
But for some reason when i try to rebuild the whole solution thats not
the order in which Visual Studio actually tries to build the projects.
I wonder what you did caused this and if you build the whole solution(xxx.sln file), this issue will not happen. So I want to know which build format or what you did to your solution.
Since MSBuild cannot build wixproj file, so you should use VS IDE or devenv xxx.sln /build to build the whole solution.
Suggestion
As a suggestion, you could use Project Reference instead to specify build order which will set the order in every xxx.proj rather than xxx.sln file. This will be more reliable and safe.
1) Remove the build orders under Project Dependencies(Right-click on Solution)
2) Right-click on WIX project-->References-->Add Reference-->Projects-->Select Project A and Project B.
3) Right-click on Project A-->References-->Add Reference-->Project-->Project B.
In addition, if it does not help you, please share with us what you did to cause this issue and any steps which caused it so that it will help us troubleshoot your issue more quickly.
Update 1
Just hint from Eric, and thanks to him for sharing the solution and test result.
Solution
First, remove all the project dependencies and then re-add them, after that, it fixes the issue. It could be an issue to this project since it was migrated from old VS2010.
I had the same problem. I was able to build my solution in Visual Studio. But when building it on Azure DevOps Pipelines with MSBuild, it built them in the wrong order; with my WiX .wixproj project being built before one of its dependencies.
My .sln file looked like this:
What I did to solve the problem, was to manually add more dependencies to this file:
So my advice is to look in your .sln file and add additional dependencies that you think would make sense.

How to make one project build before all others?

I know you can set Build Dependencies for projects in Visual Studio and that will make the dependency build before the other projects. However, I have a project that all others will depend on (including new ones that may be added in the future), and so I'd rather just say "this project must build first, before all others" without bothering to set a dependency for every other project. Is there a way to do this in VS?
Is there a way to do this in VS?
Visual Studio calculates the build order according to your dependence. Like project A depends on project B, Visual Studio will build B prior to A. So if you want to make one project build before all others, you could not avoid this bothering setting or Visual Studio could not get the build order, will build in the error order.
In order to avoid set a dependency for every other project, you can right-click on your solution and find the Project Dependencies menu item:
The dialog that opens will allow you to set build dependencies and show you the calculated build order (you can affect the order by changing the dependencies).
In this case, you can set a dependency for every other project at once, no need to set for each one, but for new ones that may be added in the future, you still need to do the settings after added.

Forced to Rebuild Project Depedencies in Visual Studio 2012

I recently upgraded to VS.NET 2012 and I started encountering a very frustrating issue when debugging.
Project A has a project reference to Project B. When I edit Project B, I would expect that action of building/debugging Project A to detect the change in Project B and automatically include it during the build of Project A. (That's kinda the point of project dependencies.) And that's exactly what used to happen in VS 2010.
But this doesn't happen in 2012. Indeed, even if I build Project B explicitly, Project A will not pick up that change unless I rebuild project A.
So now I'm forced to explicitly build Project B then go back to Project A and explicitly rebuild it for the modifications in Project B to be included.
Thoughts?
Did you set the dependency projects in your solution?
We can set them from:
Right click solution->Properties->Project Dependencies
By the way, did you build your solution in the Visual Studio IDE or by msbuild command?
If you use msbuild to build the projects, you should add DependsOnTargets attribute in your project files.
More information you can refer to:
http://msdn.microsoft.com/en-us/library/ms366724.aspx
Your 3rd paragraph seems to indicate a misunderstanding with the way dependencies work. Forgive me if I am mistaken.
If project A has a dependency on project B, building project B would not force a rebuild of project A. Building project A however should check for changes in project B and rebuild B automatically if needed.
Project A in this case is the parent or root project. Building the parent would check all the projects on which it depends and rebuild them if they are out of date. Always building the parent would automatically build outdated dependencies first, and then the parent project would be built picking up any new changes in the dependencies.
A
\
B
|\
C D
In this example, if B had dependencies they would be built as well. If you explicitly built B, its dependencies C and D would be checked and rebuilt but not A.
If you explicitly built A, first C and D would be checked and rebuilt if necessary, then B, then finally A.
I had a similar problem before, and the cause was that the A's reference to B was a regular reference (to the DLL file), rather than a "Project Reference". This situation would certainly explain the behaviour you see.
To check, look at ProjectA.csproj in a text editor, and examine all the <Reference> and <ProjectReference> elements to make sure they are the way you think they should be.
I think you're looking for a continuous integration (CI) server. A CI can be configured to monitor source repositories and triggered to build target assemblies when new changes have been committed to the code base.
Otherwise, as others have mentioned, project dependencies can be used to rebuild referenced assemblies.
One potential cause of this is locked .suo files. See my answer on this question:
Visual Studio 2012 not building dependent projects

Project dependencies across multiple Visual Studio versions

I have 3 .net projects.
Project1.dll is generated by a VS2008 project.
Project2.dll is generated by a VS2005 project that references Project1.dll.
Project3.dll is generated by a VS2008 project that references both Project1.dll and Project2.dll.
Right now, I build Project1.dll, and manually copy it to the place where Project 2 can pick it up.
Then I build Project2.dll and manually copy it and Project1.dll to the place where Project 3 can pick them up.
Obviously I'm doing something wrong (manual). What is the correct way to keep my projects' references up to date?
Updating Project2 to VS2008 and then creating one solution containing all 3 projects is not an option at this time. We have a 3rd party visualstudio plugin that does not yet work in VS2008. Project2 must stay in VS2005
De-updating Project1 and Project3 to VS2005 and then creating one solution is not an option either. We're relying on C# 3.0 and .net 3.5 features in those projects.
Probably the best option would be to have a common build folder for all three projects. This can be done in the Project Properties-> Build -> Output path. Then point the references to the output folder. That way anytime you build any of the lower projects, the higher projects would have the latest versions. You can set the path per configurations (Debug, Release) as well, so you won't need to change that for each type of build.
How about a pre-build event for Project3, that goes out and uses a batch file to build Project1 copy it to Project2 folder and then build project2 and copy it to project3 folder.
I would recommend sharing the csproj/vbproj files between the solutions. The format of the project files is compatible between the two versions of studio (solution files are not, however), and as long as your VS2008 projects are targeting the 2.0 runtime you should have no trouble compiling them. This will allow you to reference the projects, which will take care of dependencies.
The only place where this gets hairy is if you have a web project that needs to work between the two versions of studio. In that case there are some modifications to the project files which will point to the correct MSBuild target files.
We use a build script that handles the dependencies, builds the DLLs and does what you're doing manually.
A trick I have used in the past is to move everything to 2008. Then I setup a special solution in 2005 for project two and use it to work with the addin. Getting this to work just depends on how bad project two behaves in 2008.

Resources