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

时间:2013-02-21 21:53:50

标签: dependencies package nuget nuget-package nuspec

我正在使用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中;这是一个新的“功能”吗?我找不到任何描述这个“功能”的文档或者它实现的时候。

6 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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包供其他人使用,您知道他们不希望在其包中具有依赖关系,那么您可以避免用户必须手动将该属性添加到他们的packages.config文件中使用NuGet 2.8 developmentDependency attribute in the metadata section of your .nuspec file。当您的软件包由其他人安装时,这将自动将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

答案 2 :(得分: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 :(得分: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

答案 4 :(得分:3)

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

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

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

答案 5 :(得分:0)

我不确定您为什么要忽略NuGet包运行所需的依赖项,但是您是否考虑过使用NuGet Package Explorer工具来创建包?