简体   繁体   English

在 VS2019 中 nuget 不将引用的项目视为 lib,而是将其视为 nuget 包

[英]In VS2019 nuget doesn't consider referenced projects as lib, but as nuget packages

I'm using VS2019 community edition.我正在使用 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. 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. 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.

How can I change this so Y will be added as DLL in X's package?如何更改此设置,以便在 X 的 package 中将 Y 添加为 DLL?

I tried to add the following to X's project (.csproj):我尝试将以下内容添加到 X 的项目(.csproj)中:

  <PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>
  <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
    <ItemGroup>
      <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;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. But I still get the same result, nuget.exe tries to restore Y as Nuget package not as DLL comes with X's package.

  • UPDATE:更新:

Also if Y has another Nuget dependency, it should be considered in X without referencing it directly in X.此外,如果 Y 有另一个 Nuget 依赖项,则应在 X 中考虑它,而不直接在 X 中引用它。

I tried the following:我尝试了以下方法:
In X's project:在 X 的项目中:

  • Go to Dependencies->Projects. Go 到依赖项-> 项目。
  • Right click on Y's project then Properties .右键单击 Y 的项目,然后单击Properties
  • Set Private Assets to AllPrivate Assets设置为All
    Now this will add Y's dll file in the lib folder in the X's nuspec file.现在这将在 X 的 nuspec 文件的lib文件夹中添加 Y 的nuspec文件。
    This will work fine if Y doesn't depend on any other Nuget packages that X doesn't know about, because those packages will not be mentioned as dependencies in X's nuspec file.如果 Y 不依赖于 X 不知道的任何其他 Nuget 包,这将正常工作,因为这些包不会在 X 的nuspec文件中作为依赖项被提及。

Finally I settled down on this:最后我确定了这一点:
Just reference Y, generate package for it too and live with it:'(只需参考 Y,也为它生成 package 并使用它:'(

In VS2019 nuget doesn't consider referenced projects as lib, but as nuget packages在 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. 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.

If you simply want Y to be an Assembly DLL rather than a nuget package, these methods won't work.如果您只是希望Y成为一个程序集 DLL 而不是 nuget package,这些方法将不起作用。 I have tried these methods, and it really made me struggling.这些方法我都试过了,真的让我很挣扎。

After a deep research , I found that the problem is that dotnet pack will add the referenced project as a nuget pacakge into the main nuget package automatically.经过深入研究,我发现问题是dotnet pack会将引用的项目作为nuget pacakge自动添加到主nuget ZEFE90A8E604A7C840E88D03A67F6B7D中。 Because dotnet pack will automatically generates an Nuspec file to package the project according to its rules, which treat the referenced project as a Nuget package by default.因为dotnet pack会根据其规则自动生成一个Nuspec文件给package项目,默认将引用的项目视为Nuget package。 You can open the X.nupkg file and will see like this under X.nuspec file:您可以打开X.nupkg文件,并在X.nuspec文件下看到如下所示:

在此处输入图像描述

However , in this pack mechanism, we do not have the right to change the rule.但是,在这种打包机制中,我们无权更改规则。

Suggestion建议

1) when you create the X.nupkg file using Pack Button in VS2019 , please do some changes to X.nupkg manually. 1)VS2019中使用Pack Button创建X.nupkg文件时,请手动对X.nupkg进行一些修改。

Use zip to open nuget compressed package, then open X.nuspec file and delete these node:使用zip打开 nuget 压缩 package,然后打开X.nuspec文件并删除这些节点:

<dependencies>
      <group targetFramework=".NETStandard2.0">
        <dependency id="Y" version="1.0.0" exclude="Build,Analyzers" />
      </group>
</dependencies>

Save the changes and then use the modified X.nupkg .保存更改,然后使用修改后的X.nupkg

2) Or you should create a custom nuspec file based on our needs and rules manually to guidance the nuget pack process. 2)或者您应该根据我们的需要和规则手动创建自定义 nuspec 文件来指导 nuget 打包过程。

Try to use this nuspec file:尝试使用这个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>

Also if Y has another Nuget dependency, it should be considered in X without referencing it directly in X.此外,如果 Y 有另一个 Nuget 依赖项,则应在 X 中考虑它,而不直接在 X 中引用它。

So far , Nuget does not have the feature to ask the main project whether to use the dependency package dll of the referenced project.到目前为止,Nuget 没有询问主项目是否使用引用项目的依赖 package dll 的功能。

And the dependencies from the referenced projects always exist in the main project.并且来自引用项目的依赖项始终存在于主项目中。

Besides , if you still want these, you could report these problems onour Team Forum and I hope the Team will check them carefully and give you a satisfactory reply.另外,如果您仍然需要这些问题,您可以在我们的团队论坛上报告这些问题,希望团队仔细检查并给您满意的答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM