Dependencies Don';在创建NuGet包时,不要包含packages.config文件中的依赖项

Dependencies Don';在创建NuGet包时,不要包含packages.config文件中的依赖项,dependencies,package,nuget,nuget-package,nuspec,Dependencies,Package,Nuget,Nuget Package,Nuspec,我正在使用Nuget创建包。我想创建一个不包含任何依赖项的包(在.nuspec文件中)到任何其他NuGet包。我的项目在其packages.config文件中定义了NuGet包依赖项 首先,我创建.nuspec文件 C:\code\MySolution>.nuget\nuget.exe spec MyProject\MyProject.csproj <dependencies> <dependency id="Google.ProtocolBuffers" vers

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

首先,我创建
.nuspec
文件

C:\code\MySolution>.nuget\nuget.exe spec MyProject\MyProject.csproj
<dependencies>
  <dependency id="Google.ProtocolBuffers" version="2.4.1.473" />
</dependencies>
我将生成的
.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>
这是该命令的输出

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
文件中没有的依赖项部分

C:\code\MySolution>.nuget\nuget.exe spec MyProject\MyProject.csproj
<dependencies>
  <dependency id="Google.ProtocolBuffers" version="2.4.1.473" />
</dependencies>
如何使NuGet自动解析依赖项并将它们插入由
pack
命令生成的
.nuspec
文件


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

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

您可以明确指定要包含哪些文件,然后在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>
根据1956年发布的()和拉取请求3998(),此功能已包含在2.4版本2.7版本中

然而,我找不到有关该功能的文档,我仍然不知道如何使用它。如果有人知道,请更新此答案


更新:开发商已更新1956年版。该功能未包含在2.4版本中,但推迟到即将发布的2.7版本。这就是为什么它还没有被记录下来。

在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包作为依赖项包含的NuGet包,您可以像这样使用此属性:

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

如果您不想强制用户在使用软件包之前至少安装NuGet v2.8,则可以在NuGet 2.8 developmentDependency属性存在之前安装。

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

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


全部的


我们的构建过程由CI服务器TeamCity自动完成,因此我们没有考虑在此过程中使用GUI工具。我认为这个工具不会帮助我们实现自动化。我们不需要
.nuspec
中包含的依赖项的原因是因为我们只使用GoogleProtocolBuffers来生成适当的C源文件;它不是运行时依赖项。您可以使用该工具创建和维护.nuspec文件,然后直接从生成服务器上的.nuspec文件生成包,而不是从.csprojI生成包。请参阅。。。您建议使用该工具维护一个
.nuspec
文件,类似于@UfukHacıoğulları在其回答中建议的内容。我将在试验他的解决方案时试用它。在我们的例子中,这是因为我们在许多程序集上使用ILMerge。我们不需要Nuget拉入所有的依赖程序集,因为它们已经是主程序集的一部分,并且不再“依赖”它们;等我有时间时,我会尝试这个解决方案。我的印象是,使用
.nuspec
文件来生成包不是一个好主意;首选使用
.csproj
文件。以这种方式构建包有什么后果吗?@JesseWebb我想你不能用变量填充一些字段,比如version,但除此之外应该没问题。我认为使用nuspec文件没有什么错。你有没有其他的消息来源?我想没有人特别建议你不要这样做;NuGet文档只是简单地说在pack命令中使用
csproj
文件,“nuspec实际上会被拾取”。不过,这个解决方案似乎很有希望,我会尽快尝试。。。在过去的几周里,我真的被淹没了。@DiggyJohn规范在以后的版本中可能已经改变了。引用.nuspec而不是.csproj将起作用(即不包括未明确指定的依赖项)。缺点是无法解析标准替换令牌,例如,$version$不会设置为项目的AssemblyInfo.cs中的值。请参阅最近更新的问题。该功能似乎已经过测试,并发现了一些问题。删除packages.config文件或将其重命名。然后,打包操作将忽略依赖项。这是我在2.7发布之前的临时解决方案。有人用有效答案回答了这个问题吗。没有这种能力可能是我遇到过的最愚蠢的事情。对我来说,我不能让它包括我的依赖性:(我也无法将依赖项包括在内。多年来,我开始认为这是一个很大的谎言。使用pack命令上的-IncludeReferencedProjects标志来自动包含依赖项,但对删除依赖项没有多大帮助。此功能的开箱即用实现要求包的每个用户将此属性添加到在Nuget 2.8的@deadlydog中,你现在可以声明你的包a了。我在博客中介绍了包作者如何在安装包的过程中自动执行这一步骤,从而免除了用户的责任“发展
<?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>
<PackageReference Include="Nerdbank.GitVersioning" Version="1.5.28-rc" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="1.5.28-rc">
   <PrivateAssets>all</PrivateAssets>
</PackageReference>