2

I've modified a csproj file in VS2017 to create a Nuget package when my .NET 4.5 project is release-built. The next automation step is to add this package to my private feed on a network share. Here are the commands I'm using:

<Exec Command="nuget.exe pack -Properties &quot;Configuration=Release&quot; -Symbols $(ProjectName).csproj" />
<Exec Command="nuget.exe add $(ProjectName).$(ProductVersion).nupkg -source \\MYSERVER\Nuget packages" />

Line 1 works and produces a nupkg file of the form productname.nn.nn.nn.nn.

However line 2 is not returning a value for the ProductVersion token (which was a guess on my part).

I've struggled to find a reference for MSBUILD tokens (that in itself would be useful to know), but what I really need to know is the correct MSBUILD token/variable/property for the the version - and that is the same value as the generated Nuget package.

CrispinH
  • 1,899
  • 4
  • 23
  • 38
  • Is this a "new world" SDK-based csproj with integrated msbuild nuget support (begins with`` ) or a classic csproj+nuspec? For the first one, there's `$(PackageVersion)` – Martin Ullrich May 09 '17 at 19:44
  • Not it's the old-fashioned type. I gave it a go anyway, but no version number returned. – CrispinH May 09 '17 at 20:16

2 Answers2

2

I explored $(PackageVersion) suggested by Martin Ullrich, but it's not going to work with older projects even with Nuget 4.10 installed. Also I couldn't get Troopers sample to work how I wanted and I ended up with a variation on one of the posts here. I adapted it to give me the assembly version as opposed to the file version.

  <UsingTask TaskName="GetVersionParts" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <AssemblyPath ParameterType="System.String" Required="true" />
      <MajorVersion ParameterType="System.Int32" Output="true" />
      <MinorVersion ParameterType="System.Int32" Output="true" />
      <BuildVersion ParameterType="System.Int32" Output="true" />
    </ParameterGroup>
    <Task>
      <Using Namespace="System.Reflection" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
        Version v = AssemblyName.GetAssemblyName(this.AssemblyPath).Version;

        this.MajorVersion = v.Major;
        this.MinorVersion = v.Minor;
        this.BuildVersion = v.Build;    
      ]]>
      </Code>
    </Task>
  </UsingTask>
  <Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
    <Message Text="**** After-build process starting ****" />
    <Exec Command="nuget.exe pack -Properties &quot;Configuration=Release&quot; -Symbols $(ProjectName).csproj" />
    <GetVersionParts AssemblyPath="$(OutputPath)$(AssemblyName).dll">
      <Output TaskParameter="MajorVersion" PropertyName="MajorVersionNumber" />
      <Output TaskParameter="MinorVersion" PropertyName="MinorVersionNumber" />
      <Output TaskParameter="BuildVersion" PropertyName="BuildVersionNumber" />
    </GetVersionParts>
    <Exec Command="nuget.exe add $(MSBuildProjectName).$(MajorVersionNumber).$(MinorVersionNumber).$(BuildVersionNumber).nupkg -source  &quot;\\My feed server\Shared location&quot;" />
    <Exec Command="move *.symbols.nupkg &quot;\\My feed server\Shared location\Symbols&quot;" />
    <Message Text="**** After-build process completed ****" />
  </Target>

Although this works, I can't help feeling that it should be easier.

Further reading

https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-inline-tasks https://learn.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties https://learn.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference#add

Community
  • 1
  • 1
CrispinH
  • 1,899
  • 4
  • 23
  • 38
  • We could use [`GetAssemblyIdentity`](https://learn.microsoft.com/en-us/visualstudio/msbuild/getassemblyidentity-task?view=vs-2015) built-in task if it was possible to change the generated `.nupkg` file name to include the revision number too. – crush Jan 08 '19 at 17:29
0

You could get the assembly version with this macro. I call it before the postbuild event for example

<!--Macro to get the version -->
<Target Name="PostBuildMacros">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
        <Output TaskParameter="Assemblies" ItemName="CurrentAssembly" />
    </GetAssemblyIdentity>
    <ItemGroup>
        <VersionNumber Include="%(CurrentAssembly.Version)" />
    </ItemGroup>
</Target>
<!-- override PostBuildEvent to call PostBuildMacros -->
<PropertyGroup>
    <PostBuildEventDependsOn>
        $(PostBuildEventDependsOn);
        PostBuildMacros;
    </PostBuildEventDependsOn>
    <PostBuildEvent>
        ...
    </PostBuildEvent>
</PropertyGroup>
Troopers
  • 5,127
  • 1
  • 37
  • 64