NuGet:替换令牌 'Configuration' 在构建 Visual Studio 解决方案时没有价值

NuGet: replacement token 'Configuration' has no value when building Visual Studio solution

我使用 VS2013 的 NuGet Packager 项目模板创建了一个 NuGet 包。我正在尝试使用 Package.nuspec 文件中的 Replacement Tokensbin\CONFIGURATION 文件夹中的 DLL 文件复制到 nupkg 文件的 lib 文件夹中。

在 Visual Studio 中构建解决方案时,我在 NuGet.log 中遇到以下错误:

[INFO] : Attempting to build package from 'Package.nuspec'.
[ERROR] : System.InvalidOperationException: The replacement token 'configuration' has no value.
[ERROR] :    at NuGet.Preprocessor.ReplaceToken(String propertyName, IPropertyProvider propertyProvider, Boolean throwIfNotFound)
[ERROR] :    at NuGet.Preprocessor.Process(Stream stream, IPropertyProvider propertyProvider, Boolean throwIfNotFound)
[ERROR] :    at NuGet.Manifest.ReadFrom(Stream stream, IPropertyProvider propertyProvider, Boolean validateSchema)
[ERROR] :    at NuGet.PackageBuilder.ReadManifest(Stream stream, String basePath, IPropertyProvider propertyProvider)
[ERROR] :    at NuGet.PackageBuilder..ctor(String path, String basePath, IPropertyProvider propertyProvider, Boolean includeEmptyDirectories)
[ERROR] :    at NuGet.CommandLine.PackCommand.CreatePackageBuilderFromNuspec(String path)
[ERROR] :    at NuGet.CommandLine.PackCommand.BuildFromNuspec(String path)
[ERROR] :    at NuGet.CommandLine.PackCommand.BuildPackage(String path)
[ERROR] :    at NuGet.CommandLine.PackCommand.ExecuteCommand()
[ERROR] :    at NuGet.CommandLine.Command.ExecuteCommandAsync()
[ERROR] :    at NuGet.CommandLine.Command.Execute()
[ERROR] :    at NuGet.CommandLine.Program.MainCore(String workingDirectory, String[] args)

Package.nuspec的相关片段:

<package>
  <files>
    <file src="bin$configuration$$id$.dll" target="lib\net451\" />

我可以使用这个命令从命令行打包它:

c:\Path\To\Project> nuget pack -Properties Package.nuspec

所以我在 NuGetPackage 中做了一些尝试。ps1 并找到了这个部分:

# Create symbols package if any .pdb files are located in the lib folder
If ((Get-ChildItem *.pdb -Path .\lib -Recurse).Count -gt 0) {
    $packageTask = Create-Process .\NuGet.exe ("pack Package.nuspec -Symbol -Verbosity Detailed")
    $packageTask.Start() | Out-Null
    $packageTask.WaitForExit()

    $output = ($packageTask.StandardOutput.ReadToEnd() -Split '[\r\n]') |? {$_}
    $error = (($packageTask.StandardError.ReadToEnd() -Split '[\r\n]') |? {$_}) 
    Write-Log $output
    Write-Log $error Error

    $global:ExitCode = $packageTask.ExitCode
}
Else {
    $packageTask = Create-Process .\NuGet.exe ("pack Package.nuspec -Verbosity Detailed")
    $packageTask.Start() | Out-Null
    $packageTask.WaitForExit()

    $output = ($packageTask.StandardOutput.ReadToEnd() -Split '[\r\n]') |? {$_}
    $error = (($packageTask.StandardError.ReadToEnd() -Split '[\r\n]') |? {$_}) 
    Write-Log $output
    Write-Log $error Error

    $global:ExitCode = $packageTask.ExitCode
}

看到NuGet.exe命令是在哪里创建的,所以修改为:

$packageTask = Create-Process .\NuGet.exe ("pack -Properties Package.nuspec -Verbosity Detailed")

然后我得到:

NuGet.CommandLineException: Please specify a nuspec or project file to use.

我去掉了文件路径的$configuration$\段,然后得到:

The replacement token 'id' has no value


更新:我将 Package.nuspec 替换为我在 NuGetPackage 中的项目的 .csproj 文件的名称。ps1 现在通过 Visual Studio 构建正确地创建了 .nupkg 文件。不过,我不想对每个 NuGet 包都这样做。


从 Visual Studio 而不是命令行构建时如何在 Package.nuspec 中使用替换标记?

我找到了解决方法。 Visual Studio 中的 NuGet Packager 项目模板中包含的用于打包项目的默认 power shell 脚本会打包 .nuspec 文件。将其更改为我的项目的 .csproj 文件解决了这个问题,我必须在两个地方做:

# Create symbols package if any .pdb files are located in the lib folder
If ((Get-ChildItem *.pdb -Path .\lib -Recurse).Count -gt 0) {
    $packageTask = Create-Process .\NuGet.exe ("pack YourProject.csproj -Symbol -Verbosity Detailed")

    // ...
}
Else {
    $packageTask = Create-Process .\NuGet.exe ("pack YourProject.csproj -Verbosity Detailed")

    // ...
}

这不是 100% 理想,因为 NuGet Packager 项目开箱即用,但它是一个简单的更改。

请注意 PowerShell 构建脚本中从 pack Package.nuspecpack YourProject.csproj 的更改,您将 YourProject.csproj 替换为 NuGet 包项目的 .csproj 文件。