[英]In VS2019 nuget doesn't consider referenced projects as lib, but as nuget packages
我正在使用 VS2019 社區版。 I have two .net core projects X, Y, X is referencing Y, and I want to package X as a Nuget package, I'm using the package feature in VS2019. when I try to add X's Nuget package in another project it searches for Y as a Nuget package and not as DLL should be in X.
如何更改此設置,以便在 X 的 package 中將 Y 添加為 DLL?
我嘗試將以下內容添加到 X 的項目(.csproj)中:
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>
But I still get the same result, nuget.exe tries to restore Y as Nuget package not as DLL comes with X's package.
此外,如果 Y 有另一個 Nuget 依賴項,則應在 X 中考慮它,而不直接在 X 中引用它。
我嘗試了以下方法:
在 X 的項目中:
Properties
。Private Assets
設置為All
lib
文件夾中添加 Y 的nuspec
文件。nuspec
文件中作為依賴項被提及。 最后我確定了這一點:
只需參考 Y,也為它生成 package 並使用它:'(
在 VS2019 中 nuget 不將引用的項目視為 lib,而是將其視為 nuget 包
Actually , the new nuget feature which you used and also PackagePath="lib" function will treat the dll as a nuget dependency rather than an assembly dll.
如果您只是希望Y成為一個程序集 DLL 而不是 nuget package,這些方法將不起作用。 這些方法我都試過了,真的讓我很掙扎。
經過深入研究,我發現問題是dotnet pack會將引用的項目作為nuget pacakge自動添加到主nuget ZEFE90A8E604A7C840E88D03A67F6B7D中。 因為dotnet pack
會根據其規則自動生成一個Nuspec
文件給package項目,默認將引用的項目視為Nuget package。 您可以打開X.nupkg
文件,並在X.nuspec
文件下看到如下所示:
但是,在這種打包機制中,我們無權更改規則。
建議
1)在VS2019中使用Pack Button創建X.nupkg
文件時,請手動對X.nupkg
進行一些修改。
使用zip打開 nuget 壓縮 package,然后打開X.nuspec
文件並刪除這些節點:
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="Y" version="1.0.0" exclude="Build,Analyzers" />
</group>
</dependencies>
保存更改,然后使用修改后的X.nupkg
。
2)或者您應該根據我們的需要和規則手動創建自定義 nuspec 文件來指導 nuget 打包過程。
嘗試使用這個nuspec
文件:
<?xml version="1.0"?>
<package >
<metadata>
<id>xxx</id>
<version>1.0.0</version>
<title>xx</title>
..................
</metadata>
<files>
<file src="bin\xxx\xxx\Y.dll" target="lib\xxx(targetframework)"/>
</files>
</package>
此外,如果 Y 有另一個 Nuget 依賴項,則應在 X 中考慮它,而不直接在 X 中引用它。
到目前為止,Nuget 沒有詢問主項目是否使用引用項目的依賴 package dll 的功能。
並且來自引用項目的依賴項始終存在於主項目中。
另外,如果您仍然需要這些問題,您可以在我們的團隊論壇上報告這些問題,希望團隊仔細檢查並給您滿意的答復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.