自动构建 NuGet 包,包括引用的依赖项

我想要 运行本地/内部 NuGet 存储库。我想我已经找到了如何“重用”现有的 NuGet 包,包括在一个虚拟项目中使用 NuGet 和扫描包文件到 抓住我的本地缓存的 .nupkg文件,但是..。

如何从一个项目中创建一个 NuGet 包(.nupkg) ,自动包含 所有 dll依赖项,而不仅仅是那些通过 NuGet 抓取的依赖项?

具体来说:

  1. 创建解决方案
  2. 添加新项目
  3. 添加对各种 .dll文件/其他项目 < ——这是缺失的部分的引用
  4. 通过包管理器/cmdline/whatever 添加 NuGet 包
  5. 自动创建 .nupkg

根据我的发现,你应该做一些

  • 手动编辑 .csproj文件以添加 <BuildPackage>true</BuildPackage>以包含依赖项
  • 手动创建 .nuspec文件和手动 列出你的依赖项(相似?)
  • .nuspec文件上手动运行 nuget pack

但所有的东西都是手动的,这很愚蠢。即使是半自动的解决方案也很笨拙,或者说是半手动的:

I'll settle for something that automatically creates a .nuspec manifest from project references. Then theoretically that + the nuget build-event can be rolled up into a build-project/nuget package, which is what I really want to see.

108195 次浏览

Your point #3 (Add references to various .dll files/other projects <-- this is the missing part) really contains two different issues: (1) add references to various dll files, and (2) add references to other projects in the same solution.

Number (2) here has gotten some added support as of NuGet 2.5. You can add an option to include references to other projects in the same solution when creating a NuGet package for a project:

nuget pack projectfile.csproj -IncludeReferencedProjects
  • If projectfile.csproj references any other projects in your solution that also is exposed as NuGet packages, these projects' NuGet packages will be added as dependencies.
  • If it references projects in your solution that doesn't expose themselves as NuGet packages, their dlls will be included in this NuGet package.

As for (1), if you find yourself often adding dlls to your projects that aren't available as NuGet packages, you could just create your own (internal) NuGet packages with these files. If you then add these dlls as a NuGet package instead of the files directly, this NuGet package will be a dependency in your project's NuGet package.

For other Googlers, you can use this if you are using the NuGet.targets file to run NuGet Pack:

<Target Name="PrePackage" BeforeTargets="BuildPackage">
<PropertyGroup>
<BuildCommand>$(BuildCommand) -IncludeReferencedProjects</BuildCommand>
</PropertyGroup>
</Target>

Check this out!

The solution which I found is an extension for Visual Studio: https://visualstudiogallery.msdn.microsoft.com/fbe9b9b8-34ae-47b5-a751-cb71a16f7e96/view/Reviews

You simply add new project called NuGet Package: NuGet Package

Then you are adding interesting you projects to references and BOOOM !! All dependencies and file directories are automatically added. If you want to modify NuSpec data you click right at project and go to Properties, then modify what you want. Generated NuSpec and nupkg will be in folder obj of your new project. I hope it helps ;).

I found a well-written article on this topic. I have the same issue with certain packages that have a hierarchy of dependencies and up until now I've been uploading each as a separate NuGet package (what. a. waste. of. time)

I've just tested the solution found here: https://dev.to/wabbbit/include-both-nuget-package-references-and-project-reference-dll-using-dotnet-pack-2d8p

And after examining the NuGet package using NuGet Package Explorer, the DLLs produced by referenced projects are indeed present. I'm going to test by actually submitting this package to NuGet and testing it.

Here's my source in case it is helpful to you: https://github.com/jchristn/NuGetPackTest

And the test NuGet package: https://www.nuget.org/packages/NuGetPackTest/1.0.0

The solution appears to work well. I don't know what it's going to look like when there are layers of references, I'm sure it could get really hairy and really fast.

enter image description here

.csproj from NuGetPackTest library which references project TestLibrary (portions removed for brevity)

<Project Sdk="Microsoft.NET.Sdk">
 

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp3.1;net461</TargetFrameworks>
...
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>


<!-- added this line -->
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>


<ItemGroup>


<!-- modified this ProjectReference to include the children ReferenceOutputAssembly and IncludeAssets -->
<ProjectReference Include="..\TestLibrary\TestLibrary.csproj">
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
<IncludeAssets>TestLibrary.dll</IncludeAssets>
</ProjectReference>
</ItemGroup>


<!-- added this section -->
<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))"/>
</ItemGroup>
</Target>
  

</Project>

I solved this for my case by adding the whole TargetDir to the nuget package. Just add this to the .csproj :

<Target Name="IncludeAllFilesInTargetDir" AfterTargets="Build">
<ItemGroup>
<None Include="$(TargetDir)\**">
<Pack>true</Pack>
<PackagePath>tools</PackagePath>
</None>
</ItemGroup>
</Target>