简体   繁体   中英

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

I'm using VS2019 community edition. 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.

How can I change this so Y will be added as DLL in X's package?

I tried to add the following to X's project (.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.

  • UPDATE:

Also if Y has another Nuget dependency, it should be considered in X without referencing it directly in X.

I tried the following:
In X's project:

  • Go to Dependencies->Projects.
  • Right click on Y's project then Properties .
  • Set Private Assets to All
    Now this will add Y's dll file in the lib folder in the X's nuspec file.
    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.

Finally I settled down on this:
Just reference Y, generate package for it too and live with it:'(

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

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. 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. 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. You can open the X.nupkg file and will see like this under X.nuspec file:

在此处输入图像描述

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.

Use zip to open nuget compressed package, then open X.nuspec file and delete these node:

<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 .

2) Or you should create a custom nuspec file based on our needs and rules manually to guidance the nuget pack process.

Try to use this nuspec file:

<?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.

So far , Nuget does not have the feature to ask the main project whether to use the dependency package dll of the referenced project.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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