问题描述
我有一个大型解决方案,其中包含100多个项目(C ++,托管C ++,C#),并且其中许多相互依赖.
I have a large solution with more than 100 projects (C++, Managed C++, C#) and many of them depends on each others.
我有一个TeamCity服务器,我想在那里建立该解决方案.
I have a TeamCity server and I want build this solution there.
当我在Visual Studio中构建解决方案时,一切都很好,但是使用TeamCity时出现CS0006错误.我知道为什么会这样-TeamCity使用MSBuild 4来构建解决方案,但是MSBuild 4中存在一个已知的错误-它会忽略构建顺序,并根据需要从解决方案中构建项目.由于存在此行为,因此:
When I build solution in VisualStudio everything goes fine, but with TeamCity I have a CS0006 error.I know why that so - TeamCity uses MSBuild 4 to build solutions, but there is a known bug in MSBuild 4 - it ignores build order and build projects from solutions in order it wants.Because of this behavior if you have:
Project A
Project B which has reference to A
MSBuild可以按以下顺序构建这些项目:
MSBuild can build these project in such order:
1. B
2. A
最简单的解决方案是设置 BuildProjectReferences = true (默认设置),所有引用的项目都会自动构建.但是我不能使用这种方法,因为不是该解决方案中的所有参考项目,而且我不能从其他解决方案中构建项目.
The easiest solution is to set BuildProjectReferences=true (which is default) and all referenced project will be builded automatically. But I can't use this approach because not all referenced project in this solution, and I can't build projects from another solution.
这是针对此问题的另一种解决方法-使用 ConfigurationManager 并禁用不应构建的所有项目,但仅在VisualStudio中有效-MSBuild会忽略该问题并构建所有引用的项目.
Here is another fix for this problem - use ConfigurationManager and disable all projects, which shouldn't build, but it works only in VisualStudio - MSBuild ignores that and builds all referenced projects.
问题是还原生成顺序,该顺序可以在VisualStudio的 ProjectBuildOrder 窗口中看到,如果我直接从控制台使用MSBuild,那是不正确的.
The problem is to restore build order which I can see in VisualStudio in window ProjectBuildOrder which is not true if I use MSBuild directly from Console.
推荐答案
请参见在Visual Studio博客上使用MSBuild.exe时不正确的解决方案构建顺序:
您以前可能没有这样做,因为您不想引用项目引用的目标,而只是订购构建.但是,在4.0中,您可以创建仅对构建进行排序而不添加参考的项目参考.看起来像这样–注意元数据元素,所有这些当然都在<ItemGroup>
标记内:
You may not have done that before because you didn’t want to reference the target of the project reference, but merely order the build. However, in 4.0 you can create a project reference that only orders the build without adding a reference. It would look like this – note the metadata element, and all this is inside an <ItemGroup>
tag of course:
<ProjectReference Include="foo.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
请注意,您必须使用文本编辑器添加子元素-Visual Studio可以添加项目引用,但不会公开此元数据的UI.
Note that you have to add the child element with a text editor — Visual Studio can add a project reference, but doesn’t expose UI for this metadata.
我也可以通过删除解决方案文件中的依赖项来进行整理-删除类似这样的不必要行-您的GUID将有所不同,但是使用VS对话框即可完成工作...
I can tidy up by removing the dependency in the solution file as well – removing now-unnecessary lines like this — your GUID will be different, but use the VS dialog and it will do the job...
这篇关于MSBuild构建顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!