September 16, 2009

Making MSBuild / Visual Studio to automatically copy all indirect dependencies to "bin" folder

Yesterday I asked this question on, and didn't get the answer I wanted. So is it possible to make MSBuild to automatically copy all indirect references (dependencies) to output folder?

Yes, this is possible, and the solution is provided below. But first let's think when this is desirable. Actually I hardly imagine why this does not always happen automatically. Really, if AssemblyA needs AssemblyB, and  my application needs AssemblyA, most likely, it won't work without AssemblyB as well. But as you know, AssemblyB won't be automatically copied to bin folder, if it isn't directly referenced from your project, that is actually a rare case, especially if you tend to use loosely coupled components.

Let's list few particular examples we have:

Case 1. Our SQL DOM project consists of core assembly (Xtensive.Sql) and a set of SQL DOM providers (Xtensive.Sql.Oracle, ...), and its quite desirable to copy all of them to application's bin folder, because generally it can use any provider. Let's think I created Xtensive.Sql.All assembly referencing all of them (btw, I really did this in our repository). Actually, this assembly contains a single type, which will never be instantiated:

  /// <summary>
  /// Does nothing, but references types from all SQL DOM assemblies.
  /// </summary>
  public sealed class Referencer
    private Type[] types = new [] {
      typeof (Pair<>),
      typeof (SqlType),
      typeof (SqlServer.DriverFactory),
      typeof (PostgreSql.DriverFactory),
      typeof (Oracle.DriverFactory),
      typeof (VistaDb.DriverFactory),

    // This is the only constructor. So you can't instantiate this type.
    private Referencer()

As you see, this type references types from all SQL DOM assemblies (including its providers). This is necessary, because otherwise C# complier will not add references to these assemblies to Xtensive.Sql.All.dll, even although the project it is built by includes them.

So practically you can't use this type. But it makes C# compiler to list all the references we need Xtensive.Sql.All.dll assembly:

Note that each of these assemblies also needs many others. For example, let's take a look at Xtensive.Sql.PostgreSql.dll assembly there. It references Npgsql.dll, which in turn references Mono.Security.dll.

So now you understand the problem. I'd like all these assemblies to be in bin folder of my application automatically. I don't want to manually discover all the dependencies and write a code like this to copy them:

  <Target Name="AfterBuild" DependsOnTargets="RequiresPostSharp">
    <CreateItem Include="$(SolutionDir)\Lib\*.*">
      <Output TaskParameter="Include" ItemName="CopyFiles" />
    <Copy SourceFiles="@(CopyFiles)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" />

Case 2. The same is about our Xtensive.Storage providers and assemblies. So I created Xtensive.Storage.All assembly referencing all you might need. This assembly contains very similar Referencer type.

Let's go to the solution now.

Solution: CopyIndirectDependencies.targets.

Here it is:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="">


  <!-- BuildXxx part -->

  <Target Name="CopyIndirectDependencies" 
    <Copy Condition="'%(IndirectDependency.FullPath)'!=''"
          SkipUnchangedFiles="true" >
      <Output TaskParameter="CopiedFiles" 
              ItemName="IndirectDependencyCopied" />
    <Message Importance="low"
               and '%(IndirectDependencyCopied.Extension)'!='.pdb' 
               and '%(IndirectDependencyCopied.Extension)'!='.xml'"
             Text="Indirect dependency copied: %(IndirectDependencyCopied.FullPath)" />

  <Target Name="DetectIndirectDependencies"
    <Message Importance="low"
             Text="Direct dependency: %(ReferencePath.Filename)%(ReferencePath.Extension)" />
    <Message Importance="low"
             Text="Indirect dependency: %(ReferenceDependencyPaths.Filename)%(ReferenceDependencyPaths.Extension)" />

    <!-- Creating indirect dependency list -->
    <CreateItem Include="%(ReferenceDependencyPaths.FullPath)" 
      <Output TaskParameter="Include" 
    <CreateItem Include="%(ReferenceDependencyPaths.RootDir)%(ReferenceDependencyPaths.Directory)%(ReferenceDependencyPaths.Filename).xml"
                Condition="'%(ReferenceDependencyPaths.CopyLocal)'=='true' and '$(CopyIndirectDependenciesXml)'=='true'">
      <Output TaskParameter="Include" 
    <CreateItem Include="%(ReferenceDependencyPaths.RootDir)%(ReferenceDependencyPaths.Directory)%(ReferenceDependencyPaths.Filename).pdb"
                Condition="'%(ReferenceDependencyPaths.CopyLocal)'=='true' and '$(CopyIndirectDependenciesPdb)'=='true'">
      <Output TaskParameter="Include" 

    <!-- Filtering indirect dependency list by existence -->
    <CreateItem Include="%(_IndirectDependency.FullPath)"
      <Output TaskParameter="Include" 

    <!-- Creating copied indirect dependency list -->
    <CreateItem Include="@(_IndirectDependency->'$(OutputPath)%(Filename)%(Extension)')">
      <Output TaskParameter="Include"

    <!-- Filtering copied indirect dependency list by existence -->
    <CreateItem Include="%(_ExistingIndirectDependency.FullPath)"
      <Output TaskParameter="Include"


  <!-- Build sequence modification -->


Its intended usage: add a single highlighted line importing this file to any .csproj / .vbproj.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="">


  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Project="CopyIndirectDependencies.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  <Target Name="AfterBuild">

Check out demo application using this .target:
  • Download and extract it
  • Open it in Visual Studio and build it there, or build it by typing "msbuild" (of course, if it is in your PATH)
  • Check out "bin" folder. It already contains Mono.Security.dll from Lib folder, although application references just Npgsql.dll (it requires Mono.Security.dll).
If you'd like to suppress Visual Studio warning on opening such modified projects for the first time, see this article (in particular, "Non-standard Import Elements" section).

Update: initial version of CopyIndirectDependencies.targets published here was buggy, but now it's fixed.