在 Azure DevOps 构建管道中传递 NuGet 'Pack' 个参数

Passing NuGet 'Pack' arguments in Azure DevOps Build Pipeline

在我的 Azure DevOps 构建管道中,我添加了 pack 命令类型(版本 2.*)的 NuGet 任务。

我正在尝试传递 -Exclude ***.tt 参数,以便在从其他项目引用时排除这些文件。但是,在生成的 NuGet 包中,这些 .tt 文件出现在 Content 文件夹中。

主机日志中写入的命令为:

nuget.exe pack PROJECTNAME.csproj -NonInteractive -OutputDirectory SOME_OUTPUTPATH -Properties "Configuration=release;\"-Exclude ***.tt\"" -Verbosity Detailed

如您所见,exclude 不是属性的一部分,但应作为单独的 -Exclude 参数。

知道我该如何实现吗?非常感谢!

添加了屏幕截图和 YAML:

steps:
- task: NuGetCommand@2
  displayName: 'NuGet pack'
  inputs:
    command: pack
    packagesToPack: ProjectName.csproj
    buildProperties: '-Exclude ***.tt'

Passing NuGet 'Pack' arguments in Azure DevOps Build Pipeline

您不能以这种方式使用参数 -Exclude。那是因为这个参数是用于.nuspec而不是.csproj-Exclude不是工程文件中的属性,所以我们不能和.csproj一起使用。

查看nuget文档nuget pack command (NuGet CLI)时,可以看到如下例子:

nuget pack Package.nuspec -exclude "*.exe" -exclude "*.bat"

如果你想排除 .tt 项目文件 .csproj 的文件,你必须去你的项目文件排除这种类型的文件,如:

  <ItemGroup>
    <None Update="**.tt">
      <Pack>False</Pack>
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>Test.txt</LastGenOutput>
    </None>
    ...
  </ItemGroup>

如果不想修改源文件,可以创建.nuspec文件,然后用参数-Exclude打包.nuspec文件。

例如,我创建以下 .nuspec:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>MyTestCore</id>
    <version>6.0.0</version>
    <authors>TestContentFile</authors>
    <owners>TestContentFile</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package Description</description>
    <contentFiles>
      <files include="any/any/Data.xml" buildAction="content" flatten="true" copyToOutput="true"/>
      <files include="any/any/Test.cs" buildAction="content" flatten="true" copyToOutput="true"/>
    </contentFiles>
  </metadata>

  <files>
    <file src="contentFiles/any/any/Data.xml" target="contentFiles/any/any/Data.xml" />
    <file src="contentFiles/any/any/Test.cs" target="contentFiles/any/any/Test.cs" />
  </files>
</package>

如果我用参数 -Exclude "**\*.cs":

打包这个 .nuspec 文件

然后我们可以知道,.cs被排除在包中。

希望这对您有所帮助。

当您将 -Excludenuget pack .csproj 一起使用时,您需要提供排除文件的完整路径。但这里还有另一个问题,Azure DevOps 将 -Exclude 附加到 -Properties 并且忽略了排除项,因此我们需要以另一种方式添加 -Exclude,如何?在 NuGet custom 命令中:

- task: NuGetCommand@2
  inputs:
    command: custom
    arguments: 'pack "$(Build.SourcesDirectory)\Path\To\.csproj" -Exclude "$(Build.SourcesDirectory)\Path\to\*.tt"'

这是您尝试时来自常规 -Exclude 的日志,您可以看到 nuget 打包了我的 .exe 文件:

这里是我创建自定义命令后的日志,没有.exe:

我的任务是:

- task: NuGetCommand@2
  inputs:
    command: custom
    arguments: 'pack "$(Build.SourcesDirectory)\TestVars\TestVars\TestVars.csproj" -Exclude "$(Build.SourcesDirectory)\TestVars\TestVars\bin\Debug\*.exe"'

它在 Visual Studio 2015 年为我解决了:

<ItemGroup>
    <None Include="**.tt">
        <Pack>False</Pack>
        <Generator>TextTemplatingFileGenerator</Generator>
        <LastGenOutput>Test.txt</LastGenOutput>
    </None>
    ...
</ItemGroup>