创建NuGet包时,不要包含packages.config文件中的依赖项

问题描述 投票:51回答:6

我正在使用Nuget来创建包。我想创建一个包,其中不包含任何依赖项(在.nuspec中)到任何其他NuGet包。我的项目确实在其packages.config文件中定义了NuGet包依赖项。

首先,我创建了.nuspec文件...

C:\code\MySolution>.nuget\nuget.exe spec MyProject\MyProject.csproj

我将生成的.nuspec文件编辑为最小,没有依赖项。

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyProject</id>
    <version>1.2.3</version>
    <title>MyProject</title>
    <authors>Example</authors>
    <owners>Example</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Example</description>
    <copyright>Copyright 2013 Example</copyright>
    <tags>example</tags>
    <dependencies />
  </metadata>
</package>

然后我构建解决方案并创建一个NuGet包......

C:\code\MySolution>.nuget\nuget.exe pack MyProject\MyProject.csproj -Verbosity detailed

这是该命令的输出......

Attempting to build package from 'MyProject.csproj'.
Packing files from 'C:\code\MySolution\MyProject\bin\Debug'.
Using 'MyProject.nuspec' for metadata.
Found packages.config. Using packages listed as dependencies

Id: MyProject
Version: 1.2.3
Authors: Example
Description: Example
Tags: example
Dependencies: Google.ProtocolBuffers (= 2.4.1.473)

Added file 'lib\net40\MyProject.dll'.

Successfully created package 'C:\code\MySolution\MyProject.1.2.3.nupkg'.

创建的.nupkg包中包含一个.nuspec文件,但它包含了我在原始.nuspec文件中没有的依赖项部分...

<dependencies>
  <dependency id="Google.ProtocolBuffers" version="2.4.1.473" />
</dependencies>

我相信这是因为这个......(来自上面的输出)

Found packages.config. Using packages listed as dependencies

如何让NuGet不自动解析依赖关系并将它们插入从.nuspec命令生成的pack文件中?

我目前正在使用NuGet 2.2。另外,我不认为这种行为发生在较旧版本的NuGet中;这是一个新的“功能”吗?我找不到任何描述此“功能”的文档或何时实现。

dependencies package nuget nuget-package nuspec
6个回答
46
投票

在2.7版本中,有一个名为developmentDependency的选项可以设置为package.config以避免包含依赖项。

Excluding development dependencies when creating packages

<?xml version="1.0" encoding="utf-8"?>
<packages>
    <package id="jQuery" version="1.5.2" />
    <package id="netfx-Guard" version="1.3.3.2" developmentDependency="true" />
    <package id="microsoft-web-helpers" version="1.15" />
</packages>

27
投票

防止您的包依赖于其他包

至于NuGet 2.7 there is a new developmentDependency attribute,可以是added to a package node in your project's packages.config file。因此,如果您的项目包含一个您不希望NuGet包作为依赖项包含的NuGet包,则可以使用此属性,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="CreateNewNuGetPackageFromProjectAfterEachBuild" version="1.8.1-Prerelease1" targetFramework="net45" developmentDependency="true" />
</packages>

你需要在这里添加的重要部分是developmentDependency="true"

防止您的开发包被其他包依赖

如果您正在创建一个开发NuGet包以供其他人使用,您知道他们不希望在其包中具有依赖关系,那么您可以避免用户必须使用NuGet 2.8 developmentDependency attribute in the metadata section of your .nuspec file手动将该属性添加到他们的packages.config文件中。当您的软件包由其他人安装时,这将自动将developmentDependency属性添加到packages.config文件中。这里的问题是它要求用户至少安装NuGet 2.8。幸运的是,我们可以使用NuGet 2.5 minClientVersion attribute来确保用户至少安装了v2.8;否则,他们会在安装软件包之前通知他们需要更新他们的NuGet客户端。

我有一个开发NuGet包,这是完美的。这是我的.nuspec文件的样子,显示了如何使用minClientVersion和developmentDependency属性(分别为第3行和第20行):

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata minClientVersion="2.8">
    <id>CreateNewNuGetPackageFromProjectAfterEachBuild</id>
    <version>1.8.1-Prerelease1</version>
    <title>Create New NuGet Package From Project After Each Build</title>
    <authors>Daniel Schroeder,iQmetrix</authors>
    <owners>Daniel Schroeder,iQmetrix</owners>
    <licenseUrl>https://newnugetpackage.codeplex.com/license</licenseUrl>
    <projectUrl>https://newnugetpackage.codeplex.com/wikipage?title=NuGet%20Package%20To%20Create%20A%20NuGet%20Package%20From%20Your%20Project%20After%20Every%20Build</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Automatically creates a NuGet package from your project each time it builds. The NuGet package is placed in the project's output directory.
    If you want to use a .nuspec file, place it in the same directory as the project's project file (e.g. .csproj, .vbproj, .fsproj).
    This adds a PostBuildScripts folder to your project to house the PowerShell script that is called from the project's Post-Build event to create the NuGet package.
    If it does not seem to be working, check the Output window for any errors that may have occurred.</description>
    <summary>Automatically creates a NuGet package from your project each time it builds.</summary>
    <releaseNotes>- Changed to use new NuGet built-in method of marking this package as a developmentDependency (now requires NuGet client 2.8 or higher to download this package).</releaseNotes>
    <copyright>Daniel Schroeder 2013</copyright>
    <tags>Auto Automatic Automatically Build Pack Create New NuGet Package From Project After Each Build On PowerShell Power Shell .nupkg new nuget package NewNuGetPackage New-NuGetPackage</tags>
    <developmentDependency>true</developmentDependency>
  </metadata>
  <files>
    <file src="..\New-NuGetPackage.ps1" target="content\PostBuildScripts\New-NuGetPackage.ps1" />
    <file src="Content\NuGet.exe" target="content\PostBuildScripts\NuGet.exe" />
    <file src="Content\BuildNewPackage-RanAutomatically.ps1" target="content\PostBuildScripts\BuildNewPackage-RanAutomatically.ps1" />
    <file src="Content\UploadPackage-RunManually.ps1" target="content\PostBuildScripts\UploadPackage-RunManually.ps1" />
    <file src="Content\UploadPackage-RunManually.bat" target="content\PostBuildScripts\UploadPackage-RunManually.bat" />
    <file src="tools\Install.ps1" target="tools\Install.ps1" />
    <file src="tools\Uninstall.ps1" target="tools\Uninstall.ps1" />
  </files>
</package>

如果您不希望强制用户在使用您的软件包之前至少安装了NuGet v2.8,那么您可以在NuGet 2.8 developmentDependency属性存在之前使用use the solution I came up with and blogged about


8
投票

如果您使用PackageReference元素而不是package.config文件,这里是解决方案:

<PackageReference Include="Nerdbank.GitVersioning" Version="1.5.28-rc" PrivateAssets="All" />

要么

<PackageReference Include="Nerdbank.GitVersioning" Version="1.5.28-rc">
   <PrivateAssets>all</PrivateAssets>
</PackageReference>

Related GitHub discussion


3
投票

您可以显式指定要包含的文件,然后在nuspec文件本身上运行pack命令。

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyProject</id>
    <version>1.2.3</version>
    <title>MyProject</title>
    <authors>Example</authors>
    <owners>Example</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Example</description>
    <copyright>Copyright 2013 Example</copyright>
    <tags>example</tags>
    <dependencies />
  </metadata>

  <files>
    <file src="bin\Release\MyProject.dll" target="lib" />
  </files>

</package>

您应该在此处使用相对于nuspec文件的路径设置src属性。然后你可以运行pack命令。

nuget.exe pack MyProject\MyProject.nuspec

3
投票

根据问题#1956(https://nuget.codeplex.com/workitem/1956)和拉请求3998(https://nuget.codeplex.com/SourceControl/network/forks/adamralph/nuget/contribution/3998)此功能已包含在 2.4发布 2.7发布。

但是我无法找到有关该功能的文档,我仍然没有想到如何使用它。如果有人知道,请更新此答案。

更新:问题#1956已由开发人员更新。该功能未包含在2.4版本中,但延迟到即将发布的2.7版本。这就是为什么它还没有记录。


0
投票

我不确定你为什么要忽略你的NuGet包需要运行的依赖项,但你有没有想过使用NuGet Package Explorer工具来创建你的包?

© www.soinside.com 2019 - 2024. All rights reserved.