如何在TFS post构建脚本中创建nuget包?

时间:2015-02-12 15:46:51

标签: powershell tfs nuget tfsbuild azure-devops

我尝试使用来自TFS post构建脚本的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和托管构建控制器。

1 个答案:

答案 0 :(得分:2)

为什么不使用标准机制?

  1. 右键单击您的解决方案"启用Nuget Package Restore" 这将创建一个包含NuGet.targets文件的文件夹
  2. 打开文件并将Property BuildPackage设置为true
  3.       <!-- Property that enables building a package from a project -->
          <BuildPackage Condition=" '$(BuildPackage)' == '' ">true</BuildPackage>
    
    1. 编辑您想要从

      生成nuget包的解决方案中的每个项目
      • 右键点击该项目 - &gt; &#34;卸载项目&#34;

      • 再次右键点击该项目&#34;编辑[NameofYourProject]&#34;

      • 通过将以下内容添加到第一个PropertyGroup

      • 将BuildPackage设置为true
    2.     
          <BuildPackage>true</BuildPackage> 
      
      1. 保存文件并重新加载项目

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

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

        如果您需要更多地控制包创建,可以在项目中包含.nuspec文件 您可以在其中指定更多元数据属性,例如:依赖项,标记,标题,所有者等

        甚至可以使用

        之类的参数
            <version>$version$</version>
        

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

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

        <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform);OutDir=$(OutDir)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
        
相关问题