如何以现代通用方式打包具有以下属性的.NET库?
这是一系列问题和答案,记录了我对现代NuGet包创作主题的研究结果,特别关注NuGet 3引入的变化。您可能也对一些相关问题感兴趣:
- How to package a .NET Framework library?
- How to package a .NET library targeting the Universal Windows Platform?
- How to package a portable .NET library targeting .NET Core?
- How to package a multi-architecture .NET library that targets the Universal Windows Platform?
- How to package a .NET library that targets the Universal Windows Platform and depends on Visual Studio extension SDKs?
答案 0 :(得分:2)
此答案以principles used to package .NET Framework libraries,principles used to package Universal Windows Platform libraries和principles used to package portable libraries为基础。首先阅读链接的答案,以便更好地理解以下内容。
要提供所描述的一组平台,您需要将解决方案相应地构建到多个类库项目中:
您需要实现以下NuGet包结构:
\---lib
+---dotnet
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
|
+---net46
| MyDotNetLibrary.dll
| MyDotNetLibrary.pdb
| MyDotNetLibrary.XML
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
|
\---uap10.0
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
| MyUwpLibrary.dll
| MyUwpLibrary.pdb
| MyUwpLibrary.pri
| MyUwpLibrary.XML
|
\---MyUwpLibrary
HashControl.xaml
MyUwpLibrary.xr.xml
如果您已熟悉上面列出的其他答案,那么您应该对此比较熟悉。唯一特殊的部分是便携式库存在三个副本,因为它的内容将提供给所有目标平台。
可以使用基于以下模板的nuspec文件来实现所需的包结构:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata minClientVersion="3.2">
<id>Example.MyMultiSurfaceLibrary</id>
<version>1.0.0</version>
<authors>Firstname Lastname</authors>
<description>Example of a multi-platform library that exposes different API surfaces to .NET 4.6 and UWP and also includes a portable component.</description>
<dependencies>
<!-- UWP has more dependencies than other platforms (Newtonsoft.Json). -->
<group targetFramework="uap10.0">
<dependency id="Newtonsoft.Json" version="8.0.1" />
<dependency id="System.Linq" version="4.0.0" />
<dependency id="System.Numerics.Vectors" version="4.1.0" />
<dependency id="System.Resources.ResourceManager" version="4.0.0" />
<dependency id="System.Runtime" version="4.0.20" />
</group>
<!-- All other platforms - just the dependencies of the portable library here. -->
<group>
<dependency id="System.Linq" version="4.0.0" />
<dependency id="System.Numerics.Vectors" version="4.1.0" />
<dependency id="System.Resources.ResourceManager" version="4.0.0" />
<dependency id="System.Runtime" version="4.0.20" />
</group>
</dependencies>
</metadata>
<files>
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\net46" />
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\uap10.0" />
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\dotnet" />
<file src="..\..\MyDotNetLibrary\bin\Release\MyDotNetLibrary.*" target="lib\net46" />
<!-- Double wildcard also ensures that the subdirectory is packaged. -->
<file src="..\..\MyUwpLibrary\bin\Release\MyUwpLibrary**" target="lib\uap10.0" />
</files>
</package>
请注意,定义了两组独立的依赖项:一个通用组和一个特定于Universal Windows Platform的依赖项,因为UWP库对Newtonsoft.Json包具有额外的依赖性。您可以将相同的模式扩展到具有特定于平台的依赖项的任意数量的平台。
就是这样 - 这个NuGet包现在可以安装到.NET Framework 4.6项目,通用Windows平台项目和面向兼容API表面的可移植库项目中。可移植库的功能将在所有平台上导出,特定于平台的库也可在适当的平台上使用。
请记住在创建NuGet包之前使用Release配置构建解决方案。
示例库和相关的打包文件为available on GitHub。与此答案对应的解决方案是MultiSurfaceLibrary。