【问题标题】:How can I wrap C# DLLs into a NuGet package on Ubuntu?如何在 Ubuntu 上将 C# DLL 包装到 NuGet 包中?
【发布时间】:2021-01-13 13:35:18
【问题描述】:

在 Ubuntu 上,我想将一些 C# DLL 文件包装到 NuGet 包中。在 Windows 上可以使用NuGet package explorer or nuget.exe + manually edited *.csproj.nuspec。总之,当手动编辑 *.nuspec 文件时,可以通过 <files> 部分添加 DLL:

<files>
   <file src="some\Path\YourDll.dll" target="lib"></file>
</files>

在 Ubuntu 上,我想改用 dotnet pack。但是它似乎无法对*.csproj.nuspec 文件进行操作:

Usage: dotnet pack [options] <PROJECT | SOLUTION>

Arguments:
  <PROJECT | SOLUTION>   The project or solution file to operate on. If a file is not specified, the command will search the current directory for one.

Options:
  -h, --help                            Show command line help.
  -o, --output <OUTPUT_DIR>             The output directory to place built packages in.
  --no-build                            Do not build the project before packing. Implies --no-restore.
  --include-symbols                     Include packages with symbols in addition to regular packages in output directory.
  --include-source                      Include PDBs and source files. Source files go into the 'src' folder in the resulting nuget package.
  -c, --configuration <CONFIGURATION>   The configuration to use for building the package. The default for most projects is 'Debug'.
  --version-suffix <VERSION_SUFFIX>     Set the value of the $(VersionSuffix) property to use when building the project.
  -s, --serviceable                     Set the serviceable flag in the package. See https://aka.ms/nupkgservicing for more information.
  --nologo                              Do not display the startup banner or the copyright message.
  --interactive                         Allows the command to stop and wait for user input or action (for example to complete authentication).
  --no-restore                          Do not restore the project before building.
  -v, --verbosity <LEVEL>               Set the MSBuild verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].
  --runtime <RUNTIME_IDENTIFIER>        The target runtime to restore packages for.
  --no-dependencies                     Do not restore project-to-project references and only restore the specified project.
  --force                               Force all dependencies to be resolved even if the last restore was successful.
                                        This is equivalent to deleting project.assets.json.

我可以在 Ubuntu 上使用 dotnet CLI 将 C# DLL 包装到 NuGet 包中吗?还是我必须使用nuget CLI (apt-get install nuget) 来代替 Windows 上的方法?

【问题讨论】:

  • *.csproj.nuspec 文件是什么?它们不是两个不同的文件吗?
  • @PavelAnikhouski 是的,当然这是单独的文件。在*.csproj.nuspec 中,可以定义要包含在nuget 包中的DLL。问题是这是否也可以通过*.csproj 实现。然后我可以使用dotnet pack。否则我可能不得不改用nuget
  • 您可以直接在.cspoj 文件中定义属性,如此处所述Create and publish a package (dotnet CLI)
  • 你的意思是通过&lt;ItemGroup&gt;

标签: c# .net ubuntu dll nuget-package


【解决方案1】:

我会将其添加到 .csproj 文件中:

<ItemGroup>
    <Content Include="some.dll" PackageCopyToOutput="true">
        <pack>true</pack>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>

它不漂亮,但它有效。

【讨论】:

  • 我正在试验&lt;Content Include="lib\net5.0\Some.dll"&gt; &lt;Pack&gt;true&lt;/Pack&gt; &lt;PackagePath&gt;lib\$(TargetFramework)&lt;/PackagePath&gt; &lt;/Content&gt;。有什么区别?
  • 这行得通吗?我已经有一段时间了,从一个我知道有效的旧项目中获取了这个:-)。我在让它工作时遇到了一些麻烦,提供这个作为例子希望它可以节省你一些时间。
  • github中有很多使用这个的例子。我认为它有效,但我还不知道。
  • 似乎CopyToOutputDirectory 通常只有在需要提供static files and the like as part of the package 时才需要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多