Automating creating NuGet package as part of build process

29,245

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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

Share:
29,245
SonOfPirate
Author by

SonOfPirate

Updated on January 13, 2020

Comments

  • SonOfPirate
    SonOfPirate over 4 years

    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.

  • SonOfPirate
    SonOfPirate about 12 years
    Version 1.6 adds a solution-level option that allows you to add the build target to each project so you don't have to commit the packages to source control. While the build target file is used by all projects, it is still executed as part of the build process for each individual project. That doesn't help with my situation where I need to generate a single package containing the build output from several projects.
  • SonOfPirate
    SonOfPirate about 12 years
    Can you provide me with some references to see how this can be done?
  • Schwarzie2478
    Schwarzie2478 about 12 years
    I believe you can copythe output from other projects also in the post build step, if you know they are already built...
  • SonOfPirate
    SonOfPirate about 12 years
    Your comment is the key - "if you know they are already built".
  • Schwarzie2478
    Schwarzie2478 about 12 years
    i assume there is a certain order to builds that get launched. Otherwise you could configure empty projects that always get build as a final step
  • Josh Rack
    Josh Rack about 12 years
    Note: the above sample has not been fully tested, but it's conceptually how you might go about it. The MSBuild reference on MSDN should help you flesh it out more fully.
  • Alex
    Alex almost 12 years
    Although, it's not possible to generate one nuget package per project with this pattern. Has anyone ever done that at TFS 2010 level ?
  • H A
    H A over 11 years
    NuGetter should also work for TFS 2012, but you will need to upgrade the assemblies to TFS 2012.
  • H A
    H A over 11 years
    I recommend using NuGetter. It is much simpler to use and configure :)
  • Chris McGrath
    Chris McGrath about 11 years
    You most certainly can I have built several libraries for work that creates a nuget package as part of the build process and even submits it to our company internal nuget feed as part of our release build process using msbuild scripts and the nuget command line tool although i am happy to see there is a library that helps with this now i am going to have to experiment with it during our next experimental build phase
  • Prisoner ZERO
    Prisoner ZERO over 9 years
    I thought this plugin was for IDE builds in Visual Studio, not MSBuilds in TFS. Am I wrong?
  • habakuk
    habakuk over 9 years
    @PrisonerZERO - It works for IDE builds and I didn't tried it yet for MSBuilds.
  • RJB
    RJB almost 8 years
    NuGetter was also recommended in passing by Microsoft MVP Marcel de Vries in an unrelated video. Curious about its merits vs. NuBuild.
  • RJB
    RJB almost 8 years
    Curious about its merits vs NuGetter