简体   繁体   English

如何在TFS生成后脚本中创建nuget包?

[英]How do you create a nuget package in a TFS post build script?

I'm trying to package and publish a library with nuget from a TFS post build script. 我正在尝试使用TFS后期构建脚本中的nuget打包和发布库。 It seems that TFS build doesn't use the standard project output paths, so it's making things complicated. TFS构建似乎没有使用标准的项目输出路径,因此使事情变得复杂。 All I want to do is build the project (which it does) and then pack and publish with nuget. 我要做的就是构建项目(它确实可以完成),然后使用nuget打包和发布。

Here's my script. 这是我的剧本。 It runs just fine on my local machine. 它在我的本地计算机上运行得很好。

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

And here's the error I'm getting. 这是我遇到的错误。

& : 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

I simplified the script so the line number won't match, but you can see that it's the last line that's causing the error. 我简化了脚本,所以行号不匹配,但是您可以看到它是导致错误的最后一行。 I'm using Visual Studio Online with a hosted build controller. 我正在将Visual Studio Online与托管的构建控制器一起使用。

Why don't you use the standard mechanism? 您为什么不使用标准机制?

  1. Right click on your solution and "Enable Nuget Package Restore" This will create a folder that contains a NuGet.targets file 右键单击您的解决方案,然后单击“启用Nuget软件包还原”,这将创建一个包含NuGet.targets文件的文件夹
  2. Open than file and set to true the Property BuildPackage 打开比文件并将属性BuildPackage设置为true
<!-- Property that enables building a package from a project -->
      <BuildPackage Condition=" '$(BuildPackage)' == '' ">true</BuildPackage>
  1. Edit each project in your solution you want to produce a nuget package from 编辑您要从中生成nuget包的解决方案中的每个项目

    • Right click on the project -> "Unload Project" 右键单击项目->“卸载项目”

    • Right click on the project again and "Edit [NameofYourProject]" 再次右键单击该项目,然后单击“编辑[NameofYourProject]”

    • Set BuildPackage to true by adding the following to the first PropertyGroup 通过将以下内容添加到第一个PropertyGroup来将BuildPackage设置为true

<BuildPackage>true</BuildPackage>
  1. Save the file and Reload the project 保存文件并重新加载项目

  2. Commit your changes queue a new build and you should see your package within the build outcome. 将更改提交到新构建队列中,然后应该在构建结果中看到您的软件包。

This will basically use the project file (for C# the .csproj) to create your package 这将基本上使用项目文件(对于C#.csproj)创建您的程序包


If you need more control over your package creation you could include in you project a .nuspec file Where you can specify more metadata attributes such as: dependencies, tags, title, owners, etc 如果您需要对包创建的更多控制,则可以在项目中包含一个.nuspec文件,您可以在其中指定更多元数据属性,例如:依赖项,标签,标题,所有者等。

or even you could use parameters like 甚至您可以使用类似的参数

<version>$version$</version>

to synchronize the nuget package version with the actual assembly version. 使nuget软件包版本与实际程序集版本同步。

If you need to specify the OutDir on the nuget build command for the build server modify the NuGet.targets file BuildCommand definition as follow. 如果您需要在nuget build命令上为构建服务器指定OutDir,请修改NuGet.targets文件BuildCommand定义,如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM