问题描述
我尝试从 .NET 4.7.2 类库 (VS2017) 制作 NuGet 包,但生成的 NuGet 包令人惊讶地没有显示依赖项(这是一个错误).
I try to make a NuGet package from a .NET 4.7.2 class library (VS2017), but the resulting NuGet package surprisingly shows no dependencies (which is an error).
我的设置是这样的:
- 我的类库是 .NET Framework 4.7.2
- 我的类库使用另一个 NuGet 包(具有 依赖项).
- 我的类库在 .csproj 中使用 packageReferences
- 我的类库包含一个正确的 .nuspec 文件
- 我使用 nuget.exe 包 来创建包
- My class library is .NET Framework 4.7.2
- My class library uses another NuGet package (that has dependencies).
- My class library uses packageReferences in .csproj
- My class library contains a proper .nuspec file
- I use nuget.exe pack for creating the package
nuget.exe pack 命令应该自动 填充所需的依赖项 - 这也曾经是早期的情况(在另一个项目中).但是,当时我在我的类库中使用了 packages.config 而不是 packageReferences.这有什么改变吗?
The nuget.exe pack command should automatically fill in the needed dependencies - this also used to be the case earlier (in another project). However, at that time I used packages.config instead of packageReferences with my class library. Does that change anything?
发生了什么事?
如何强制系统在我的包中再次包含所需的依赖项?
注意事项:
该包是由我们的 TeamCity 构建服务器上的 MSBuild 脚本构建的(没有 VS2017).构建脚本调用 "nuget.exe restore" 和后来的 "nuget.exe pack" 作为其构建逻辑的一部分.
The package is built by a MSBuild script on our TeamCity build server (without VS2017). It's the build script that calls both "nuget.exe restore" and later "nuget.exe pack" as part of its build logic.
MSBuild 是 15.7 版
MSBuild is version 15.7
nuget.exe 是 4.6.2 版
nuget.exe is version 4.6.2
推荐答案
如何强制系统在我的包中再次包含所需的依赖项?
How can I force the system to again include the needed dependencies in my package?
这是一个关于 nuget pack 在使用 PackageReference 而不是 packages.config 时忽略依赖项的已知问题.
要解决此问题,您可以使用以下解决方法,NuGet 团队仍在积极努力改进此方案:
To resolve this issue, you can use the following workaround, and NuGet team are still actively working on improving this scenario:
打包您的 C# 类库,该类库通过以下方式管理您的依赖项PackageReference
在 csproj 本身中,
To package your C# Class Library which manages your dependencies via
PackageReference
in the csproj itself,
请添加参考NuGet.Build.Tasks.Pack
(https://www.nuget.org/packages/NuGet.Build.Tasks.打包/) 并运行msbuild/t:pack
从命令行.
please add a reference to
NuGet.Build.Tasks.Pack
(
https://www.nuget.org/packages/NuGet.Build.Tasks.Pack/) and run
msbuild /t:pack
from the command line.
我已经测试了这个解决方法,它工作正常.为确保此解决方法正常工作,我们需要注意以下要点:
I have test this workaround, it works fine. To make sure this workaround works fine, we need to pay attention to the following points:
- 需要将nuget包
NuGet.Build.Tasks.Pack
添加到项目中. - 需要添加属性
/p:PackageOutputPath="D:TesterFolder" -p:Authors=tester
- 使用命令
msbuild.exe/t:pack
,例如:msbuild.exe/t:pack "MyTestLibrary.csproj"/p:PackageOutputPath="D:TestFolder" -p:Authors=tester
- Need to add the nuget package
NuGet.Build.Tasks.Pack
to the project. - Need to add properties
/p:PackageOutputPath="D:TesterFolder" -p:Authors=tester
- Use the command
msbuild.exe /t:pack
, like:msbuild.exe /t:pack "MyTestLibrary.csproj" /p:PackageOutputPath="D:TestFolder" -p:Authors=tester
另外,如果你想使用.nuspec
文件来创建nuget包,你应该使用下面的.nuspec
文件:
Besides, if you want to use .nuspec
file to create the nuget package, you should use the following .nuspec
file:
<?xml version="1.0"?>
<package >
<metadata>
<id>MyTestLibrary</id>
<version>1.0.0</version>
<authors>Tester</authors>
<owners>Tester</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2018</copyright>
<tags>Tag1 Tag2</tags>
<dependencies>
<group targetFramework=".NETFramework4.7.2">
<dependency id="Microsoft.Owin" version="4.0.0" exclude="Build,Analyzers" />
</group>
</dependencies>
</metadata>
<files>
<file src="binDebugMyTestLibrary.dll" target="lib
et472MyTestLibrary.dll" />
</files>
</package>
然后我们可以使用 nuget.exe pack
来创建 nuget 包.但是,使用这种方法,我们必须在 .nuspec
文件中手动填写所需的依赖项.
Then we could use nuget.exe pack
to create the nuget package. But, using this method, we have to manually fill in the needed dependencies in the .nuspec
file.
希望这会有所帮助.
这篇关于NuGet 包显示没有依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!