Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a recommended approach to configuring a NuGet package targeting multiple frameworks in TeamCity using MSBuild?

I've read a handful of posts (see references below) and have yet to find a guide on best practices that is specific to my tech stack.

The goal: Create a single NuGet package targeting multiple .NET frameworks built from a single .csproj file via TeamCity using MSBuild and NuGet.

The constraints:

  1. Pull the code from the VCS only once.
  2. All compiled assemblies should be versioned the same.
  3. Single .csproj (not one per target framework).

I have two approaches in mind:

  1. Create a single build configuration. It would contain three build steps: compile .NET 3.5, compile .NET 4.0, pack with NuGet. Each build step would be contingent upon success of the last. The only real problem I see with this approach (and hopefully there's a solution that I'm not aware of) is that each build step would require its own set of build parameters (e.g., system.TargetFrameworkVersion and system.OutputPath) to designate the unique location for the DLL to sit (e.g., bin\release\v3.5 and bin\release\v4.0) so that the NuGet pack step would be able to do its thing based upon the Files section in the .nuspec file.

  2. Create multiple build configurations. One build configuration per the build steps outlined above. With this approach, it is easy to solve the TargetFrameworkVersion and OutputPath build parameters issue but I now have to create snapshot dependencies and share the assembly version number across the builds. It also eats up build configuration slots which is ok (but not optimal) for us since we do have an Enterprise license.

Option #1 seems like the obvious choice. Options #2 feels dirty.

So my two questions are:

  1. Is it possible to create parameters that are unique to a build step?
  2. Is there a third, better approach?

References:

  1. Multi-framework NuGet build with symbols for internal dependency management
  2. Nuget - packing a solution with multiple projects (targeting multiple frameworks)
  3. http://lostechies.com/joshuaflanagan/2011/06/23/tips-for-building-nuget-packages/
  4. http://msdn.microsoft.com/en-us/library/hh264223.aspx
  5. https://stackoverflow.com/a/1083362/607701
  6. http://confluence.jetbrains.com/display/TCD7/Configuring+Build+Parameters
  7. http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package
like image 496
David Peden Avatar asked Apr 04 '13 16:04

David Peden


People also ask

Can you have multiple target frameworks?

For SDK-style projects, you can configure support for multiple targets frameworks (TFM) in your project file, then use dotnet pack or msbuild /t:pack to create the package. nuget.exe CLI does not support packing SDK-style projects, so you should only use dotnet pack or msbuild /t:pack .

What is target framework in packages config?

The target framework moniker (TFM) to apply when installing the package. This is initially set to the project's target when a package is installed. As a result, different <package> elements can have different TFMs. For example, if you create a project targeting .

What is PackagePath?

PackagePath : Path where the file should be output in the package. NuGet issues a warning if more than one file is added to the same package path. BuildAction : The build action to assign to the file, required only if the package path is in the contentFiles folder. Defaults to "None".


1 Answers

Here is my preferred solution (Option #1):

The magic relies on an unfortunate workaround. If you're willing to make this compromise, this solution does work. If you are not, you can follow the issue that I opened on JetBrains' issue tracker.

The single build configuration looks like this:

enter image description here

Note the name of the first two build steps. They are, in fact, named explicitly as the TargetFrameworkVersion values for .NET 3.5 and 4.0, respectively.

Then, in the Build Parameters section, I have configured the following parameters:

enter image description here

And finally, the Nuget Pack step does the file path translation according to my .nuspec's files section:

<files>     <file src="bin\release\v3.5\*.*" target="lib\net35" />     <file src="bin\release\v4.0\*.*" target="lib\net40" /> </files> 
like image 57
David Peden Avatar answered Oct 09 '22 10:10

David Peden