Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild copy output from another project into the output of the current project

I have a situation where I want to copy the output assembly from one project into the output directory of my target application using MSBuild, without hard-coding paths in my MSBuild Copy task. Here's the scenario:

  • Project A - Web Application Project
  • Project B - Dal Interface Project
  • Project C - Dal Implementation Project

There is a Business layer too, but has no relevance for the MSBuild problem I'm looking to solve.

My business layer has a reference to my Dal.Interface project. My web project has a reference to the Business layer and as it stands, doing a build will pull the business layer and Dal.Interface projects into the output. So far, so good. Now in order for the web app to run, it needs the Dal implementation. I don't want the implementation referenced anywhere since I want to enforce coding to the interface and not having a reference means it won't show up in intellisense, etc.

So I figured I could handle this through the MSBuild copy operation as an AfterBuild task (I have the Dal Implementation setup to build when the web project builds, just not referenced). I don't want to hard code paths or anything else in the MSBuild params, so I'm trying to figure out how to reference the output of the Dal project from the Web Application Project's MSBuild file.

So based on the projects mentioned above this is what I want to see happen:

  1. Web app build is kicked off
  2. All required projects build (already configured, so this is done)
  3. MSBuild "AfterBuild" task kicks off and the output from Project C (Dal Implementation) is copied to the Bin directory of Project A (web app)

Part 3 is where I'm stuck.

I'm sure this can be done, I'm just not finding a good reference to help out. Thanks in advance for any help.

like image 531
Andrew Van Slaars Avatar asked Nov 05 '08 21:11

Andrew Van Slaars


2 Answers

I have made this work, though I would love to find a cleaner solution that takes advanctage of the built-in parameters within MSBuild (like $(TargetDir), etc but to point at the project I want to grab the output for). Anyway, here is what I've done:

<Target Name="AfterBuild">
<Copy SourceFiles="$(SolutionDir)MyProject.Dal.Linq\bin\$(Configuration)\MyProject.Dal.Linq.dll" DestinationFolder="$(TargetDir)"/>
</Target>

I would love to see a cleaner solution, but this should do for now.

like image 85
Andrew Van Slaars Avatar answered Oct 05 '22 21:10

Andrew Van Slaars


So, you want to have a reference, but not have it visible in VS. So you want it built if needed, and copied to output like any other Content file. Here's how you'd do it:

<Target Name="IncludeDALImplementation" BeforeTargets="AfterBuild">
  <MSBuild Projects="..\DalImplementation\DAL.csproj" BuildInParallel="$(BuildInParallel)" Targets="Build">
    <Output TaskParameter="TargetOutputs" ItemName="DalImplementationOutput" />
  </MSBuild>

  <ItemGroup>
    <Content Include="@(DalImplementationOutput)" />
  </ItemGroup>
</Target>
like image 26
Jonathan Avatar answered Oct 05 '22 20:10

Jonathan