MS在单个NuGet程序包中构建多个dll [英] MSBuild multiple dll in a single NuGet package

查看:57
本文介绍了MS在单个NuGet程序包中构建多个dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个项目的Visual Studio 2017解决方案:

I have a Visual Studio 2017 solution that contains two projects:

Foo.csproj
Foo.Core.csproj

这两个项目都针对多个框架:net452;netstandard1.2

Both of these projects target multiple frameworks: net452;netstandard1.2

Foo.csproj包含对Foo.Core.csproj的项目引用:

Foo.csproj includes a project reference to Foo.Core.csproj:

<ItemGroup>
    <ProjectReference Include="..\Foo.Core\Foo.Core.csproj" />
</ItemGroup>

当我为Foo.csproj生成NuGet程序包时,我希望nupkg文件包含这两个程序集.

When I generate a NuGet package for Foo.csproj, I want the nupkg file to include both of these assemblies.

当前正在发生的事情是,创建的NuGet程序包具有Foo.dll,然后具有对Foo.Core的NuGet依赖关系(不存在).

What is currently happening is that the NuGet package that gets created has Foo.dll and then a NuGet dependency on Foo.Core (which doesn't exist).

如何使用msbuild生成一个包含两个程序集的单个NuGet程序包?

How can I generate a single NuGet package using msbuild that will include both assemblies?

作为参考,这是我当前正在使用的命令(无法正常运行):

For reference this is the command I am currently using (which is not working how I want it to):

msbuild /p:restore,pack Foo.csproj

推荐答案

NuGet当前不直接支持此功能.您可以按照此GitHub问题进行更新.

This is currently not directly supported by NuGet out of the box. You can follow this GitHub issue for updates.

但是,有几种方法可以创建这样的NuGet包.

However, there are a few ways to create such NuGet package.

  1. 使用"Nugetizer 3000"

这是一个新开发的工具,用于从项目中构建NuGet软件包,并且可以通过安装NuGet.Build.Packaging nuget软件包来工作.您可以在其GitHub Wiki页面上找到一些文档,但是由于它是一个非常新的项目,因此关于它的文档或社区知识还很少(!)(但是开发它的团队非常有帮助,您可以归档GitHub问题,如果您卡住了.

This is an newly developed tool to build NuGet packages from projects and works by installing the NuGet.Build.Packaging nuget package. You can find some documentation on it on its GitHub wiki page but since it is a very new project, there isn't much documentation or community knowledge around it yet(!) (but the team developing it is very helpful, you could file GitHub issues if you get stuck).

  1. 在项目中添加自定义目标(2.0.0工具/VS 2017 15.3+):在csproj中创建一个项目,其中将包括所引用项目的输出DLL

此方法非常棘手,因为它依赖于包目标使用的内部MSBuild项目.它的工作方式是先将<ProjectReference>标记为不被创建的nuget包引用,如下所示:

This approach is very hacky as it relies on an internal MSBuild item that the pack targets use. It works by first marking the <ProjectReference> to not be referenced from the created nuget package like this:

<ProjectReference Include="..\libA\libA.csproj" PrivateAssets="All"/>

然后,您可以将其添加到项目中,以将生成的libA.dll包括在nuget包中:

Then you can add this to the project to include the generated libA.dll in the nuget package:

<PropertyGroup>
  <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);IncludeP2PAssets</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="IncludeP2PAssets">
  <ItemGroup>
    <BuildOutputInPackage Include="$(OutputPath)\testprivatelib.dll" />
  </ItemGroup>
</Target>

请注意,这要求您将引用项目的所有<PackageReference>项添加到生成包的项目中,因为由于您实际上禁用了传递引用行为,因此生成的包中将缺少这些项.

Note that this requires you to add all the <PackageReference> items of the referenced project to the project you generate the package from since they would be missing from the generated package since you effectively disabled the transitive reference behaviour.

  1. 创建自定义.nuspec文件

在撰写本文时,这可能是最受支持"的方式,也是最复杂的方式. NuGet允许您通过在项目中设置<NuspecFile>属性以及<NuspecProperties>属性来禁用生成的.nuspec文件的自动生成和文件的自动收集,该属性允许您传递替换令牌以解析文件.

At the time of writing, this is probably the most "supported" way, but also the most complex. NuGet allows you to disable the automatic generation of the resulting .nuspec file and automatic collection of files by setting the <NuspecFile> property in your project, along with a <NuspecProperties> property that allows you to pass replacement tokens for parsing the .nuspec file.

这可以通过如下方式修改项目文件来实现:

This works by modifying the project file like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
    <NuspecFile>$(MSBuildThisFileDirectory)$(MSBuildProjectName).nuspec</NuspecFile>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\LibB\LibB.csproj" />
  </ItemGroup>

  <Target Name="SetNuspecProperties" BeforeTargets="GenerateNuspec">
    <PropertyGroup>
      <NuspecProperties>$(NuspecProperties);id=$(AssemblyName)</NuspecProperties>
      <NuspecProperties>$(NuspecProperties);config=$(Configuration)</NuspecProperties>
      <NuspecProperties>$(NuspecProperties);version=$(PackageVersion)</NuspecProperties>
      <NuspecProperties>$(NuspecProperties);description=$(Description)</NuspecProperties>
      <NuspecProperties>$(NuspecProperties);authors=$(Authors)</NuspecProperties>
    </PropertyGroup>
  </Target>
</Project>

这将自动查找与项目名称相同的.nuspec文件(somelib.csproj => somelib.nuspec),并将一些属性传递给该文件.这些属性是在目标中创建的,以便能够访问完全解析的默认属性,例如PackageVersion.

This will automatically look for a .nuspec file with the same name as the project (somelib.csproj => somelib.nuspec) and pass some properties along to it. The properties are created in a target in order to be able to access fully resolved and defaulted properties like PackageVersion.

.nuspec文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <authors>$authors$</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <dependencies>
      <group targetFramework=".NETStandard1.4">
        <dependency id="NETStandard.Library" version="1.6.1" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="bin\$config$\netstandard1.4\*.dll" target="lib\netstandard1.4\" />
  </files>
</package>

请注意,您必须将所有引用的NuGet软件包作为<dependency>元素添加到.nuspec文件中,因为它们不再由项目文件中的<PackageReference>项自动生成.请参考 NuSpec参考有关更多详细信息.

Note that you must add all referenced NuGet packages as a <dependency> element in the .nuspec file since these are no longer automatically generated from the <PackageReference> items in your project file. Refer to the NuSpec Reference for more details.

我最近在GitHub上创建了一个示例项目,该示例项目展示了自定义.nuspec的使用文件就是为了这个目的.

I have recently created an example project on GitHub demonstrating the use of a custom .nuspec file for exactly this purpose.

这篇关于MS在单个NuGet程序包中构建多个dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆