63
votes

I have an automated build process that I'd like to extend so I can build the libraries I am distributing via NuGet. Currently, running nuget.exe to create the packages is a manual operation.

What is the best way to setup VS 2010 so that my NuGet package (*.nupkg) file is the end result of a "Release" build?

Keep in mind that I have other files (content and tools) for some of the packages. And, in most cases, I have multiple projects merged into a single NuGet package to support .NET 4, Silveright and Phone 7.

(I should clarify that the existing "automated" process is a simple batch-file runner that builds a solution using the command line.)

UPDATE

I want to refresh this discussion because the issue has not been resolved. While the link @pravin supplied is helpful, it doesn't address the fact that I have multiple projects in a single package as well as other contents like PowerShell scripts, configuration and source code transformations, etc.

The best example I can use is an assembly that has both a .NET 4 and Silverlight 5 version. These are distributed in the same package. I cannot use a post-build event to create the package because the package is dependent upon TWO projects.

9
That doesn't have anything to do with my question. That link describes a workflow for USING nuget packages. My question has to do with CREATING nuget packages.SonOfPirate
Also auto-incrementing the version in release builds would be nice.Ufuk Hacıoğulları

9 Answers

32
votes

One thing that might work well is to create a custom MSBuild .proj file. You could define a couple targets in the custom script, the first to execute the compile on your solution. A second target to execute following compilation would use the EXEC MSBuild task to invoke the nuget.exe command line utility. Then, you update your batch file-runner to execute the msbuild executable supplying your custom project file as an argument. You might already be using MSBuild in your batch script, which in that case it would simply be a matter of argument swapping. You could include your custom proj file in the solution items of your solution. If you did that you could easily add an external tool reference in Visual Studio to quickly test out your custom script to make sure it was building and producing the package like you hope.

Sample MSBuild

You can use this as a starting place:

<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
    <PropertyGroup>
      <SolutionFile></SolutionFile>
      <NugetExecutable>C:\PathToNuget\nuget.exe</NugetExecutable>
      <NuspecFile></NuspecFile>
    </PropertyGroup>

    <Target Name = "Compile">
        <MSBuild Projects="$(SolutionFile)" Properties="Configuration=Release" />
    </Target>

    <Target Name = "Package">
    <!-- You could use the MSBuild Copy task here to move the compiled code into
           a structure that fits your desired package format -->
      <Exec Command="&quot;$(NugetExecutable)&quot; pack $(NuspecFile)" />
    </Target>
</Project>

You'd then call this like:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" Build.proj /p:SolutionFile=PathToSolution\App.sln;NuspecFile=foo.nuspec
10
votes

I'm doing the thing you want to achieve already in my current project:

Every assembly is built into its own proper nuget package, with dependencies in place.

I solved it by making a package folder in the project for which I wanted to make a nuget package. There I configure a nuspec file with the necessary information about the nupkg

There I make all the folders and unchanging files in there needed for Nuget package structure.

I added a post build step in the project that copies the files that just have been built into the package folder and run nuget.exe

So it goes:

  • Build Project.
  • Copy output back into Package\Lib of project.
  • Run nuget.exe with nuspec file in package folder.
  • Copy result to output folder next to the rest of the output.

Nuget.exe has to be either in a fixed folder on your system and the buildserver (dirty solution) or included in your build (less dirty).

Buildscript:

Xcopy.exe /Y "$(TargetPath)" "$(ProjectDir)\Package\Lib" 
cd "$(ProjectDir)Package" 
"$(TargetDir)..\Buildscripts\Nuget.exe" pack MyPackage.nuspec xcopy /Y *.nupkg "$(TargetDir)" 

In order to use this, the only thing you have to take care of, is deciding where to checkin the nuget.exe. I made a buildscripts folder on the top level of my development tree.

7
votes

If you're in a TFS 2010 environment, the NuGetter project should solve the problem of creating nuget packages automatically. It creates one package for the entire build. It is in fact a TFS 2010 build workflow that does the job by calling nuget.exe with some arguments.

7
votes

I created a NuGet project type (.nuproj) Visual Studio extension called NuBuild that should do what you want. It allows you to build your NuGet packages from Visual Studio as well as MSBuild. You can install it from the gallery or get the source at github.

4
votes

Install the NuGet Powertools package in your sln and it will add a build target for creating the nupkg then just modify your CI to run that task as well. http://nuget.org/packages/NuGetPowerTools

1
votes

There is the Nuget package CreateNewNuGetPackageFromProjectAfterEachBuild which claims that it can do what you want. There is also a documentation/project site.

1
votes

A simple suggestion that might work good enough... just put it as Postbuild event into the .csproj file:

  <PropertyGroup>
    <PostBuildEvent>$(SolutionDir)<YourPathToNugetHere>\NuGet.exe pack $(ProjectPath) -OutputDirectory ..\..\$(OutDir) -IncludeReferencedProjects -Symbols -Properties Configuration=$(Configuration)</PostBuildEvent>
  </PropertyGroup>

This will collect your custom .nuspec file (that needs to be named like the .csproj file) and build the .nupkg.

Thats it.

You can even do this simply within the Visual Studio Project settings.

0
votes

As far as I'm aware, you can't.

Instead, do it properly and have a proper build environment/process that fires a build script on commit/push to your main repository that does the following:

  • Clone/pull changes.
  • Build solution.
  • Build package(s).
  • Upload package(s) to package server.

You could run TeamCity, CruiseControl.NET or some other CI server on a VM or on your existing build server.

0
votes

Install the 'NuGet.for.MSBuild' nuget package. No '.nuspec' file is required and the required information will be taken from the AssemblyInfo.cs.

Set the build to 'Release' mode. Once built the nupkg file will be in the 'bin/Release' folder.

https://nuget4msbuild.codeplex.com/