How to build a dependent project first with msbuild

I just started learning msbuild because I want to create my own build scripts. At the moment I can create build scripts that only compile one project, but how do I handle dependencies?

For example, what if I have two projects that are building with these two msbuild scripts?

  • projectA.xml
  • projectB.xml

How do I tell msbuild that when I execute projectB.xml it should execute projectA.xml first?

I have a lot for this, but it seems that he does not understand anything, as a beginner understands like me. I would be more than happy with a link to an article describing this, or maybe just a small sample code.

The reason I want this control is because of the library I am building. The library consists of several projects. The developer should be able to pull out the source code of the library and create only those libraries that he needs.

Actually I want to be able to create .net modules from different projects. This is why I want to be able to run a customized msbuild script.


source to share

3 answers

I'm setting up build scripts so I have a few general targets that don't do anything, but use DependsOnTargets to set up project dependencies and run the build.

<Project ToolsVersion="4.0" xmlns="">

  <!-- ************************************************************************************************ -->
  <!-- Targets that run the builds -->
  <!-- ************************************************************************************************ -->
  <Target Name="AutoBuild" DependsOnTargets="BuildProject1;BuildProject2;BuildInstallers">
    <OnError ExecuteTargets="NotifyFailure" />
  <Target Name="FullCompile" DependsOnTargets="BuildProject1;BuildProject2">
    <OnError ExecuteTargets="NotifyFailure" />

  <!-- Build Project 1 -->
  <Target Name="BuildProject1">
    <!-- Use MSBuild task and point it to build project1.csproj, project1.sln or whatever your projects is -->

  <!-- Build Project 2 -->
  <Target Name="BuildProject2">
    <!-- Use MSBuild task and point it to build project2.csproj, project2.sln or whatever your projects is -->

  <Target Name="BuildInstallers">
    <!-- Whatever logic you have for building installers -->





If you are building a solution with two projects, you can target the .sln file to msbuild rather than build projects directly, it should take care of the project dependencies :)

But if you are using standard .csproj projects ...

Ok, I looked at the project I'm working on and like this:

   <ProjectReference Include="..\SomeFolder\SomeProject.csproj">


And here



You don't need to build with sln. If you are using project references in your csproj then the order of the dependencies is taken from MSBuild. Try it. Automajically. You don't need to sort the order of the dependencies in your msbuild script.



All Articles