Search code examples
nugetnuspec

How to specify configuration-specific folder in nuspec


The examples I've seen specify files to be included using a path relative to the location of the nuspec file, e.g.:

<file src=".\bin\Debug\SomeFile.dll" target="..." />

Is there a way to specify this in such a way that it will use the appropriate source directory depending on my build configuration: i.e.:

  • bin\Debug if I package with -Prop Configuration=Debug
  • bin\Release if I package with -Prop Configuration=Release

Solution

  • TL;DR

    Use the $configuration$ token.

    Full Explanation

    In the .nuspec file reference, under Replace Tokens, it defines the $configuration$ token as:

    "Configuration used to build the assembly, defaulting to Debug. Note that to create a package using a Release configuration, you always use -properties Configuration=Release on the command line."

    And continues with:

    Tokens can also be used to resolve paths when you include assembly files and content files. The tokens have the same names as the MSBuild properties, making it possible to select files to be included depending on the current build configuration.

    And then concludes with two examples:

    For example, if you use the following tokens in the .nuspec file:

    <files>
        <file src="bin\$configuration$\$id$.pdb" target="lib\net40\" />
    </files>
    

    And you build an assembly whose AssemblyName is LoggingLibrary with the Release configuration in MSBuild, the resulting lines in the .nuspec file in the package is as follows:

    <files>
        <file src="bin\Release\LoggingLibrary.pdb" target="lib\net40" />
    </files>