1 minute read

So you’ve got a super awesome library/assembly that you want to share with others, but you’re too lazy to actually use NuGet to package it up and upload it to the gallery; or maybe you don’t know how to create a NuGet package and don’t have the time or desire to learn. Well, my friends, now this can all be handled for you automatically.

A couple weeks ago I posted about a new PowerShell script that I wrote and put up on CodePlex, called New-NuGetPackage PowerShell Script, to make creating new NuGet packages quick and easy. Well, I’ve taken that script one step further and use it in a new NuGet package called Create New NuGet Package From Project After Each Build (real creative name, right) that you can add to your Visual Studio projects. The NuGet package will, you guessed it, pack your project and its dependencies up into a NuGet package (i.e. .nupkg file) and place it in your project’s output directory beside the generated dll/exe file. Now creating your own NuGet package is as easy as adding a NuGet package to your project, which if you’ve never done before is dirt simple.

I show how to add the NuGet package to your Visual Studio project in the New-NuGetPackage PowerShell Script documentation (hint: search for “New NuGet Package” (include quotes) to find it in the VS NuGet Package Manager search results), as well as how you can push your package to the NuGet Gallery in just a few clicks.

Here’s a couple screenshots from the documentation on installing the NuGet Package:

Navigate To Manage NuGet Packages Install NuGet Package From Package Manager

Here you can see the new PostBuildScripts folder it adds to your project, and that when you build your project, a new .nupkg file is created in the project’s Output directory alongside the dll/exe.

Files Added To Project NuGet Package In Output Directory"

So now that packaging your project up in a NuGet package can be fully automated with about 30 seconds of effort, and you can push it to the NuGet Gallery in a few clicks, there is no reason for you to not share all of the awesome libraries you write.

Happy coding!

Comments

John

Hi, interesting project - i seem to be getting a error when i build like so:

The command “REM Create a NuGet package for this project and place the .nupkg file in the project’s output directory. ECHO Building NuGet package in Post-Build event… PowerShell -NoProfile -ExecutionPolicy Bypass -Command “& ‘C:\SVN\Chevron\Chevron RTRM\ISS.WebAPI.PubSub\ISS.WebAPI.PubSub\trunk\ISS.WebAPI.PubSub.Attributes\PostBuildScripts\New-NuGetPackage.ps1’ -ProjectFilePath ‘C:\SVN\Chevron\Chevron RTRM\ISS.WebAPI.PubSub\ISS.WebAPI.PubSub\trunk\ISS.WebAPI.PubSub.Attributes\ISS.WebAPI.PubSub.Attributes.csproj’ -PackOptions ‘-OutputDirectory ““C:\SVN\Chevron\Chevron RTRM\ISS.WebAPI.PubSub\ISS.WebAPI.PubSub\trunk\ISS.WebAPI.PubSub.Attributes\bin\Debug"” -NonInteractive’ -DoNotUpdateNuSpecFile -NoPrompt”” exited with code 1.

I have powershell 3.0 installed - maybe that is the problem? not an expert at ps by any stretch of the imagination…

deadlydog

Hi John, PowerShell v3.0 is required, so that shouldn’t be the problem. This “exited with code 1” is a generic “running the batch script failed” error message. The bottom of this page (https://newnugetpackage.codeplex.com/wikipage?title=NuGet%20Package%20To%20Create%20A%20NuGet%20Package%20From%20Your%20Project%20After%20Every%20Build) shows how to find the real error message. Can you post that error message please. Also, it might make more sense for you to open an Issue on the CodePlex page (https://newnugetpackage.codeplex.com/workitem/list/basic) rather than continuing in these comments. Thanks.

Hasibul Haque

Sir, Thanks for wonderful work. I need your help very much. In your BuildNewPackage-RanAutomatically.ps1 file there is an option to provide additional parameter.

I have provided -IncludeReferencedProjects as parameter and it works.

Now i want to generate Symbol package so i need to pass -Symbols.

My Question is how i can send two parameter or how i can get Symbol package also.

Hope you will help me.

Thanks in advance.

Tom

Hi I was just wondering how you handle transforms like $rootnamespace$ is there an automated way to generate the pp files with namespaces swapped out? Cheers, Tom

deadlydog

@Tom Hey Tom, I actually have never looked at these transforms before, but found the docs at http://docs.nuget.org/docs/creating-packages/configuration-file-and-source-code-transformations.

My extension simply calls the NuGet.exe with whatever additional parameters you specify in the BuildNewPackage-RanAutomatically.ps1 file. So transforms will work as they always have; I don’t provide any additional functionality related to them, yet ;).

Grzegorz Mrozik

Hi. Thank you for great tool. However, there is a problem with non-english Windows. Constants defined in line 290 of New-NuGetPackage.ps1 are valid only on english Windows. On localized Windows (polish version in my case) NuGet.exe output is also localized and pattern matching (e.g. line 1159) returns false.

deadlydog

@Grzegorz Mrozik Yup, you are right. I’m not sure how to do localization in PowerShell, which is why I grouped all of those console messages together in the script; to make it easy for you to change them to their equivalent strings for your language. Basically you will need to perform the pack and push operations against NuGet.exe manually to see what strings the console spits out, and then change the script to use those strings.

harre

Awesome plugin. A little gotcha is that you need to change the packProperties to this $packProperties = “VisualStudioVersion=12.0”

If you don’t do that msbuild won’t find msbuild targets for vs2013 project types, for example MVC project.

Joel Damon Elkhorne

Fantastic addition - makes developing much more productive :)

If I want to publish to a private NuGet server for use in-house only via post-build event say for a new purpose-built build config [Pack], can I add a .nuspec in the project directory (to give me author and perhaps other bits and pieces and do: PowerShell -NoProfile -ExecutionPolicy Bypass -Command “& ‘$(ProjectDir)_CreateNewNuGetPackage\DoNotModify\UploadNuGetPackage.ps1’” after the creation Powershell line?

The reason I ask is that I don’t want a new package after every build but if I do want a package created I also want it published automatically so that any assemblies that rely on it will see updates in their NuGet management UI

waqas

How can we automate the transformations as well..?? what I mean is that I want to create the transformation files automatically when I build the project..I want to read from the local app.config or web.config and copy the settings into a new xdt file..

Cli

Thank you sharing this great tool with us, sir.

I tried to upload created nuget package to a local gallery with $sourceToUploadTo = “local-gallery-address” in config.ps1. But it’s not working nor any errors reported. Am I misunderstanding something?

deadlydog

@Cli You will need to manually run the “RunMeToUploadNugetPackage.cmd” file to actually upload the new version to your NuGet server. This is not ran as part of the build, as it would potentially result in sooooooo many packages being uploaded to the server, as well as increasing build times.

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...