How to compile another project before compiling main project?

I have a solution with two executing projects. Users can execute project B from project A by pressing a button. Project A Debug folder contains the project B Debug folder, so it executes project B by the hardcoded path to that folder. If I want to make some changes in project B - I need to recompile project B and replace its Debug folder in project A Debug folder. I think it's not a smart and comfortable way from the point of architecture view. So I want to include such actions in project A build events, but I have never done it before, so I would be happy to hear your suggestions.

Upvotes: 1

Views: 1858

Answers (1)

Markus
Markus

Reputation: 22456

The easiest way to achieve this is to create a reference from A to B, so you coud access the (public) members of B from project A. This way, you could also run the code of B in the process of A, eliminating the need to start a separate process.

If you do have the requirement that you need to have a separate exe for B that runs in a separate process, there is another approach that requires more effort:

  1. Do not create a reference from A to B, but use a Project Dependency* to make sure that B is compiled before A. You can configure project dependencies in a dialog that you access in the context menu of the solution or in the application menu Project -> Project Dependencies. After setting the dependency, you can check the build order on the second tab of the dialog.
  2. Adjust the build of A to copy the output of B to the output folder. There are several ways to do this, one of the more advanced options is to add Build Targets to the project file of A that perform the copy and clean for the files. You have to unload project A and edit the project file (see context menu) and add code similar to the following:

  <Target Name="AfterBuild">
    <ItemGroup>
      <AdditionalFiles Include="$(MSBuildProjectDirectory)\..\ProjectB\bin\$(Configuration)\ProjectB.*" />
    </ItemGroup>
    <Copy SourceFiles="@(AdditionalFiles)" DestinationFolder="$(OutputPath)" />
  </Target>
  <Target Name="AfterClean">
    <ItemGroup>
      <AdditionalFilesToDelete Include="$(OutputPath)\ProjectB.*" />
    </ItemGroup>
    <Delete Files="@(AdditionalFilesToDelete)" />
  </Target>

Please see the documentation on build targets and note that the code above is only a sample that you need to adjust to your specific needs. For instance, it only works with the standard output paths.

Upvotes: 1

Related Questions