场景:日常工作中,你可能会碰到需要新建一个全新的解决方案的情况(如公司新起了一个新项目,需要有全新配套的后台程序),如果公司内部基础框架较多、解决方案需要ddd模式等,那么从新起项目到各种依赖引用到能实际可用,一大堆的配置都需要重新设定、测试,耗时耗力,根据项目的大小,往往可能需要 1-2 小时甚至更久。

在 .net core 之前,虽然有相关的解决方法可以实现“项目模板”这个需求,但在具体操作时很不方便,从 .net core 1.0 开始,提供了“模板引擎”,增加了 dotnet new –install(-i) 命令和选项,通过该命令,可以让你方便的创建属于你自己的项目模板。

通过本文你可以了解和掌握:

  1. 掌握如何将一个现有解决方案中的项目作为项目模板。
    1. 掌握如何在本地创建项目模板并安装和使用。
    2. 掌握如何将本地模板打包成 nuget 包,并通过包 id 进行安装使用该模板。
  2. 了解、掌握简单的 dotnet 和 nuget 命令及其配置。(windows 和 mac 会做差异说明)

准备工作

本次项目结构如下(ddd):

你可以在我的 github 库:
https://github.com/artechchu/template 直接下载该模板源码

  • 用于发布的项目一共两个,template.console 和 template.webapi
    • 其中 console 项目就是简单的引用了下其他项目进行输出。
    • webapi 项目简单配置了下依赖注入,你可以将此项目作为 api 模板项目来说

console 项目概要:

webapi 项目概要:

将本地项目作为本地模板,通过命令进行安装和使用

  1. 本次示例以 console 为例,将控制台项目涉及到的项目拷贝一份到如下文件夹中:

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

    {
      "$schema": "http://json.schemastore.org/template",
      "author": "artech",
      "classifications": [ "console" ],
      "name": "custom console",
      "identity": "custom console", //模板唯一标识
      "groupidentity": "custom console", 
      "shortname": "customconsole", //【修改】短名称,使用 dotnet new <shortname> 安装模板时的名称
      "tags": {
        "language": "c#", 
        "type": "project" 
      },
      "sourcename": "template", //【修改】在使用 -n 选项时,会替换模板中项目的名字
      "prefernamedirectory": true
    }
    • 这里主要说明下 shortname 和 sourcename 这 2 个属性。
      • shortname:短名称,用于在使用“dotnet new -l”命令时显示,安装时也可直接根据该短名称进行安装。
      • sourcename:当我们在使用”dotnet new” 命令进行安装时,如果指定了 -n 或者 -o 选项,那么选项后面的名字会自动替换 sourcename 中指定的名字,因为我们的项目命名规则是 “template.xxxx”,所以这里设定为“template”,如果你的项目命名规则是“公司.项目.xxx”,那么这里请设定为“公司.项目”。
  3. 安装该模板到本地模板库

    # 通过如下命令查看当前本机已安装模板:
    dotnet  new  -l 

    # 模板安装命令:dotnet  new  i <path | nugetid>
    # 这里因为是安装本地模板,直接使用路径(绝对和相对均可)
    dotnet  new  -i  .

  4. 安装该短名称为 customconsole 的模板

    假定安装路径为 d:\testtemplate
    假定新起的项目名为“company.group”

    # 这里使用 -n 和 -o 选项来分别指定新项目的名字以及输出目录
    # 设定新项目的名字为“company.group”,因为当前定位已经在 testtemplate 文件夹内,所以直接用“.”,如下:
     dotnet   new   customconsole   -n   company.group   -o   .

    文件夹内容如下:

    • 使用模板新建的项目文件夹自动为“company.group.xxxx”

    测试:

    更多关于 template.json 的说明请参考:

    • 在 template.json 中,你还可以指定 symbols 等,来实现更多的自定义功能,如联动预编译指令等等。

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

本次示例以 console +webapi 为例,在 templates\nuget 文件夹中,建立 content 文件夹用于存放 nuget 包内容,具体如下:

  • consoletemplate 中的 .template.config\template.json 内容同上方 console 示例。
  • webapitemplate 中的 .template.config\template.json 内容如下:

    {
      "$schema": "http://json.schemastore.org/template",
      "author": "artech",
      "classifications": [ "webapi" ],
      "name": "custom webapi",
      "identity": "custom webapi",
      "groupidentity": "custom webapi",
      "shortname": "customwebapi",
      "tags": {
        "language": "c#",
        "type": "project"
      },
      "sourcename": "template",
      "prefernamedirectory": true
    }
  1. 在 content 目录内创建一个 nuspec 文件:custom.template.netcore.nuspec,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
      <metadata>
        <id>custom.template.netcore</id>
        <version>1.0.1</version>
        <description>
          custom template, including webapi, console
        </description>
        <authors>artech</authors>
        <packagetypes>
          <packagetype name="template" />
        </packagetypes>
      </metadata>
    </package>
    • 需要注意,packagetype 为 template,metadata.id 必须保证唯一,其他按需设置即可。
    • 必须是在 content 文件夹内。nuget 在打包的时候,是根据 content 文件夹来进行的。
  2. 使用 nuget pack 命令打包

    # 注意路径的相对位置
    nuget   pack   custom.template.netcore.nuspec   -outputdirectory   .

  • 打包后的内容为:

  1. 发布该 nuget 包到 nuget server

    这里用的是自建 nuget server,你可以按自身情况打包上传。

    • 你可以直接使用 nuget package explorer 进行发布包
    • 也可以使用 nuget push 来发布,如下:
    nuget push custom.template.netcore.1.0.1.nupkg -source "你的nuget 服务 url" -apikey "你的nuget api key"
  2. 通过 nuget 安装模板到本地

  • 安装前本地已经安装的模板如下:

  • 安装

    dotnet new -i custom.template.netcore::*

  1. 通过模板安装 customwebapi

    安装路径为:d:\testwebapitemplate

    dotnet  new  customwebapi  -n  company.group  -o  .
  2. 创建一个解决方案,并将所有的项目添加到解决方案 company.group.sln 中

    dotnet new sln -n company.group
    # windows 下无法使用 glob pattern 只能逐个添加
    dotnet sln company.group.sln add company.group.application\company.group.application.csproj
    dotnet sln company.group.sln add company.group.domain\company.group.domain.csproj
    dotnet sln company.group.sln add company.group.domainservice\company.group.domainservice.csproj
    dotnet sln company.group.sln add company.group.iapplication\company.group.iapplication.csproj
    dotnet sln company.group.sln add company.group.idomainservice\company.group.idomainservice.csproj
    dotnet sln company.group.sln add company.group.infrastructure.crosscutting\company.group.infrastructure.crosscutting.csproj
    dotnet sln company.group.sln add company.group.repository\company.group.repository.csproj
    dotnet sln company.group.sln add company.group.webapi\company.group.webapi.csproj

    如果你用的是 mac / linux ,则可以直接用 globbing pattern 来添加,如下:

    dotnet sln company.group.sln add **/*.csproj

参考