4

The problem how to dynamically change dependency version in the nuspec file during nuget pack

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyPacked.Name</id>
    <version>1.1.1</version>
    <authors>Pawel Cioch</authors>
    <owners>Pawel Cioch</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Some Cool package</description>
    <releaseNotes>Initial Release</releaseNotes>
    <copyright>Copyright 2015</copyright>
    <tags>replace dependency version</tags>
    <dependencies>
      <dependency id="dep1" version="I want version here to be same as package version" />
      <dependency id="dep2" version="2.1.3" />
    </dependencies>
  </metadata>
</package>

Please don't ask "why do you need it"

Also im sorry if the question is obscure, but since I solved it via try/fail everything seems to be obvious now so "I don't know how to ask"

Pawel Cioch
  • 2,895
  • 1
  • 30
  • 29

1 Answers1

8

The <version> tag can be changed simply by using the -Version switch:

nuget pack -Version 2.0.0

You can also use Replacement Tokens. What we can do is use a custom token/tag like this:

    ...
    <version>$myVersion$</version>
    ...
    <dependencies>
      <dependency id="dep1" version="$myVersion$" />

Now to replace this token the -Version switch would not do. We must use the -Properties switch instead:

nuget pack -Prop myVersion=3.0.0

Here's a more an example of a advanced command:

nuget pack My.ProjName\My.ProjName.csproj -Build -Prop Configuration=Release;myVersion=3.0.0 -OutputDirectory SomeDirHere

And to play nicely with powershell (for example), you must wrap -Prop value in double quotes if using semicolon for multiple properties

$userVersion = Read-Host 'Enter a valid version you want to use'
nuget pack .\My.ProjName\My.ProjName.csproj -Build -Prop "Configuration=Release;myVersion=$userVersion" -OutputDirectory SomeDirHere

NOTE: using Nuget's predefined $version$ token can work but the version in that case would be obtained from AssemblyInfo.cs whereas the idea above was to set the version explicitly via the command line.

I hope someone will find it useful and time saving.

G S
  • 35,511
  • 22
  • 84
  • 118
Pawel Cioch
  • 2,895
  • 1
  • 30
  • 29
  • They really should state explicitly that the semicolon separated properties list should be enclosed in quotes, this really threw me. Thank you. – James Blake Aug 15 '16 at 15:31