创建模板包

准备工作

项目模板所在路径:

必须是在 content 文件夹内。nuget 在打包的时候,是根据 content 文件夹来进行的

image.png
如下项目结构作为项目模板
image.png

手动创建一个名为“.template.config”的文件夹,并在该文件夹内创建文件:template.json,内容如下:

{
  "$schema": "http://json.schemastore.org/template",
  "author": "Eureka",
  "classifications": [ "Web/WebApi" ],
  "name": "Eureka.eHospital.Business.Template",
  "identity": "Eureka.eHospital.Business.Template", //模板唯一标识
  "groupIdentity": "Eureka.eHospital.Business.Template", 
  "shortName": "ebt", //【修改】短名称,使用 dotnet new <shortName> 安装模板时的名称
  "tags": {
    "language": "C#", 
    "type": "project" 
  },
  "sourceName": "eHospital.Core.Nursing", //【修改】在使用 -n 选项时,会替换模板中项目的名字
  "preferNameDirectory": true
}

将本地项目打包为 nuget 包,并通过命令进行安装和使用

在 content 目录内创建一个 nuspec 文件:Eureka.eHospital.Business.Template.nuspec,内容如下:
image.png

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>
    <id>Eureka.eHospital.Business.Template</id>
    <version>1.0.0</version>
    <description>
      业务服务模板包
    </description>
    <authors>Eureka</authors>
    <packageTypes>
      <packageType name="Template" />
    </packageTypes>
  </metadata>
</package>
  • 需要注意,packageType 为 Template,metadata.id 必须保证唯一,其他按需设置即可。
  • 必须是在 content 文件夹内。nuget 在打包的时候,是根据 content 文件夹来进行的。

使用 nuget pack 命令打包

PS D:\projects\sample\dotnetcore.template\Nuget> nuget pack Eureka.eHospital.Business.Template.nuspec

image.png
显示成功
image.png

安装模板包到本地

PS D:\projects\sample\dotnetcore.template\Nuget> dotnet new install Eureka.eHospital.Business.Template.1.0.0.nupkg

image.png
如果将 NuGet 包上传到 NuGet 源,可以使用 dotnet new install <PACKAGE_ID> 命令,其中,<PACKAGE_ID> 与 .csproj 文件中的 设置相同。 此包 ID 与 NuGet 包标识符相同

使用dotnet new list 查看是否安装成功
image.png

使用模板包创建项目基架

PS D:\projects\sample\dotnetcore.template\test> dotnet new ebt -n eHostipal.Core.test

image.png
image.png

posted @ 2023-01-11 17:40  无敌土豆  阅读(39)  评论(0编辑  收藏  举报