English 中文(简体)
使用MSBuild针对不同的框架进行定位会导致依赖性问题。
原标题:
  • 时间:2009-02-13 17:52:53
  •  标签:

我有一个小项目,我想要有2个编译版本:

  • one that is targetting the .NET 2.0 framework
  • one that is targetting the .NET 3.5 framework

一切进行得很顺利;我将我的项目置于持续集成(使用CC.NET),并创建了2个CC.NET项目。一个项目用于每个目标框架。

我不会深入(不相关的)细节,但是我的解决方案是针对VS.NET中的.NET 3.5框架进行设置的。

我有两个msbuild任务:

  • one task that builds the solution for .NET 3.5 (simple and easy)
  • one task that builds the solution for .NET 2.0

    在这个任务中,我调用MSBuild,并指定目标框架版本应为v2.0。我还定义了一些额外的构建条件(以便不会在面向.NET2.0的程序集中构建特定于.NET3.5的代码)。

So far, so good. Everything works fine. Now, the problem however is this:

My solution has a few dependencies (references to 3rd party assemblies). In VS.NET, I ve set copy local to true for these dependencies. When CC.NET builds my .NET3.5 version of the assembly, the 3rd party dependencies are indeed copied to my output-directory.

然而,当CC.NET生成我的.NET 2.0版本程序集时,依赖项并未复制到我的输出目录中。(这导致我的单元测试失败)。

My question now is: How can I say to msbuild that certain of the 3rd party references have to be copied local when building my .NET2.0 version of my project ? Or, is there any other way to achieve this, since, I wouldn t like to specify every dependency once again in my build-script. This would quickly become a maintenance nightmare, I guess.

最佳回答

I ve been able to solve this problem by making sure that I do not reference assemblies from the GAC. Instead, I ve created a lib directory in my project that contains the 3rd party assemblies. In my solution, I reference the 3rd party assemblies from there, and set copy local==True.

Next to that, you must also make sure that in your csproj file, the referenced assemblies have a Private tag whose value is set to true. Like this:

<Reference Include="...">
   <SpecificVersion>False</SpecificVersion>
   <HintPath>...</HintPath>
   <Private>True</Private>
</Reference>
问题回答

I ve been revisiting this problem again, since I do not like to have to manually change the csproj file. (When I change my reference, I must not forget to adapt the csproj file again, to set the Private node to true again).

所以,我一直在深入研究MSDN,并偶然发现了这个:

ResolveAssemblyReference.TargetFrameworkDirectories Property

Remarks This property is required to determine the CopyLocal status for resulting items.

If this property is not specified, no resulting items will be have a CopyLocal value of true unless they explicitly have a Private metadata value of true on their source item.

So, this means that there is yet another possibility, and that is to set the TargetFrameworkDirectories of the ResolveAssemblyReference task. However, is there anybody out there who knows how to do this ?
I ve been trying different things, but nothing seems to be working ...

我试过了这个:

<ItemGroup>
    <TargetFrameworkDir Include="$(SystemRoot)Microsoft.NETFrameworkv2.0.50727" />
</ItemGroup>

<PropertyGroup>
   <TargetDirsToUse>@(TargetFrameworkDir)</TargetDirsToUse>
</PropertyGroup>

<ResolveAssemblyReference TargetFrameworkDirectories="$(TargetDirsToUse)" />

But to no avail ... Maybe someone else knows how to do this, or has a golden tip. (I ve been spending way to much time on this f*cking issue).

一个问题:如果您尝试在VisualStudio中编译您的2.0解决方案/项目,会发生什么?第三方引用会自动复制吗?

这很奇怪,这能在3.5上运行,但却不能在2.0上运行。我自己没有做过并行构建,但当我将我的项目从2.0转换为3.5时,所有第三方引用都被复制,无论.NET版本如何。

顺便说一下:我从不从GAC引用第三方库(仅限Microsoft的库,甚至并非所有)。我总是将它们复制到我的lib目录结构中,将它们添加到源代码控制中并从那里引用它们。在我的看法中,使用来自GAC的程序集是不好的做法,因为它代表了开发机器设置上的不必要依赖关系。

这样一个目录的例子:http://code.google.com/p/projectpilot/source/browse/#svn/trunk/lib





相关问题
热门标签