C# nuget 打包 csproj 与 nuspec 之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14797525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:01:16  来源:igfitidea点击:

Differences between nuget-packing a csproj vs. nuspec

c#.netnuget

提问by Matthias

Recently, I started to pack nuget packages out of my several projects. First I started with the Package Explorer application. It is a nice tool, but it's less useful if you do continuous integration. Then I looked into specifying the nuspectemplate file, and passing changing data, e.g. version number, as command line arguments. Later, I wondered how to define the nuget package dependencies. As it turns out, the nuget.exe already does this based on the package.configif you specify a csproj. Moreover, it extracts relevant data like Author, Version, Copyright right from the assembly info. What I'm missing right now is the ability to specify a licenseUrl in the command line. But I wanted the question to be more generic. And so I'm asking:

最近,我开始从我的几个项目中打包 nuget 包。首先,我从 Package Explorer 应用程序开始。这是一个不错的工具,但如果您进行持续集成,则它的用处不大。然后我研究了指定nuspec模板文件,并将更改的数据(例如版本号)作为命令行参数传递。后来,我想知道如何定义nuget包依赖项。事实证明,如果您指定csproj,nuget.exe 已经基于package.config执行此操作。此外,它还从程序集信息中提取相关数据,如作者、版本、版权。我现在缺少的是在命令行中指定 licenseUrl 的能力。但我希望这个问题更笼统。所以我问:

What is the prefered way to pack nuget packages?

打包 nuget 包的首选方法是什么?

采纳答案by Xavier Decoster

Here's a little-known fact: you can combine both! Target a csproj file, and make sure there's a nuspec file in the same directory with the same name as the csproj file. NuGet will merge the two during package creation.

这是一个鲜为人知的事实:您可以将两者结合起来!定位一个 csproj 文件,并确保在同一目录中有一个与 csproj 文件同名的 nuspec 文件。NuGet 将在包创建期间合并两者。

So in short: target <ProjectName>.csproj, optionally add a corresponding tokenized<ProjectName>.nuspecfile to be used as metadata by NuGet.exe.

简而言之: target <ProjectName>.csproj,可选择添加相应的标记化<ProjectName>.nuspec文件,以用作 NuGet.exe 的元数据。

It saves you from managing output location, dependencies, version, and other stuff that can be derived from the project.

它使您无需管理输出位置、依赖项、版本和其他可从项目派生的内容。

回答by manojlds

For simple packages you can directly create the packages off .csprojor .vbproj. But for more advance packages, especially when you need to pull in custom files into your package, you need to use .nuspec. I usually start off with the csproj and move to nuspec as needed. You can always get the nuspec using the command nuget specon the csproj.

对于简单的包,您可以直接创建包 off.csproj.vbproj。但是对于更高级的包,尤其是当您需要将自定义文件拉入包时,您需要使用.nuspec. 我通常从 csproj 开始,然后根据需要转向 nuspec。您始终可以使用 csproj 上的命令nuget spec获取 nuspec。

https://docs.nuget.org/create/creating-and-publishing-a-package

https://docs.nuget.org/create/creating-and-publishing-a-package

You can specify any of the properties including licenseUrlusing the Propertiesparameter to nuget pack

您可以指定任何属性,包括licenseUrl使用Properties参数nuget pack

nuget pack -properties licenseUrl=http://blah

回答by truepudding

With a .csproj for Visual Studio 2017, you don't need a .nuspec file. You can actually add the values directly to your csproj and it will pick them up.

对于 Visual Studio 2017 的 .csproj,您不需要 .nuspec 文件。您实际上可以将值直接添加到您的 csproj 中,它会选择它们。

Right click the project in Visual Studio, Edit xxxxx.csproj. Notepad works fine too.

在 Visual Studio 中右键单击项目,编辑 xxxxx.csproj。记事本也可以正常工作。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.1</Version>
    <authors>Subtracts</authors>
    <TargetFrameworks>netstandard1.6;net452</TargetFrameworks>
    <AssemblyName>Checkout.net</AssemblyName>
    <PackageId>Checkout.net</PackageId>

...

</Project>

p.s. Since I don't have sufficient reputation to comment, I am leaving an answer instead of a comment on Xavier's answer. :)

ps 由于我没有足够的声誉来发表评论,因此我将留下答案而不是对 Xavier 的答案发表评论。:)

回答by Luke Puplett

With .NET Core as of February 2018 you'll need to supply a .nuspecfile for any more than the basic spec file properties.

对于 2018 年 2 月的 .NET Core,您需要为.nuspec基本规范文件属性以外的任何内容提供文件。

But the dotnet packcommand will not use the .nuspecfile unless you add <NuspecFile>relative path to nuspec</NuspecFile>to the .csprojfile.

但是除非您添加到文件中,否则该dotnet pack命令不会使用该文件。.nuspec<NuspecFile>relative path to nuspec</NuspecFile>.csproj

See https://github.com/dotnet/cli/issues/2170

https://github.com/dotnet/cli/issues/2170

Most packages can now be made without a .nuspecfile. The thing to watch is the dependencies. You may need to add a PrivateAssetselement to some that are tools, like msbumpand um, SpecFlow maybe.

大多数包现在可以在没有.nuspec文件的情况下制作。需要注意的是依赖关系。您可能需要向PrivateAssets一些工具添加一个元素,比如msbump和嗯,可能是 SpecFlow。

<PackageReference Include="msbump" Version="2.3.2">
  <PrivateAssets>all</PrivateAssets>
</PackageReference>

This stops this package dependency "flowing" to the dependencies of your package.

这会阻止这个包依赖“流动”到你的包的依赖。

Also worth reading about specifying versions in the most flexible way.

关于以最灵活的方式指定版本也值得一读。

https://docs.microsoft.com/en-us/nuget/consume-packages/dependency-resolution#floating-versions

https://docs.microsoft.com/en-us/nuget/consume-packages/dependency-resolution#floating-versions

And range syntax.

和范围语法。

https://docs.microsoft.com/en-us/nuget/reference/package-versioning#references-in-project-files-packagereference

https://docs.microsoft.com/en-us/nuget/reference/package-versioning#references-in-project-files-packagereference