Try the following:
1) Rename you .nuspec to match the .csproj name:
If your project is: MyLib.csproj
Rename MyLib.nuspec to MyLib.csproj.nuspec
2) Modify your .nuspec file as follows:
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<version>$version$</version>
<authors>$author$</authors>
<id>$id$</id>
<title>$id$</title>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<summary>$description$</summary>
</metadata>
</package>
Here, the job is done by Nuget's "$version$" token (and others)
Take a look to this Nuget article for more tokens and information.
3) Use the following Post Build Event in every project you put a .csproj.nuspec file (please remove the Enter keys):
mkdir "$(SolutionDir)GeneratedPackages"
"$(SolutionDir).nuget\nuget.exe" pack "$(ProjectPath)"
-OutputDirectory "$(SolutionDir)GeneratedPackages"
-basepath "$(SolutionDir)$(ProjectName)\$(OutDir)"
If we have a solution C:\MySolution\MyLib.sln and the project at c:\MySolution\MyLib\MyLib.csproj, our .nuspec file should be c:\MySolution\MyLib\MyLib.csproj.nuspec and the Post Build Event Command would look like:
mkdir "C:\MySolution\GeneratedPackages"
"C:\MySolution\.nuget\nuget.exe" pack "C:\MySolution\MyLib\MyLib.csproj"
-OutputDirectory "C:\MySolution\GeneratedPackages"
-basepath "C:\MySolution\MyLib\bin\debug\"
Fix the path to the nuget.exe. In my case I am using the Nuget "auto-regenerate missing dependencies on build", and I have a local copy in the solution, under the .nuget folder.
Please note that we're not calling nuget with the .nuspec file, we're
calling it with the .csproj directly!!
This strategy will also include all the Project dependendencies declared in the
packages.xml that Nuget creates when you install packages.
(automatically, every time you build)
And you're done! Now change the Assembly and File Version in
Project > Properties > Assembly information
IMPORTANT: If you set the $author$ and/or $description$ tokens, fill also the Assembly's Company and Description fields here. If you have troubles, remove the $author$ and/or $description$ tokens and try to compile again.
and build your Project, you'll see in /GeneratedPackages folder the MyLib.X.X.X.X.nupack with the version you set in the Project Properties. If you set dependencies, you'll see that it has been automatically included as Package Dependencies.
We are using this strategy with a Jenkins CI server to build automaticaly the solution and copy the packages from the GeneratedPackages to our custom NugetServer.
You can also complete the Post Build Event with a
nuget push
To send the generated package to a Nuget Server
ISSUE IN RELEASE:
As mentioned in comments, there is an issue that don't allow Nuget.exe to generate the packages in RELEASE mode.
Do this in order to fix it:
For every project with this script:
- Project Properties > Build
- In the "Configuration" dropdown choose "All configurations"
- In the "Ouput" section, change the "Output path:" field, for example, I am currently using "bin\" (without cuotes)