ChatGPT解决这个技术问题 Extra ChatGPT

在构建过程中自动创建 NuGet 包

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.

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.
Also auto-incrementing the version in release builds would be nice.

F
Frank Bryce

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

Can you provide me with some references to see how this can be done?
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.
I recommend using NuGetter. It is much simpler to use and configure :)
s
scott-pascoe

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.


I believe you can copythe output from other projects also in the post build step, if you know they are already built...
Your comment is the key - "if you know they are already built".
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
k
kay

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.


Although, it's not possible to generate one nuget package per project with this pattern. Has anyone ever done that at TFS 2010 level ?
NuGetter should also work for TFS 2012, but you will need to upgrade the assemblies to TFS 2012.
NuGetter was also recommended in passing by Microsoft MVP Marcel de Vries in an unrelated video. Curious about its merits vs. NuBuild.
B
Brent M. Spell

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.


Curious about its merits vs NuGetter
M
Matthew M. Osborn

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


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.
h
habakuk

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


I thought this plugin was for IDE builds in Visual Studio, not MSBuilds in TFS. Am I wrong?
@PrisonerZERO - It works for IDE builds and I didn't tried it yet for MSBuilds.
B
Beachwalker

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.


N
Neil Barnwell

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.


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
C
CountZero

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/