【问题标题】:How to correctly specify dependency information in nuspec file while creating Nuget package?创建 Nuget 包时如何在 nuspec 文件中正确指定依赖信息?
【发布时间】:2021-06-27 21:52:44
【问题描述】:

我第一次尝试从 Visual Studio 2017 类库创建 Nuget 包。它是一个 .NET Framework 4.6.2 项目。 类库正在引用解决方案资源管理器下的引用部分中的其他一些 nuget 包、dll、exe。

以下是我查看一些 youtube 视频和 Microsoft 文档后采取的步骤:

右键单击项目并选择属性。 构建选项,将配置设置为发布。已保存和关闭的项目属性。 打开 csproj 文件并将配置更改为发布

<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>

现在以发布模式构建项目。我可以在

下看到dll
MyProject\bin\Release and also under MyProject\bin\Debug

然后我使用

创建规范文件

nuget 规范 打开它并进行适当的更改,然后

nuget 包 MyProject.nuspec

我收到了许多警告,例如 Debug 和 Release 目录:

WARNING: NU5100: The assembly 'bin\Debug\Encryption.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.

虽然类库(我正在创建 Nuget)有一个 packages.config 并且有引用:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Encryption" version="1.1.0" targetFramework="net462" />
    ...
    ...
    ...
<package id="TeraData" version="16.20.8" targetFramework="net462" />
</packages>

由于收到警告,我尝试在 nuspec 文件中输入依赖项信息。这是我的 nuspec 文件的样子

   <?xml version="1.0" encoding="utf-8"?>
    <package >
      <metadata>
        <id>ProjectTitle</id>
        <version>1.0.0</version>
        <title>ProjectTitle</title>
        <authors>auther name</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>desc of package</description>
        <releaseNotes>release notes text</releaseNotes>
        <copyright>Copyright info</copyright>
        <tags>some tages</tags>
    
        <dependencies>
          <dependency id="Encryption" version="1.1.0" />
    ...
          <dependency id="TeraData" version="16.20.8" />
        </dependencies>
      </metadata>
    </package>

但仍然收到相同的警告。如果可以,请提供一个示例 nuspec 中的依赖信息应该是什么样子,那真的很有帮助! 如有遗漏请指教!

【问题讨论】:

  • 由于错误是关于 nuspec 文件的,因此无法弄清楚你到底错在哪里......通常我们比用户更信任编译器......除非帖子中有明确的证据(如@ 987654321@)...
  • 我用 nupec 文件中的代码更新了帖子。请让我知道其他信息会有所帮助
  • 有些帖子引用了 lib 文件夹。例如。 stackoverflow.com/questions/8764236/…。我在我的项目目录中没有看到 lib 文件夹。它应该在哪里? lib\net40 - 这是否意味着 .Net 框架 4.0
  • “bin\Debug\Encryption.dll”是您的 DLL 还是“加密”NuGet 的一部分?我认为在这种情况下您实际上不需要做任何事情,因为依赖项应该正确安装该 NuGet...您是否尝试将新创建的 NuGet 添加到空白项目以查看 实际 发生了什么?我非常担心从我的 Bin 文件夹中包含外部 DLL(因为它可能会导致 nuget 用户在路上不匹配)。
  • 是的,我确实尝试从另一个项目中引用它,这就是我得到的:无法安装包 MyNugetPackage 1.0.0'。您正在尝试将此包安装到以“.NETFramework,Version=v4.6.2”为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件。如需更多信息,请联系包作者。

标签: c# visual-studio dependencies nuget nuspec


【解决方案1】:

我认为这只是你的 nuget pack 方法的命令的问题。

我们通常不使用nuget pack xxx.nusepc命令打包nuget包,因为它不能自动将包含主nuget项目的dll的已生成的dll,pdb文件打包到nupkg中。

你必须用它写整个nuspec 节点。您必须在 nuspec 文件中写入 &lt;files&gt; 节点以包含您的主项目的 dll,以便删除缺少依赖项的警告。你不应该额外添加&lt;references&gt;节点。

喜欢:

 <?xml version="1.0" encoding="utf-8"?>
    <package >
      <metadata>
        <id>ProjectTitle</id>
        <version>1.0.0</version>
        <title>ProjectTitle</title>
        <authors>auther name</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>desc of package</description>
        <releaseNotes>release notes text</releaseNotes>
        <copyright>Copyright info</copyright>
        <tags>some tages</tags>
    
        <dependencies>
          <dependency id="Encryption" version="1.1.0" />
    ...
          <dependency id="TeraData" version="16.20.8" />
        </dependencies>
      </metadata>
<files>
<file src="bin\Release\ProjectTitle.dll" target="lib\net462" />

.....
</files>
    </package>

然后,使用 nuget pack xxx.nuspec -Properties Configuration=Release 命令打包。您应该以这种方式打包主项目的 dll。如果您的项目引用了其他程序集 dll 或额外的 exe 文件。

你应该添加它们:

  <file src="bin\Release\extra_assembly.dll" target="lib\net462" />
  <file src="bin\Release\extra_exe.exe" target="lib\net462" />

==========================================

但是,这个功能不是很方便。而且我们通常不需要它们,我们通常使用这个:

nuget pack xxx.csproj

通常,我们使用nuget pack xxx.csproj -Properties Configuration=Release进行打包,无需任何其他节点。在此之前,您应该cd xxx\&lt;project folder&gt;

使用这个nuspec 文件:

 <?xml version="1.0" encoding="utf-8"?>
        <package >
          <metadata>
            <id>ProjectTitle</id>
            <version>1.0.0</version>
            <title>ProjectTitle</title>
            <authors>auther name</authors>
            <requireLicenseAcceptance>false</requireLicenseAcceptance>
            <description>desc of package</description>
            <releaseNotes>release notes text</releaseNotes>
            <copyright>Copyright info</copyright>
            <tags>some tages</tags>
        
            <dependencies>
              <dependency id="Encryption" version="1.1.0" />
        ...
              <dependency id="TeraData" version="16.20.8" />
            </dependencies>
          </metadata>
  <!--If you have any other referenced assembly dll files or pdb files, exe files, you should add them here.-->
    <files>
    .....
    </files>
        </package>

您不应使用 &lt;file&gt; 节点添加主 nuget 项目的 dll,它会使用该命令自动添加到您的 nupkg

你创建你的nuget包的新发布版本时,首先卸载你项目下的旧版本,然后删除C:\Users\xxx\.nuget\packages下的所有缓存文件。之后,在您的新项目中重新安装新版本。

【讨论】:

  • 依赖项下的内容和文件下的内容?我看到你提到 ProjectTitle.dll 在文件下。我一直将相同的文件(项目文件除外)放在依赖项和文件下)。在解决方案资源管理器中,我有一些 dll 和一些 exe。在 project.config 中,我只有 dll,例如()。请指教。 projectTitle.dll 也永远不会被复制到 target="lib\net462" (这是不正确的)
  • files 是将额外的项目文件打包到 nupkg 文件中,而 dependency 是为安​​装 nuget 包的主项目添加任何其他 nuget 依赖项或程序集 dll。更多信息可以参考this document。文件可以将各种项目内容打包成nupkg下不同功能的节点。见this
  • project.config 是什么意思?所有这些都应该在nuspec 文件下。您应该在 xxx.csproj 存在的项目文件夹下生成 nuspec 文件。在nuspec文件下添加这些节点,然后添加&lt;file src="bin\Release\ProjectTitle.dll"(the path of the ProjectTitle.dll) target="lib\net462" /&gt;......,然后使用cd xxx\project foldernuget pack生成新的nupkg。可以使用7zip解压nupkg,查看下是否有ProjectTitle.dll
  • 另外,&lt;package id="Encryption" version="1.1.0" targetFramework="net462" /&gt; 应该添加到packages.config 文件中。完成后,在Tools-->Nuget Package Manager-->Package Manager Console下运行update-package -reinstall进行安装。还有an issue about pack dlls。你应该检查nuget pack with nuspec document carefully
  • 你用我的方法拿到合适的包了吗?使用nuget pack xxx.csproj 而不是nuget pack xxx.nuspec。请告诉我们您的任何反馈,我们将进一步帮助您。也许你应该让你的问题更清楚:)
【解决方案2】:

这是使用 .NET 框架的 nuspec 文件结构,最终对我有用:

   <?xml version="1.0" encoding="utf-8"?>
    <package >
      <metadata>
        <id>ClasslibProj </id>
        <version>1.0.0.0</version>
        <title> ClasslibProj</title>
        <authors>author(s) name</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>desc</description>
        <releaseNotes>release notes</releaseNotes>
        <copyright>Copyright @ Company name 2021</copyright>
        <tags>tags to search </tags>
        <references>
          <group targetFramework=".NETFramework4.6.2">
            <reference file="SomeOtherNugetpackage1.dll"/>
           <reference file="anyexecutable.exe"/>
    …
    
            <reference file="ClasslibProj.dll"/> //dll you are working with
          </group>
        </references>
      </metadata>
      <files>
        <file src="bin\Release\SomeNugetOtherpackage1.dll" target="lib\net20"/>
       <file src="bin\Release\anyexecutable.exe" target="lib"/>
    ..
        <file src="bin\Release\ClasslibProj.dll" target="lib\net462"/>
      </files>
    </package>

在发布模式下构建项目。 使用命令:

nuget pack ClasslibProj.csproj 

正如 Sara Liu 所说,避免使用 ClasslibProj.nuspec
或者你可以使用详细的命令:

nuget pack ClasslibProj.csproj -Properties Configuration=Release

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多