创buildNuGet包时不要包含来自packages.config文件的依赖项

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

首先我创build.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> 

然后我构build解决scheme并创build一个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'. 

创build的.nupkg软件包中包含一个.nuspec文件,但它包含一个依赖关系部分,这在我原来的.nuspec文件中没有。

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

我相信这是因为这个…(从上面的输出)

 Found packages.config. Using packages listed as dependencies 

我怎样才能使NuGet 自动解决依赖关系,并将其插入到pack命令生成的.nuspec文件?

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

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

在创build软件包时不包括开发依赖项

防止你的包依赖于其他包

从NuGet 2.7开始,有一个新的developmentDependency属性可以添加到项目的packages.config文件中的package节点上 。 因此,如果您的项目包含一个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"

防止你的开发包被其他包所依赖

如果你正在创build一个开发NuGet包供其他人使用,你知道他们不想在他们的包中有一个依赖,那么你可以避免用户必须手动添加这个属性到他们的packages.config文件,通过使用NuGet 2.8 developmentDependency .nu​​spec文件的元数据部分中的依赖属性 。 当你的软件包由其他人安装时,这将自动将developmentDependency属性添加到packages.config文件中。 这里的问题是它要求用户至less安装了NuGet 2.8。 幸运的是,我们可以使用NuGet 2.5 minClientVersion属性来确保用户至less安装了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 (eg .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> 

如果你不想强迫你的用户在使用你的软件包之前至less安装了NuGet v2.8,那么你可以使用我提出并在 NuGet 2.8 developmentDependency属性存在之前发布的解决scheme 。

您可以明确指定要包含的文件,然后在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> 

你应该在这里设置src属性的相对于nuspec文件的path。 然后你可以运行pack命令。

 nuget.exe pack MyProject\MyProject.nuspec 

根据问题#1956( https://nuget.codeplex.com/workitem/1956 )和拉请求3998( https://nuget.codeplex.com/SourceControl/network/forks/adamralph/nuget/contribution/3998 )这function已经包含在2.4版本 2.7发行版中。

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

更新:问题#1956已被开发者更新。 该function并未包含在2.4版本中,但推迟到即将发布的2.7版本。 这就是为什么它尚未logging。

如果您正在使用PackageReference元素而不是package.config文件,则下面是解决scheme:

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

要么

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

相关的GitHub讨论

我不确定为什么要忽略NuGet包运行的依赖关系,但是您是否想过使用NuGet包资源pipe理器工具来创build包?