This answer is a slight modification to Brent Arias's answer. His PostBuildMacro worked fine for me before updating the version of Nuget.exe.
In recent releases, Nuget trims the non-essential parts of the package version number to get a semantic version like "1.2.3". For example, build version "1.2.3.0" is formatted by Nuget.exe "1.2.3". And "1.2.3.1" is formatted with "1.2.3.1" as expected.
Since I need to output the exact package file name generated by Nuget.exe, now I use this adapted macro (tested in VS2015):
<Target Name="PostBuildMacros"> <GetAssemblyIdentity AssemblyFiles="$(TargetPath)"> <Output TaskParameter="Assemblies" ItemName="Targets" /> </GetAssemblyIdentity> <ItemGroup> <VersionNumber Include="$([System.Text.RegularExpressions.Regex]::Replace("%(Targets.Version)", "^(.+?)(\.0+)$", "$1"))" /> </ItemGroup> </Target> <PropertyGroup> <PostBuildEventDependsOn> $(PostBuildEventDependsOn); PostBuildMacros; </PostBuildEventDependsOn> <PostBuildEvent>echo HELLO, THE ASSEMBLY VERSION IS: @(VersionNumber)</PostBuildEvent> </PropertyGroup>
UPDATE 2017-05-24: I fixed the regex like this: "1.2.0.0" will be translated to "1.2.0" and not "1.2" as previously encoded.
And to respond to Ehryk Apr's comment, you can adapt the regex to keep only part of the version number. As an example, to save "Major.Minor", replace:
<VersionNumber Include="$([System.Text.RegularExpressions.Regex]::Replace("%(Targets.Version)", "^(.+?)(\.0+)$", "$1"))" />
By
<VersionNumber Include="$([System.Text.RegularExpressions.Regex]::Replace("%(Targets.Version)", "^([^\.]+)\.([^\.]+)(.*)$", "$1.$2"))" />
Eric Boumendil May 4 '17 at 1:42 pm 2017-05-04 13:42
source share