问题
Short description of the current situation: We've two solutions with a bunch of general 'helper' libraries. We need all those libraries for .NET 4 and .NET 4.5 since some of our projects that depend on those libraries are running on systems that are not to be touched, meaning we can't put .NET 4.5 on those systems.
We've a working build server (TFS2012) that builds all solutions once per night. Currently we get swamped by errors since either the .NET 4 projects or the .NET 4.5 projects can't be build, depending on the current state of the checked-in .csproj files.
What I want/what would be perfect: When someone (regardless of it it's the buildserver or a Visual Studio) compiles the 'helper' solutions, a .NET 4 and a .NET 4.5 version of those files is compiled. The .NET 4 files are copied to C:\dll\a
and the .NET 4.5 version is copied to C:\dll\b
.
What I stumbled upon: http://shazwazza.com/post/multi-targeting-a-single-net-project-to-build-for-different-framework-versions/ but given the amount of .csproj files I really do not want to do that for each .csproj file.
My question being:
Is there a way to compile entire solutions for .NET 4 and .NET 4.5 and copy the compiled libraries to specific folders?
If yes, how do I do that?
If not, is there an alternative way of doing this, essentially leading to the same result (.NET 4 and .NET 4.5 libraries of the same code in two different locations so that they can be referenced by other projects)?
回答1:
Essentially, a csproj is a build target; while some things can be changed as parameters on a build, the fundamental platform etc is trickier, and it is a much better idea to simply maintain a different csproj per build target.
You correctly (comments) observe that this means you will need to maintain references in both separately - that is unavoidable, since some platforms use completely different dlls to get the same things (cf, winrt, etc).
However, is not necessarily the case that you need to maintain the files separately; here's a line from the portable class library build for protobuf-net:
<Compile Include="..\protobuf-net\**\*.cs" />
Which means: compile all .cs files (recursively) under ..\protobuf-net
. You might prefer to maintain the file list separately in smaller projects - for example, here's "dapper":
The little blus arrows indicate file links, so as you can see: the primary code is in "Dapper NET40", with "Dapper NET35" and "Dapper NET45" using the same files ("Dapper NET45" also adds an additional file).
And to emphasize why the references need to be maintained separately, here's "StackExchange.Redis" for .NET 4.5:
(nice and clean) - and here's "StackExchange.Redis" for .NET 4.0:
The extra references are from Microsoft.Bcl
, used to get the full Task
API (and other dependencies) in .NET 4.0 (it is inbuilt in .NET 4.5):
<packages>
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net40" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net40" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />
</packages>
来源:https://stackoverflow.com/questions/24822378/build-entire-solution-for-net-4-and-net-4-5-and-copy-files-to-specific-folders