問題描述
我有一個大型 c# 解決方案文件(約 100 個項目),我正在努力縮短構建時間.我認為復制本地"在很多情況下對我們來說是一種浪費,但我想知道最佳實踐.
I have a large c# solution file (~100 projects), and I am trying to improve build times. I think that "Copy Local" is wasteful in many cases for us, but I am wondering about best practices.
在我們的 .sln 中,應用程序 A 依賴于程序集 B,而程序集 B 又依賴于程序集 C.在我們的例子中,有幾十個B"和少數C".由于這些都包含在 .sln 中,因此我們使用的是項目引用.當前所有程序集都構建到 $(SolutionDir)/Debug(或 Release)中.
In our .sln, we have application A depending on assembly B which depends on assembly C. In our case, there are dozens of "B" and a handful of "C". Since these are all included in the .sln, we're using project references. All assemblies currently build into $(SolutionDir)/Debug (or Release).
默認情況下,Visual Studio 將這些項目引用標記為復制本地",這會導致每個C"被復制到 $(SolutionDir)/Debug 中,對于每個構建的B".這似乎很浪費.如果我只關閉復制本地"會出現什么問題?其他擁有大型系統的人是做什么的?
By default, Visual Studio marks these project references as "Copy Local", which results in every "C" being copied into $(SolutionDir)/Debug once for every "B" that builds. This seems wasteful. What can go wrong if I just turn "Copy Local" off? What do other people with large systems do?
跟進:
很多回復建議將構建分解為更小的 .sln 文件...在上面的示例中,我將首先構建基礎類C",然后是大部分模塊B",然后是一些應用程序,A".在這個模型中,我需要從 B 獲得對 C 的非項目引用.我遇到的問題是調試"或發布"被納入提示路徑,我最終構建了B"的發布版本針對C"的調試版本.
Lots of responses suggest breaking up the build into smaller .sln files... In the example above, I would build the foundation classes "C" first, followed by the bulk of the modules "B", and then a few applications, "A". In this model, I need to have non-project references to C from B. The problem I run into there is that "Debug" or "Release" gets baked into the hint path and I wind up building my Release builds of "B" against debug builds of "C".
對于那些將構建拆分為多個 .sln 文件的人,您如何處理這個問題?
For those of you that split the build up into multiple .sln files, how do you manage this problem?
推薦答案
在之前的項目中,我使用了一個帶有項目引用的大型解決方案,但也遇到了性能問題.解決方案是三倍:
In a previous project I worked with one big solution with project references and bumped into a performance problem as well. The solution was three fold:
始終將 Copy Local 屬性設置為 false 并通過自定義 msbuild 步驟強制執行此操作
Always set the Copy Local property to false and enforce this via a custom msbuild step
將每個項目的輸出目錄設置為同一個目錄(最好相對于$(SolutionDir)
Set the output directory for each project to the same directory (preferably relative to $(SolutionDir)
框架附帶的默認 cs 目標計算要復制到當前正在構建的項目的輸出目錄的引用集.由于這需要在參考"關系下計算傳遞閉包,這可能會變得非常代價高昂.我的解決方法是在導入 Microsoft 后在每個項目中導入的通用目標文件(例如
.導致每個項目文件如下所示:Common.targets
)中重新定義 GetCopyToOutputDirectoryItems
目標.CSharp.targets
The default cs targets that get shipped with the framework calculate the set of references to be copied to the output directory of the project currently being built. Since this requires calculating a transitive closure under the 'References' relation this can become VERY costly. My workaround for this was to redefine the GetCopyToOutputDirectoryItems
target in a common targets file (eg. Common.targets
) that's imported in every project after the import of the Microsoft.CSharp.targets
. Resulting in every project file to look like the following:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
... snip ...
</ItemGroup>
<Import Project="$(MSBuildBinPath)Microsoft.CSharp.targets" />
<Import Project="[relative path to Common.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>
這將我們在給定時間的構建時間從幾個小時(主要是由于內存限制)減少到了幾分鐘.
This reduced our build time at a given time from a couple of hours (mostly due to memory constraints), to a couple of minutes.
可以通過從 C:WINDOWSMicrosoft.NETFrameworkv2.0.50727Microsoft 復制第 2,438–2,450 和 2,474–2,524 行來創建重新定義的
變成 GetCopyToOutputDirectoryItems
.Common.targetsCommon.targets
.
The redefined GetCopyToOutputDirectoryItems
can be created by copying the lines 2,438–2,450 and 2,474–2,524 from C:WINDOWSMicrosoft.NETFrameworkv2.0.50727Microsoft.Common.targets
into Common.targets
.
為了完整起見,生成的目標定義變為:
For completeness the resulting target definition then becomes:
<!-- This is a modified version of the Microsoft.Common.targets
version of this target it does not include transitively
referenced projects. Since this leads to enormous memory
consumption and is not needed since we use the single
output directory strategy.
============================================================
GetCopyToOutputDirectoryItems
Get all project items that may need to be transferred to the
output directory.
============================================================ -->
<Target
Name="GetCopyToOutputDirectoryItems"
Outputs="@(AllItemsFullPathWithTargetPath)"
DependsOnTargets="AssignTargetPaths;_SplitProjectReferencesByFileExistence">
<!-- Get items from this project last so that they will be copied last. -->
<CreateItem
Include="@(ContentWithTargetPath->'%(FullPath)')"
Condition="'%(ContentWithTargetPath.CopyToOutputDirectory)'=='Always' or '%(ContentWithTargetPath.CopyToOutputDirectory)'=='PreserveNewest'"
>
<Output TaskParameter="Include" ItemName="AllItemsFullPathWithTargetPath"/>
<Output TaskParameter="Include" ItemName="_SourceItemsToCopyToOutputDirectoryAlways"
Condition="'%(ContentWithTargetPath.CopyToOutputDirectory)'=='Always'"/>
<Output TaskParameter="Include" ItemName="_SourceItemsToCopyToOutputDirectory"
Condition="'%(ContentWithTargetPath.CopyToOutputDirectory)'=='PreserveNewest'"/>
</CreateItem>
<CreateItem
Include="@(_EmbeddedResourceWithTargetPath->'%(FullPath)')"
Condition="'%(_EmbeddedResourceWithTargetPath.CopyToOutputDirectory)'=='Always' or '%(_EmbeddedResourceWithTargetPath.CopyToOutputDirectory)'=='PreserveNewest'"
>
<Output TaskParameter="Include" ItemName="AllItemsFullPathWithTargetPath"/>
<Output TaskParameter="Include" ItemName="_SourceItemsToCopyToOutputDirectoryAlways"
Condition="'%(_EmbeddedResourceWithTargetPath.CopyToOutputDirectory)'=='Always'"/>
<Output TaskParameter="Include" ItemName="_SourceItemsToCopyToOutputDirectory"
Condition="'%(_EmbeddedResourceWithTargetPath.CopyToOutputDirectory)'=='PreserveNewest'"/>
</CreateItem>
<CreateItem
Include="@(Compile->'%(FullPath)')"
Condition="'%(Compile.CopyToOutputDirectory)'=='Always' or '%(Compile.CopyToOutputDirectory)'=='PreserveNewest'">
<Output TaskParameter="Include" ItemName="_CompileItemsToCopy"/>
</CreateItem>
<AssignTargetPath Files="@(_CompileItemsToCopy)" RootFolder="$(MSBuildProjectDirectory)">
<Output TaskParameter="AssignedFiles" ItemName="_CompileItemsToCopyWithTargetPath" />
</AssignTargetPath>
<CreateItem Include="@(_CompileItemsToCopyWithTargetPath)">
<Output TaskParameter="Include" ItemName="AllItemsFullPathWithTargetPath"/>
<Output TaskParameter="Include" ItemName="_SourceItemsToCopyToOutputDirectoryAlways"
Condition="'%(_CompileItemsToCopyWithTargetPath.CopyToOutputDirectory)'=='Always'"/>
<Output TaskParameter="Include" ItemName="_SourceItemsToCopyToOutputDirectory"
Condition="'%(_CompileItemsToCopyWithTargetPath.CopyToOutputDirectory)'=='PreserveNewest'"/>
</CreateItem>
<CreateItem
Include="@(_NoneWithTargetPath->'%(FullPath)')"
Condition="'%(_NoneWithTargetPath.CopyToOutputDirectory)'=='Always' or '%(_NoneWithTargetPath.CopyToOutputDirectory)'=='PreserveNewest'"
>
<Output TaskParameter="Include" ItemName="AllItemsFullPathWithTargetPath"/>
<Output TaskParameter="Include" ItemName="_SourceItemsToCopyToOutputDirectoryAlways"
Condition="'%(_NoneWithTargetPath.CopyToOutputDirectory)'=='Always'"/>
<Output TaskParameter="Include" ItemName="_SourceItemsToCopyToOutputDirectory"
Condition="'%(_NoneWithTargetPath.CopyToOutputDirectory)'=='PreserveNewest'"/>
</CreateItem>
</Target>
有了這個解決方法,我發現在一個解決方案中擁有多達 120 個項目是可行的,這主要的好處是項目的構建順序仍然可以由 VS 確定,而不是通過拆分手動完成提出你的解決方案.
With this workaround in place I found it workable to have as much as > 120 projects in one solution, this has the main benefit that the build order of the projects can still be determined by VS instead of doing that by hand by splitting up your solution.
這篇關于“復制本地"的最佳做法是什么?和項目參考?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!