在本地构建时,如何在CI过程中创建Nuget包?

本文关键字:过程中 创建 Nuget CI 构建 | 更新日期: 2023-09-27 18:14:49

我知道以前有人问过这个问题,但由于某种原因,我找到的答案对我不起作用。

请让我描述一下我的问题。

我正在使用Visual Studio 2012和TFS 2012

在我的组织中,我们有几个应用程序,其中一些公开了共享的DLL,我们想把它们作为Nuget包发布到一个私有的Nuget服务器

因此,在其中一个项目中,我将以下代码添加到csproj文件
  <Target Name="AfterBuild">
    <Exec ContinueOnError="false" WorkingDirectory="$(OutDir)" Command="&quot;$(SolutionDir).nuget'nuget.exe&quot; pack &quot;$(ProjectPath)&quot; -Exclude &quot;*.nlp&quot; -IncludeReferencedProjects -NonInteractive -Verbosity detailed -NoDefaultExcludes -Properties Configuration=&quot;$(Configuration)&quot;;Platform=&quot;$(Platform)&quot;" />
  </Target>

在与我的项目文件相同的级别,我有nuspec文件表示我的Nuget元数据,它看起来像:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>Super dooper cool framework</releaseNotes>
    <copyright>$copyright$</copyright>
    <tags>Some My Company framework</tags>
  </metadata>
</package>

这在本地工作得很好,每次我构建我的项目时,它都会按我想要的方式为我创建一个Nuget包。

但是在构建服务器上它不起作用,我尝试了几种组合,我一直收到相同的错误:

     "D:'Builds'23'someCompany'somebuilddefinition'Sources'.nuget'nuget.exe" pack "D:'Builds'23'someCompany'somebuilddefinition'Sources'NugetLibrary'NugetLibrary.csproj" -Exclude "*.nlp" -IncludeReferencedProjects -NonInteractive -Verbosity detailed -NoDefaultExcludes -Properties Configuration="Release";Platform="AnyCPU"
     Attempting to build package from 'NugetLibrary.csproj'.
     NuGet.CommandLineException: Unable to find 'D:'Builds'23'someCompany'somebuilddefinition'Sources'NugetLibrary'bin'Release'NugetLibrary.dll'. Make sure the project has been built.
        at NuGet.Commands.ProjectFactory.BuildProject()
        at NuGet.Commands.ProjectFactory.CreateBuilder(String basePath)
        at NuGet.Commands.PackCommand.BuildFromProjectFile(String path)
        at NuGet.Commands.PackCommand.BuildPackage(String path)
        at NuGet.Commands.PackCommand.ExecuteCommand()
        at NuGet.Commands.Command.Execute()
        at NuGet.Program.Main(String[] args)

这是可以理解的,因为在构建服务器上,输出路径被覆盖,它指向:

D:'Builds'23'someCompany'somebuilddefinition'Binaries

所以我尝试了几种组合,但我找不到一种方法来强迫Nuget使用我的自定义路径来查找由构建过程生成的DLL。

这些是我到目前为止尝试过的组合:

(基本上我玩过以下Nuget属性:OutputDirectory, BasePathProperties)

顺便说一句,$(OutDir) MSBuild属性指向构建服务器上的正确文件夹

<Exec ContinueOnError="false" WorkingDirectory="$(OutDir)" Command="&quot;$(SolutionDir).nuget'nuget.exe&quot; pack &quot;$(ProjectPath)&quot; -Exclude &quot;*.nlp&quot; -IncludeReferencedProjects -NonInteractive -Verbosity detailed -NoDefaultExcludes -Properties Configuration=&quot;$(Configuration)&quot;;Platform=&quot;$(Platform)&quot;" />

<Exec ContinueOnError="false" WorkingDirectory="$(ProjectDir)" Command="&quot;$(SolutionDir).nuget'nuget.exe&quot; pack &quot;$(ProjectPath)&quot; -Exclude &quot;*.nlp&quot; -IncludeReferencedProjects -NonInteractive -Verbosity detailed -NoDefaultExcludes -BasePath &quot;$(OutDir)&quot; -OutputDirectory &quot;$(OutDir)&quot;" />

<Exec ContinueOnError="false" WorkingDirectory="$(ProjectDir)" Command="&quot;$(SolutionDir).nuget'nuget.exe&quot; pack &quot;$(ProjectPath)&quot; -Exclude &quot;*.nlp&quot; -IncludeReferencedProjects -NonInteractive -Verbosity detailed -NoDefaultExcludes -OutputDirectory &quot;$(OutDir)&quot;" />

<Exec ContinueOnError="false" WorkingDirectory="$(ProjectDir)" Command="&quot;$(SolutionDir).nuget'nuget.exe&quot; pack &quot;$(ProjectPath)&quot; -Exclude &quot;*.nlp&quot; -IncludeReferencedProjects -NonInteractive -Verbosity detailed -NoDefaultExcludes -BasePath &quot;$(OutDir)&quot; -OutputDirectory &quot;$(OutDir)&quot;" />

<Exec ContinueOnError="false" WorkingDirectory="$(ProjectDir)" Command="&quot;$(SolutionDir).nuget'nuget.exe&quot; pack &quot;$(ProjectPath)&quot; -Exclude &quot;*.nlp&quot; -IncludeReferencedProjects -NonInteractive -Verbosity detailed -NoDefaultExcludes -BasePath &quot;$(OutDir)&quot; -OutputDirectory &quot;$(OutDir)&quot; -Properties Configuration=&quot;$(Configuration)&quot;;Platform=&quot;$(Platform)&quot;" />

<Exec ContinueOnError="false" WorkingDirectory="$(OutDir)" Command="&quot;$(SolutionDir).nuget'nuget.exe&quot; pack &quot;$(ProjectPath)&quot; -Exclude &quot;*.nlp&quot; -IncludeReferencedProjects -NonInteractive -Verbosity detailed -NoDefaultExcludes" />

我一直收到相同的错误:

     NuGet.CommandLineException: Unable to find 'D:'Builds'23'somecompany'somebuildserver'Sources'NugetLibrary'bin'Release'NugetLibrary.dll'. Make sure the project has been built.

所以Nuget只是忽略BasePath属性?为什么?我错过了什么?

在本地构建时,如何在CI过程中创建Nuget包?

看起来您可能会遇到http://nuget.codeplex.com/workitem/1486。根据对这个问题的评论,看起来将-Properties OutDir=$(OutDir)添加到您的pack命令中可能会解决这个问题。

你可能想给我的个人项目http://nuproj.codeplex.com/一个尝试。它允许通过MSBuild创建NuGet包。