【问题标题】:How do you create a nuget package in a TFS post build script?如何在 TFS 后期构建脚本中创建 nuget 包?
【发布时间】:2015-04-13 10:35:23
【问题描述】:

我正在尝试使用 TFS 后期构建脚本中的 nuget 打包和发布库。似乎 TFS 构建不使用标准项目输出路径,所以它使事情变得复杂。我要做的就是构建项目(它确实如此),然后用 nuget 打包和发布。

这是我的脚本。它在我的本地机器上运行良好。

param([string]$project, [string]$version)

if (-not $project)
{
  Write-Error ("The project parameter is required.")
  exit 1
}

if (-not $version)
{
  $version = $Env:TF_BUILD_BUILDNUMBER
}

if (-not $version)
{
  Write-Error ("Either the version parameter or the TF_BUILD_BUILDNUMBER environment variable is required.")
  exit 1
}

$projectFile = "$project\$project.csproj"
$packageFile = "$project.$version.nupkg"
$nugetPath = ".nuget\NuGet.exe"

if ($Env:TF_BUILD_SOURCESDIRECTORY)
{
  # relative paths won't work on the build server
  $projectFile = "$Env:TF_BUILD_SOURCESDIRECTORY\$projectFile"
  $packageFile = "$Env:TF_BUILD_SOURCESDIRECTORY\$packageFile"

  $nugetPath = "$Env:TF_BUILD_SOURCESDIRECTORY\$nugetPath"
}
else
{
  $nugetPath = ".\$nugetPath"
}

Write-Host ("packing $projectFile...")
& $nugetPath pack "$projectFile" -Version $version -Symbols -IncludeReferencedProjects

这是我遇到的错误。

& : The term '.\.nuget\NuGet.exe' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\a\src\project\.build\PublishPackage.ps1:85 char:3
+ & $nugetPath pack "$projectFile" -Version $version -Symbols -IncludeReferencedPr ...
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\.nuget\NuGet.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

我简化了脚本,因此行号不匹配,但您可以看到它是导致错误的最后一行。我正在使用带有托管构建控制器的 Visual Studio Online。

【问题讨论】:

    标签: powershell tfs nuget tfsbuild azure-devops


    【解决方案1】:

    为什么不使用标准机制?

    1. 右键单击您的解决方案并“启用 Nuget 包还原” 这将创建一个包含 NuGet.targets 文件的文件夹
    2. 打开比文件并将 Property BuildPackage 设置为 true
    true
    1. 编辑解决方案中要从中生成 nuget 包的每个项目

      • 项目右键->“卸载项目”

      • 再次右键单击项目并“编辑 [NameofYourProject]”

      • 通过将以下内容添加到第一个 PropertyGroup 将 BuildPackage 设置为 true

    1. 保存文件并重新加载项目

    2. 将您的更改提交到新构建队列中,您应该会在构建结果中看到您的包。

    这将基本上使用项目文件(对于 C# 的 .csproj)来创建你的包


    如果您需要对包创建进行更多控制,您可以在项目中包含一个 .nuspec 文件 您可以在其中指定更多元数据属性,例如:依赖关系、标签、标题、所有者等

    甚至你可以使用像

    这样的参数 $版本$版本>

    将 nuget 包版本与实际程序集版本同步。

    如果您需要在构建服务器的 nuget 构建命令上指定 OutDir,请修改 NuGet.targets 文件 BuildCommand 定义如下。

    $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform);OutDir=$(OutDir)" $(NonInteractiveSwitch) - OutputDirectory "$(PackageOutputDir)" -symbols

    【讨论】:

    • 我什至不知道那是在NuGet.targets。这有记录吗?
    • 我还没有机会尝试。我将大部分构建移至构建预览(非 xaml)。
    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多