How to build dependent project first with msbuild

ぃ、小莉子 提交于 2020-03-01 04:07:46

问题


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.


回答1:


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>



回答2:


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...




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/9728953/how-to-build-dependent-project-first-with-msbuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!