Don Chambers
Don Chambers

Reputation: 4251

Pass variable to nuspec with dotnet pack

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?

Upvotes: 6

Views: 10145

Answers (2)

Hameed Syed
Hameed Syed

Reputation: 4245

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

Upvotes: 4

Don Chambers
Don Chambers

Reputation: 4251

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.

Upvotes: 22

Related Questions