如何通过 NuGet 包共享源代码以用于 .NET Core 项目
How to share source code via NuGet packages for use in .NET Core projects
我想让一小段源代码(例如帮助程序 classes)可用于 .NET Core 项目 (.csproj)。
在这一点上,我根据不同的博客文章和官方 nuget 文档,以多种不同的方式用 NuGet 打包了源代码。我使用了一个 nuspec 文件来控制我的源文件在 nuget 包中的最终位置,例如:
<files>
<file src="*.cs" target="content/LruCache" />
<file src="*.cs" target="contentFiles/cs/any/LruCache" />
</files>
我没有包含任何 msbuild 目标文件或安装脚本。
每当我将 NuGet 包安装到 .NET Core 项目 (https://docs.microsoft.com/en-us/dotnet/core/tools/csproj) 时,我根本就没有得到任何东西。我的项目中不会包含任何源文件。我为 .csproj(PrivateAssets 等)中的 <PackageReference/>
节点尝试了不同的设置,但没有成功。
这意味着可能吗?如果可以,应该怎么做?
背景:
这样做的原因是某种钻石问题,我们有项目 B 和 C 都使用 helper class A 和第三个项目 D 使用 B 和 C。
在这种情况下,当 B 和 C 中使用了不同(不兼容)版本的 A 时,我不想处理程序集版本冲突。
Is it meant to be possible at all? If so, how should it be done?
答案是肯定的。由于您测试的项目类型是 .net core。您应该使用 contentFiles
而不是 content
。 content
用于 packages.config。查看 Using the contentFiles element for content files and blog NuGet ContentFiles Demystified 了解更多详情。
所以你的 .nuspec
文件应该是:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MyTestCore</id>
<version>5.0.0</version>
<authors>TestContentFile</authors>
<owners>TestContentFile</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package Description</description>
<contentFiles>
<files include="any/any/Test.cs" buildAction="content" flatten="true" copyToOutput="true"/>
</contentFiles>
</metadata>
<files>
<file src="contentFiles/any/any/Test.cs" target="contentFiles/any/any/LruCache" />
</files>
</package>
nuget 包应该是这样的:
注意:当你创建一个新的包时,不要忘记删除C:\Users\<UserName>\.nuget\packages
文件夹中这个包的nuget缓存,否则,它总是安装旧包。
使用此方法,源文件将包含到您的项目中。
希望这对您有所帮助。
我想让一小段源代码(例如帮助程序 classes)可用于 .NET Core 项目 (.csproj)。 在这一点上,我根据不同的博客文章和官方 nuget 文档,以多种不同的方式用 NuGet 打包了源代码。我使用了一个 nuspec 文件来控制我的源文件在 nuget 包中的最终位置,例如:
<files>
<file src="*.cs" target="content/LruCache" />
<file src="*.cs" target="contentFiles/cs/any/LruCache" />
</files>
我没有包含任何 msbuild 目标文件或安装脚本。
每当我将 NuGet 包安装到 .NET Core 项目 (https://docs.microsoft.com/en-us/dotnet/core/tools/csproj) 时,我根本就没有得到任何东西。我的项目中不会包含任何源文件。我为 .csproj(PrivateAssets 等)中的 <PackageReference/>
节点尝试了不同的设置,但没有成功。
这意味着可能吗?如果可以,应该怎么做?
背景:
这样做的原因是某种钻石问题,我们有项目 B 和 C 都使用 helper class A 和第三个项目 D 使用 B 和 C。
在这种情况下,当 B 和 C 中使用了不同(不兼容)版本的 A 时,我不想处理程序集版本冲突。
Is it meant to be possible at all? If so, how should it be done?
答案是肯定的。由于您测试的项目类型是 .net core。您应该使用 contentFiles
而不是 content
。 content
用于 packages.config。查看 Using the contentFiles element for content files and blog NuGet ContentFiles Demystified 了解更多详情。
所以你的 .nuspec
文件应该是:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MyTestCore</id>
<version>5.0.0</version>
<authors>TestContentFile</authors>
<owners>TestContentFile</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package Description</description>
<contentFiles>
<files include="any/any/Test.cs" buildAction="content" flatten="true" copyToOutput="true"/>
</contentFiles>
</metadata>
<files>
<file src="contentFiles/any/any/Test.cs" target="contentFiles/any/any/LruCache" />
</files>
</package>
nuget 包应该是这样的:
注意:当你创建一个新的包时,不要忘记删除C:\Users\<UserName>\.nuget\packages
文件夹中这个包的nuget缓存,否则,它总是安装旧包。
使用此方法,源文件将包含到您的项目中。
希望这对您有所帮助。