zoukankan      html  css  js  c++  java
  • Making MSBuild / Visual Studio to automatically copy all indirect dependencies to "bin" folder

     
    Yesterday I asked this question on StackOverflow.com, 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:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    /// <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:
    1
    2
    3
    4
    5
    6
    <Target Name="AfterBuild" DependsOnTargets="RequiresPostSharp">
      <CreateItem Include="$(SolutionDir)Lib*.*">
        <Output TaskParameter="Include" ItemName="CopyFiles" />
      </CreateItem>
      <Copy SourceFiles="@(CopyFiles)" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" />
    </Target>
    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:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    <?xml version="1.0" encoding="utf-8"?>
     
      <PropertyGroup>
        <CopyIndirectDependencies   
          Condition="'$(CopyIndirectDependencies)'==''">true</CopyIndirectDependencies>
        <CopyIndirectDependenciesPdb
          Condition="'$(CopyIndirectDependenciesPdb)'==''">false</CopyIndirectDependenciesPdb>
        <CopyIndirectDependenciesXml
          Condition="'$(CopyIndirectDependenciesXml)'==''">false</CopyIndirectDependenciesXml>
      </PropertyGroup>
     
     
      <!-- BuildXxx part -->
     
      <Target Name="CopyIndirectDependencies"
              Condition="'$(CopyIndirectDependencies)'=='true'"
              DependsOnTargets="DetectIndirectDependencies">
        <Copy Condition="'%(IndirectDependency.FullPath)'!=''"
              SourceFiles="%(IndirectDependency.FullPath)"
              DestinationFolder="$(OutputPath)"
              SkipUnchangedFiles="true" >
          <Output TaskParameter="CopiedFiles"
                  ItemName="IndirectDependencyCopied" />
        </Copy>
        <Message Importance="low"
                 Condition="'%(IndirectDependencyCopied.FullPath)'!=''
                   and '%(IndirectDependencyCopied.Extension)'!='.pdb'
                   and '%(IndirectDependencyCopied.Extension)'!='.xml'"
                 Text="Indirect dependency copied: %(IndirectDependencyCopied.FullPath)" />
      </Target>
     
      <Target Name="DetectIndirectDependencies"
              DependsOnTargets="ResolveAssemblyReferences">
         
        <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)"
                    Condition="'%(ReferenceDependencyPaths.CopyLocal)'=='true'">
          <Output TaskParameter="Include"
                  ItemName="_IndirectDependency"/>
        </CreateItem>
        <CreateItem Include="%(ReferenceDependencyPaths.RootDir)%(ReferenceDependencyPaths.Directory)%(ReferenceDependencyPaths.Filename).xml"
                    Condition="'%(ReferenceDependencyPaths.CopyLocal)'=='true' and '$(CopyIndirectDependenciesXml)'=='true'">
          <Output TaskParameter="Include"
                  ItemName="_IndirectDependency"/>
        </CreateItem>
        <CreateItem Include="%(ReferenceDependencyPaths.RootDir)%(ReferenceDependencyPaths.Directory)%(ReferenceDependencyPaths.Filename).pdb"
                    Condition="'%(ReferenceDependencyPaths.CopyLocal)'=='true' and '$(CopyIndirectDependenciesPdb)'=='true'">
          <Output TaskParameter="Include"
                  ItemName="_IndirectDependency"/>
        </CreateItem>
     
        <!-- Filtering indirect dependency list by existence -->
        <CreateItem Include="%(_IndirectDependency.FullPath)"
                    Condition="Exists('%(_IndirectDependency.FullPath)')">
          <Output TaskParameter="Include"
                  ItemName="IndirectDependency"/>
        </CreateItem>
     
        <!-- Creating copied indirect dependency list -->
        <CreateItem Include="@(_IndirectDependency->'$(OutputPath)%(Filename)%(Extension)')">
          <Output TaskParameter="Include"
                  ItemName="_ExistingIndirectDependency"/>
        </CreateItem>
     
        <!-- Filtering copied indirect dependency list by existence -->
        <CreateItem Include="%(_ExistingIndirectDependency.FullPath)"
                    Condition="Exists('%(_ExistingIndirectDependency.FullPath)')">
          <Output TaskParameter="Include"
                  ItemName="ExistingIndirectDependency"/>
        </CreateItem>
     
      </Target>
     
     
      <!-- Build sequence modification -->
     
      <PropertyGroup>
        <CoreBuildDependsOn>
          $(CoreBuildDependsOn);
          CopyIndirectDependencies
        </CoreBuildDependsOn>
      </PropertyGroup>
    </Project>
    Its intended usage: add a single highlighted line importing this file to any .csproj / .vbproj.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     
      ...
     
      <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>
      <Target Name="AfterBuild">
      </Target>
      -->
    </Project>
    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.

    http://blog.alexyakunin.com/2009/09/making-msbuild-visual-studio-to.html

  • 相关阅读:
    SSH 多台VM之间无密码登录
    JavaScript Calling WebService by SOAP
    Excel大数据排查重复行内容方法,三步搞定!
    linux添加软件的service start/stop快捷服务(简单版)
    Linux服务器其中一个磁盘满了怎么办?在不做磁盘扩容的情况下,一个软连接就搞定。
    oracle数据库中将clob字段内容利用java提取出至文本文档中
    最全的常用正则表达式大全——包括校验数字、字符、一些特殊的需求等等
    mysql数据库误删除操作说明
    mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法!
    新建ftp账号,并使登陆者只访问当前目录
  • 原文地址:https://www.cnblogs.com/vivianlou/p/3865569.html
Copyright © 2011-2022 走看看