Automate the creation of a NuGet package as part of the build process

I have an automatic build process that I would like to expand so that I can create libraries that I distribute through NuGet. Running nuget.exe to create packages is currently a manual operation.

What is the best way to install VS 2010 so that the NuGet file (* .nupkg) is the final result of the Release build?

Keep in mind that I have other files (content and tools) for some packages. And, in most cases, I have several projects combined into one NuGet package to support .NET 4, Silveright and Phone 7.

(I must clarify that the existing β€œautomated” process is a simple run-file runner that creates a solution using the command line.)

UPDATE

I want to update this discussion because the problem has not been resolved. Although the @pravin link is supplied, it is useful, it does not take into account the fact that I have several projects in one package, as well as other content, such as PowerShell scripts, configurations and source code conversions, etc.

The best example I can use is a build with .NET version 4 and Silverlight 5. They are distributed in one package. I cannot use the post-build event to create a package because the package depends on two projects.

+62
visual-studio-2010 nuget msbuild nuget-package
Feb 07 '12 at 17:30
source share
9 answers

One thing that may work well is to create a custom MSBuild.proj file. You can define a couple of goals in a custom script, the first to compile on your solution. The second goal for the next compilation will use the EXEC MSBuild task to invoke the nuget.exe command-line utility. Then you update your runner batch file to execute the msbuild executable, supplying your project file as an argument. You may already be using MSBuild in your script package, which in this case will be just a replacement for the arguments. You can include your own proj file in the solution elements of your solution. If you have done this, you can easily add a link to an external tool in Visual Studio to quickly check your custom script to make sure that it creates and creates the package as you hope.

MSBuild example

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 would call it this way:

 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" Build.proj /p:SolutionFile=PathToSolution\App.sln;NuspecFile=foo.nuspec 
+31
Mar 02 '12 at 15:35
source share

I am doing what you want to achieve already in my current project:

Each assembly is built into its own nuget package with dependencies in place.

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

There I make all the folders and immutable files there needed for the structure of the Nuget package.

I added a post-assembly to the project that copies the files that were just built into the package folder and run nuget.exe

So he goes:

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

Nuget.exe must be either in a fixed folder on your system, or in the buildserver (dirty solution), or included in your assembly (less dirty).

Buildscript:

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

To use this, the only thing you need to take care of is to decide where to check the nuget.exe file. I created the buildscripts folder at the top level of my development tree.

+10
Mar 07 2018-12-12T00:
source share

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

+7
Jul 17 '12 at 19:37
source share

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

+7
Nov 14 '12 at 18:25
source share

Install the NuGet Powertools package in your sln and it will add the build target to create nupkg and then just change your CI to run this task. http://nuget.org/packages/NuGetPowerTools

+4
Mar 05 2018-12-12T00:
source share

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

+1
Mar 21 '14 at 9:29
source share

A simple sentence that might work quite well ... just put it as a Postbuild event in a .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 (which should be named as a .csproj file) and create .nupkg.

Here it is.

You can even do this simply in your Visual Studio project settings.

+1
Jan 31 '15 at 15:40
source share

As far as I know, you cannot.

Instead, do it right and create the proper build environment / process that starts the build script when committing / clicking into your main repository, which does the following:

  • Clone / draft changes.
  • Build a solution.
  • Build package (s).
  • Uploading packages (packages) to the package server.

You can run TeamCity, CruiseControl.NET or another CI server in a virtual machine or on an existing build server.

0
Mar 01 '12 at 21:59
source share

Install the nuGet.for.MSBuild 'nuget package. A ".nuspec" file is not required, and the necessary information will be taken from AssemblyInfo.cs.

Set the build mode to vacation mode. Once created, the nupkg file will be located in the "bin / Release" folder.

https://nuget4msbuild.codeplex.com/

0
Jan 12 '17 at 18:56 on
source share



All Articles