Pass variable to nuspec with dotnet pack
Asked Answered
S

2

5

When using dotnet pack, how do I pass a variable to my nuspec file?

I am trying to pass the version. Here is the nuspec:

<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <!-- Required elements-->
        <id>1</id>
        <version>$PackageVersion$</version>
        <description>1</description>
        <authors>1</authors>
    </metadata>
</package>

And here is my command:

dotnet.exe pack /p:NuspecFile=./App.Nuspec /p:PackageVersion=9.7.28170

I get the error: Value cannot be null or an empty string.

Interestingly, if I change the variable to be the description:

<version>1.1.0</version>
<description>$PackageVersion$</description>

I get the error: Description is required.

When the tag is description the tag name is in the error message. However, when the tag is version there is no tag name in the error message - only the term value. Both are required fields.

I am doing this with the command line on my local machine, but the TFS build gives the same error. Here is the TFS tooltip for the Additional build properties for that step. Specifies a list of token = value pairs, separated by semicolons, where each occurrence of $token$ in the .nuspec file will be replaced with the given value. Values can be strings in quotation marks.

It sounds like I am following those rules. And here is what TFS generates:

"C:\Program Files\dotnet\dotnet.exe" pack D:\Agent_work\5\s\MyProject\MyProject.csproj --output D:\Agent_work\5\a /p:NuspecFile=App.nuspec /p:PackageVersion=9.7.28170 --verbosity Normal

Same as my command.

What am I doing wrong?

Scandal answered 26/11, 2018 at 16:32 Comment(0)
S
19

The csproj files acts as a pass-thru. The variables need to be setup as follows:

<NuspecFile>App.nuspec</NuspecFile>
<NuspecProperties>version=$(PackageVersion)</NuspecProperties>

The command is:

dotnet.exe pack /p:PackageVersion=9.7.28170

This uses the Nuspec file from the proj file, and passes the PackageVersion variable to the version tag of the .nuspec file.

Scandal answered 26/11, 2018 at 17:10 Comment(2)
HOW! how did you know where, how... what?Lamarlamarck
I was unable to pass more than one value when trying to use the next approach directly from the command line with dotnet pack: -p:NuspecProperties="key1=value1;key2=value2" ..found here: learn.microsoft.com/en-us/nuget/reference/… but just listing more than one <NuspecProperties> and using the approach you explained here, worked well. Thank you!Precess
Y
3

Assume your nuget specification file is something like this check.nuspec

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>abc1234</id>
     <version>1.0.0</version>
    <title>FutureGroup/title>
    <authors>FutureGroup</authors>
  </metadata>
</package>

Now using dotnet pack cli you want to generate a nupkg package using nuspec file having name FutureGroup.2.0.0-Dev

Add below line in csproj file so that the parameter is passed from dotnet cli via csproj to nuspec file.

<IsPackable>true</IsPackable> 
   <NuspecFile>check.nuspec</NuspecFile>
<NuspecProperties>version=$(PackageVersion)</NuspecProperties>

dotnet cli

dotnet pack  .\sample.csproj  -p:NuspecFile=.\nuget\check.nuspec  -p:NuspecBasePath=.\temp /p:Outputpath=package /p:PackageVersion=2.0.0-Dev
Year answered 20/9, 2020 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.