我想使用
nuget pack
命令(对于遗留的非 sdk 项目),同时能够更改包内的描述和其他元数据。在 this 问题的评论中,我被告知我需要使用 nuspec 文件来实现此目的。
据我所知,无法根据项目文件生成 nuspec 文件(
nuget spec
生成空模板),因此我应用了一个小解决方法(使用 geenRating 包两次),请参阅下面的 shell 命令:
在此之前,我只创建了一个空的.net框架类库并使用了Newtonsoft包。
PS C:\test\TestLibrary\TestLibrary> ls
Directory: C:\test\TestLibrary\TestLibrary
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 3/7/2024 4:04 PM bin
d----- 3/7/2024 4:04 PM obj
d----- 3/7/2024 4:04 PM Properties
-a---- 3/7/2024 4:04 PM 194 Class1.cs
-a---- 3/7/2024 4:05 PM 144 packages.config
-a---- 3/7/2024 4:07 PM 2565 TestLibrary.csproj
Packages.config:
PS C:\test\TestLibrary\TestLibrary> Get-Content .\packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net452" />
</packages>
.csproj 文件的内容是默认的,有以下 2 个更改:
<PropertyGroup>
..
<Description>SomeText</Description>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
现在我想通过
nuget pack
和项目文件生成包。我知道我无法用这种方式指定 description
,因此它会产生以下输出:
PS C:\test\TestLibrary\TestLibrary> nuget pack .\TestLibrary.csproj
Attempting to build package from 'TestLibrary.csproj'.
MSBuild auto-detection: using msbuild version '16.11.2.50704' from 'c:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin'.
Packing files from 'C:\test\TestLibrary\TestLibrary\bin\Debug'.
Found packages.config. Using packages listed as dependencies
WARNING: NU5115: Description was not specified. Using 'Description'.
WARNING: NU5115: Author was not specified. Using '%USER%'.
The package TestLibrary.1.0.0 is missing a readme. Go to https://aka.ms/nuget/authoring-best-practices/readme to learn why package readmes are important.
Successfully created package 'C:\test\TestLibrary\TestLibrary\TestLibrary.1.0.0.nupkg'.
包装上没有预期的
description
。
现在,我执行以下操作,将包内生成的 nuspec 文件复制到项目文件目录中:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>TestLibrary</id>
<version>1.0.0</version>
<title>TestLibrary</title>
<authors>%USER%</authors>
<description>Description</description>
<copyright>Copyright © 2024</copyright>
<dependencies>
<group targetFramework=".NETFramework4.5.2">
<dependency id="Newtonsoft.Json" version="13.0.3" />
</group>
</dependencies>
</metadata>
</package>
手动更新描述为:
<description>ChangedDescription</description>`
未应用其他更改。
然后尝试生成一个新的包,但不是项目文件,我提供更新的 nuspec nuspec:
PS C:\test\TestLibrary\TestLibrary> nuget pack .\TestLibrary.nuspec
Attempting to build package from 'TestLibrary.nuspec'.
WARNING: NU5100: The assembly 'bin\Debug\Newtonsoft.Json.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
WARNING: NU5100: The assembly 'bin\Debug\TestLibrary.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
WARNING: NU5100: The assembly 'obj\Debug\TestLibrary.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
WARNING: NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below:
- Add lib or ref assemblies for the net452 target framework
The package TestLibrary.1.0.0 is missing a readme. Go to https://aka.ms/nuget/authoring-best-practices/readme to learn why package readmes are important.
Successfully created package 'C:\test\TestLibrary\TestLibrary\TestLibrary.1.0.0.nupkg'..
已创建具有更新描述的包,但内容与第一次尝试时完全不同。它不只是添加一个
lib
文件夹,而是添加整个项目。
如何使用 nuspec 文件触发与项目文件情况相同的包内容?
只需在
nuget pack
文件旁边存在 .nuspec
文件时运行 .csproj
即可。它将自动使用。