NuGet packages project dependencies as nuget dependencies

一曲冷凌霜 提交于 2021-02-11 12:52:24

问题


I have 3 class library projects (all .NET Standard 2.0) that are all in the same solution. I want to package them into a single nuget and use the code in other repos.

However, when I package them into a NuGet package two of them are added as a nuget dependency to the third one, instead of being directly referenced as dlls.

Here is an example of my setup.

The 3 projects - A.csproj, B.csproj, C.csproj (All Class Libraries, All .NET Standard 2.0)

A is set as a startup project and references B and C

B has a reference to C

C has no references to the other two (it only references 2 3rd party nugets)

When I package my solution into a nuget package, the nuspec file has a <dependencies> group that has all nuget references from my project (correct) along with 2 dependencies for projects B and C with versions 1.0.0 (incorrect)

I am not sure what is causing nuget to behave like this (I imagine its by design) but I cannot wrap my head around to fix the issue.

What I want is for projects B and C to be packaged as DLLs to project A and not as seprate packages.


回答1:


I am not sure what is causing nuget to behave like this (I imagine its by design) but I cannot wrap my head around to fix the issue.

Yes, this behave is by design. When we pack the .net core/.NET Standard, the PrivateAssets metadata tags control whether dependency assets flow to the parent project. If the value is set to All, this dependency assets would not flow to the parent project. In other words, projects B and C will not added to the package as dependencies.

Check Controlling dependency assets for more info.

What I want is for projects B and C to be packaged as DLLs to project A and not as seprate packages.

Just like what I said in the first question, we could use PrivateAssets metadata tags control whether dependency assets flow to the parent project, but the PrivateAssets metadata tags does not package project B and C as project A DLLs.

If you want projects B and C to be packaged as DLLs to project A and not as separate packages, we need to use .nuspec file to include them manually.

The .nuspec like:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>TestDemo</id>
    <version>1.0.0</version>
    <authors>Tester</authors>
    <owners>Tester</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>TestDemo</description>
    <releaseNotes>Summary of changes made in this release of the package.    </releaseNotes>
    <copyright>Copyright 2017</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
  <files>
    <file src="bin\debug\projectA.dll" target="lib\.netstand2.0" />
    <file src="<Path>\projectB.dll" target="lib\.netstand2.0" />
    <file src="<Path>\projectC.dll" target="lib\.netstand2.0" />
  </files>
</package>

Check this thread for more details.




回答2:


You have to update the version for dependencies in .nuspec manually/ or through some pre/post build command/script. If you need to do this for a lot of projects then I'll suggest to write a script and run on pre/post build event.

The .nuspec can not detect the version for dependencies itself.




回答3:


It looks like you want to generate the DLLs to B and C, then copy them into a resource folder for A which is referenced in A's nuspec file. That is, you are only packaging A, which happens to include DLLs for B and C, so the build of B and C, and adding of there DLLs to A, should happen separately before the packaging of A.



来源:https://stackoverflow.com/questions/52814421/nuget-packages-project-dependencies-as-nuget-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!