7

I have just started looking into msbuild, because I want to make my own build scripts. For now I am able to create build scripts that compiles only one project, but how do I handle dependencies?

For example what if I have two projects that gets build with these two msbuild scripts?

  1. projectA.xml
  2. projectB.xml

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

I have googled alot on this, but it does not seem to get anything that a starter like me understands. I would be more than happy with a link to an article describing this, or maybe just a small code example.

The reason why I want this control is because of a library I am building. The library consists of several projects. A developer should be able to pull the source code for the library down and build only the libraries that he wants.

Actually I want to be able to build .net modules from the different projects. That is why I want to be able to run a customized msbuild script.

NotMe
  • 87,343
  • 27
  • 171
  • 245
mslot
  • 4,959
  • 9
  • 44
  • 76

4 Answers4

8

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

But that's if you're using standard .csproj projects...

Ok I looked at a project I'm working on, and it's like this:

<ItemGroup>
   <ProjectReference Include="..\SomeFolder\SomeProject.csproj">
      <Project>{1A94B405-2D01-4A09-90D5-A5B31180A03B}</Project>
      <Name>SomeProjectNamespace</Name>
   </ProjectReference>
</ItemGroup>

And here's an MSDN page about references. Scroll down till you find ProjectReference...

joshuahealy
  • 3,529
  • 22
  • 29
  • 1
    Further to my answer, maybe you could create two projects with one dependent on the other and see what the .sln and .csproj files look like, maybe that'll tell you how to do the dependencies in your own custom scripts. – joshuahealy Mar 15 '12 at 22:16
  • Further to my last comment, I did that for you and edited my answer with the details :) – joshuahealy Mar 15 '12 at 22:23
  • 1
    +1 for suggesting the .sln file. If there is no reason that you don't want VisualStudio to manage the order of your builds don't reinvent the wheel. – Bronumski Mar 15 '12 at 22:39
  • 1
    I have updated my question to explain why I want to be able to customise the build process. I will look into the ProjectReference target!! Thanks for sharing this. – mslot Mar 15 '12 at 22:57
  • 1
    I have voted this up because of the "look in the project file" hint. This is a useful resource that I did not realize to use. Thanks for that. – mslot Mar 16 '12 at 12:05
5

I setup my build scripts so that I have a few common targets that do not do anything, but use DependsOnTargets to setup project dependencies and run the build.

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

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

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

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

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

</Project>
BryanJ
  • 8,485
  • 1
  • 42
  • 61
  • I have accepted this because of the MSBuild task hint. This is exactly what I want to use. In this task I can reference another project to build. Thanks for that. – mslot Mar 16 '12 at 12:06
  • i'd like to build projects without having to actually include them in my solution, is this possible? – Alex Gordon Mar 07 '17 at 17:50
2

In MSBuild issue #2887 a similar situation is discussed. The thread also reveals a link to official ProjectReference Protocol.

CodeFox
  • 3,321
  • 1
  • 29
  • 41
  • Thank you for that link! I had the very same problem as discussed there... It's great to have a solution a last. – AquilaRapax Aug 26 '20 at 08:35
-1

You dont need to build using the sln. If you use project references in your csproj then the dependency order is taken care of by MSBuild. Try it. Automajically. You do not need to sort the dependency order in your msbuild script.

James Woolfenden
  • 6,498
  • 33
  • 53
  • 1
    This doesn't always work. When I converted a solution from VS 2010 to VS 2012, that dependent project never gets build in MSBuild.exe (but it does in VS 2012). However, adding project hint as described by "appclay", it works in both now. – Ray Cheng Jun 27 '14 at 20:59