![](/img/trans.png)
[英]NuGet Pack: Ignore packages.config for dependencies and use .nuspec file instead
[英]Don't include dependencies from packages.config file when creating NuGet package
我正在使用 Nuget 創建包。 我想創建一個不包含任何其他 NuGet 包的依賴項(在.nuspec
)文件的包。 我的項目確實在它的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不自動解析依賴項並將它們插入到從pack
命令生成的.nuspec
文件中?
我目前正在使用 NuGet 2.2。 另外,我不認為這種行為發生在舊版本的 NuGet 中; 這是一個新的“功能”嗎? 我找不到任何描述此“功能”或何時實施的文檔。
在 2.7 版中有一個名為 developmentDependency 的選項,可以將其設置到 package.config 中以避免包含依賴項。
<?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>
從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"
。
如果您正在創建供其他人使用的開發 NuGet 包,並且您知道他們不希望將其作為其包中的依賴項,那么您可以避免用戶必須使用NuGet將該屬性手動添加到他們的 packages.config 文件中.nuspec 文件的元數據部分中的 2.8 developmentDependency
屬性。 當你的包被其他人安裝時,這將自動將 developmentDependency 屬性添加到 packages.config 文件中。 這里的問題是它要求用戶至少安裝 NuGet 2.8。 幸運的是,我們可以利用NuGet 2.5 minClientVersion 屬性來確保用戶至少安裝了 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 屬性存在之前提出並在博客中介紹的解決方案。
如果您使用的是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>
您可以明確指定要包含的文件,然后對 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
根據問題 #1956 ( https://nuget.codeplex.com/workitem/1956 ) 和 pull request 3998 ( https://nuget.codeplex.com/SourceControl/network/forks/adamralph/nuget/contribution/3998 ) 這個功能已包含在
2.4 版本
2.7 版本中。
但是,我找不到有關該功能的文檔,而且我仍然不知道如何使用它。 如果有人知道,請更新此答案。
更新:問題 #1956 已由開發人員更新。 該功能未包含在 2.4 版本中,但延遲到即將發布的 2.7 版本。 這就是它尚未記錄的原因。
我不確定您為什么要忽略運行 NuGet 包所需的依賴項,但您是否考慮過使用NuGet 包資源管理器工具來創建包?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.