如何使用 Visual Studio 2017 创建 Nuget 包 [英] How do I create a Nuget package with Visual Studio 2017

查看:60
本文介绍了如何使用 Visual Studio 2017 创建 Nuget 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio 2017 构建一个 .net 4.5 类库 dll.

I am building a .net 4.5 class library dll using Visual Studio 2017.

我想把这个dll和一些相关的javascript、html、css等文件夹打包成一个NuGet包.

I wish to package up this dll, and some related folders of javascript, html, css and suchlike files as a NuGet package.

不幸的是,我不知道从哪里开始 - 我能找到的关于创建 NuGet 包的唯一说明适用于 Visual Studio 2015.我被告知 NuGet 包含在 Visual Studio 2017 中,但是如果我打开 Developer Command Prompt for VS 2017,然后输入 Nuget,找不到命令.

Unfortunately, I don't know where to start - the only instructions I can find for creating NuGet packages apply to Visual Studio 2015. I am told NuGet is included in Visual Studio 2017, but if I open a Developer Command Prompt for VS 2017, and type Nuget, the command is not found.

我的印象是 Visual Studio 2017 可以在 .Net 标准项目中构建 NuGet 包,但不能在其他类型的项目中构建.

I get the impression that Visual Studio 2017 can build NuGet packages in .Net standard projects, but not in other types of project.

我应该回到 Visual Studio 2015 还是什么?

Am I supposed to go back to Visual Studio 2015, or what?

推荐答案

这里有几个不同的问题:

There's a few distinct questions here:

关于 VS 集成:在 VS

2017 NuGet 通常是一个可以单独更新的扩展.在 VS 2017 中,扩展紧密集成并通过对 VS 本身的更新进行更新.这从未包括通常用于打包和推送包的命令行 nuget.exe - 该命令行客户端可从 NuGet 的下载页面.

About the VS integration: In VS < 2017 NuGet was usually an extension that could be updated individually. In VS 2017 the extension is tightly integrated and updated via updates to VS itself. This never included the command-line nuget.exe that is usually used to pack and push packages - this command line client is available from NuGet's download page.

打包 .NET 项目的经典"方法仍然有效,并记录在 nuget 的文档页面,特别重要的是 "从 Visual Studio 项目创建 .nuspec 文件...".

The "classic" approaches to packaging .NET projects still work and are documented in nuget's documentation page, especially important is the section "Creating the .nuspec file … from a Visual Studio project".

VS 2017 还引入了一种使用从 .NET Core 工具演变而来的.NET Sdk"的新型项目.这些项目与 NuGet 集成,可以由 VS 打包,也可以直接从 MSBuild/dotnet pack 打包.此项目类型还可用于创建 .NET Framework NuGet 包.但是,VS 中没有模板,因为未实现可用于经典 .NET 项目的某些功能(例如,用于 xaml、edmx 的设计器).但是对于大多数逻辑库,您可以创建一个 .NET Standard 项目并编辑 csproj 文件进行更改

VS 2017 also introduces a new type of projects that use the ".NET Sdk" that evolved from the .NET Core tooling. These projects are integrated with NuGet and can be packed by VS and directly from MSBuild / dotnet pack. This project type can also be used to create .NET Framework NuGet packages. However, there isn't a template in VS since some features available for classic .NET projects aren't implemented (e.g. Designers for xaml,edmx). But for most logic libraries you can create a .NET Standard project and edit the csproj file to change

<TargetFramework>netstandard1.6</TargetFramework>

<TargetFramework>net461</TargetFramework>

因此该项目将面向 .NET 4.6.1(其他版本可能).该项目将具有与 .NET Standard 和 .NET Core 项目相同的集成打包功能.您可以按照指南 "创建 .NET 标准包使用 Visual Studio 2017",但在创建项目文件后对其进行更改.

so the project will target .NET 4.6.1 (other versions possible). This project will have the same integrated packing functionality as .NET Standard and .NET Core projects. You can follow the Guide "Create .NET Standard Packages with Visual Studio 2017" but perform that change to the project file after creating it.

为了将项目包含在包中,您可以使用以下元数据:

In order to include items into the package, you can use the following metadata:

<ItemGroup>
  <Content Include="**\*.txt" Pack="true" />
</ItemGroup>

这会将文件放入生成的 nuget 中的 contentcontentFiles 目录中.当生成的包通过 ProjectReference 使用时,contentFile 需要一个额外的元数据属性,以确保引用项目在构建时将其复制到其输出:

This will put the files into both a content and contentFiles directory in the resulting nuget. When the resulting package is consumed via a ProjectReference, the contentFile needs an additional metadata attribute to make sure that the referencing project copies it to its output on build:

<ItemGroup>
  <Content Include="**\*.txt" Pack="true" PackageCopyToOutput="true" />
</ItemGroup>

但是,此属性仅在即将发布的 VS 2017 15.3 更新/.NET Core SDK 1.1/2.0(在撰写本文时尚未发布)中受支持.

This property however is only supported in the upcoming VS 2017 15.3 update / .NET Core SDK 1.1/2.0 (not yet released at the time of writing).

这篇关于如何使用 Visual Studio 2017 创建 Nuget 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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