制作Nuget包

1 篇文章 0 订阅
1 篇文章 0 订阅

本文仅介绍通过nuspec文件制作Nuget包,
详细的步骤或功能可参考:
https://docs.microsoft.com/zh-cn/nuget/what-is-nuget

1.新建文件夹,本文涉及到的文件夹目录结构:

文件夹介绍
build用于指定使用nuget包的项目的编译选项,本文用于介绍用于packages.config的形式部署资源文件
content包含的资源文件 , 文件直接放在子文件夹中, 用于packages.config的形式部署资源文件
contentFiles包含的资源文件, 子文件夹为 any/any/xxxxs 的形式, 用于PackageReference的形式部署资源文件
lib存放需要引用的类库,子文件夹为 any/any/xxx.dll 的形式
XXX.nuspec配置打包的脚本文件

此文件目录不能修改,必须按照Nuget官方规定的文件目录存放数据,打包时会根据不同的文件夹路径加载相应的内容

2.指定使用nuget包的项目自动引用的库

配置nuspec文件

<references>
    <reference file="Lib1.dll" />
    <reference file="Lib2.dll" />
</references>

references 标签 放到 metadata标签内, 注意指定的file必须在包内的lib\xx\xx\下的子文件夹内

3.指定使用nuget包的项目自动拷贝资源文件

拷贝资源文件的方式有两种,通过packages.config的形式,通过PackageReference的形式。
PackageReference的形式从VS2017开始引用,因此VS2017一下的IDE不能使用PackageReference的功能。

3.1通过packages.config的形式

默认vs加载nuget包时使用此形式
将资源文件放入content文件夹中, 安装nuget包时会将这些资源文件自动引用到项目中
如文件夹结构路径为:

├─build
├─content
│  └─resource
└─lib
    └─net45
├─LibTest.nuspec

通过build中的.targets或.props文件定义这些包的生成规则
.targets或.props文件 为VS项目使用的文件,如下为拷贝resource\resource.txt到生成目录的.props配置

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
<ItemGroup> 
    <Content Include="resource\resource.txt">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>
</Project>

可根据此文件定义资源文件的其他行为,嵌入资源文件,编译文件等。

此配置形式会将下载的nuget包放在本地项目解决方案的同级目录packages,
项目安装nuget包时会在项目.csproj配置文件中自动插入一下的配置信息

  <Import Project="..\packages\LibTest.1.1.1.3\build\LibTest.props" Condition="Exists('..\packages\LibTest.1.1.1.3\build\LibTest.props')" />
  <ItemGroup>
    <Content Include="resource\resource.txt" />
  </ItemGroup>
  <ItemGroup>
    <None Include="packages.config" />
  </ItemGroup>

3.1通过PackageReference的形式

仅vs2017及以上版本支持此功能,
在vs2017中的解决方案资源管理器->packages.config文件右键->Migrate from packages.config to PackageReference切换到使用PackageReference管理的形式,
将资源文件放入contentFiles\xx\xx\ 文件夹下, 配置nuspec文件

    <contentFiles>
		<!-- Copy config.xml to the root of the output folder -->
		<files include="**"  buildAction="None" copyToOuput="true"/>
	</contentFiles>

contentFiles 标签 放到 metadata标签内

此配置形式会将下载的nuget包放在本地用户.nuget\packages\统一管理所有nuget包,
项目安装nuget包时会在项目.csproj配置文件中自动插入一下的配置信息

  <ItemGroup>
    <PackageReference Include="LibTest">
      <Version>1.1.1.4</Version>
    </PackageReference>
  </ItemGroup>

4.然后将文件打包发布

nuget pack $xxx.nuspec$ -Version $y.y.y.y$
nuget push $xxx.y.y.y.y.nupkg$ $userkey$ -Source $server_repository_url$

完整的LibTest.nuspec

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata minClientVersion="3.3">
    <id>LibTest</id>
    <version>1.1.1.4</version>
    <title>test</title>
    <authors>guoys</authors>
    <owners>guoys</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>this is test</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2019</copyright>
	<references>
    <reference file="LibTest.dll" />
	</references>
  </metadata>
  <files>
	<file src="build\*" target="build"/>
	<file src="lib\**" target="lib"/>
	<file src="content\**" target="content"/>
  </files>
</package>

5.根据csproj文件编译出nuget包

当项目库依赖关系很少时,通常仅有一个库文件,可以使用vs的csproj文件直接编译出nuget包,然后上传。

nuget pack $xxx.csproj$ -Properties Configuration=Release -symbols -Version $y.y.y.y$

6.Jenkins上传Nexus服务器

打包发布到Nexus仓库(Nexus注意设置Security->Reams), APIkey从Nexus用户信息出获取
系统配置设置Nexus服务器的信息
在这里插入图片描述
项目配置生成Nuget包
在这里插入图片描述
配置构建后发布到Nexus服务器上
在这里插入图片描述
构建发布即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值