1

I have two projects (csproj) that I would like to combine and compile into one assembly, but I would like to continue to have them as separate projects as well. I thought this would be pretty straightforward with MSBuild, but it isn't working like I expected.

A minimal example demonstrating what I was hoping to do is the following:

[Full.proj]

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

  <Import Project="Proj1/Proj1.proj"/>
  <Import Project="Proj2/Proj2.proj"/>
  
  <Target Name="Build">
    <Message Text="%(Compile.FullPath)"/>
  </Target>
</Project>

Proj1.proj and Proj2.proj look like the following:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Compile Include="Test#.cs"/>
  </ItemGroup>
</Project>

So, Full.proj simply Imports both of the other projects which I was hoping would include all of their Compile items into one set, and the standard MSBuild scripts for C# projects would take care of the rest. The problem, is that the Compile item definitions end up being relative to Full.proj rather than relative to the project where they are defined (which surprised me).

Executing MSBuild.exe Full.proj will result in:

C:\users\mpflug\Desktop\msbuildtest\Test1.cs
C:\users\mpflug\Desktop\msbuildtest\Test2.cs

What I was expecting:

C:\users\mpflug\Desktop\msbuildtest\Proj1\Test1.cs
C:\users\mpflug\Desktop\msbuildtest\Proj2\Test2.cs

Is there a straightforward way to accomplish this?

MarkPflug
  • 26,749
  • 6
  • 44
  • 51
  • 1
    Why not create a `.sln` file that references both projects and compile that? – Ron Beyer Apr 26 '18 at 20:55
  • Because, I already have an .sln, and compiling it will create two separate assemblies. I'm trying to compile them into one assembly. – MarkPflug Apr 26 '18 at 20:59
  • 1
    [ilMerge](https://github.com/Microsoft/ILMerge/blob/master/ilmerge-manual.md) can do this, and can be called [from MSBuild](https://peteris.rocks/blog/merging-net-assemblies-with-msbuild/). – Dour High Arch Apr 26 '18 at 21:18
  • Take a look at this question: https://stackoverflow.com/questions/49488782/assembly-createinstance-returning-null-even-though-i-can-see-the-class-in-define. It does all the merging during the project build but requires a very different method of creating instances of objects – JayV Apr 26 '18 at 21:34
  • 1
    If you don't want to use ILMerge to combine the assemblies, you could reconfigure your solution to use Shared Projects, where you can share code across projects / assemblies. – David Moore May 02 '18 at 02:44

0 Answers0