场景:平常工做中,你可能会碰到须要新建一个全新的解决方案的状况(如公司新起了一个新项目,须要有全新配套的后台程序),若是公司内部基础框架较多、解决方案须要DDD模式等,那么重新起项目到各类依赖引用到能实际可用,一大堆的配置都须要从新设定、测试,耗时耗力,根据项目的大小,每每可能须要 1-2 小时甚至更久。html
在 .net core 以前,虽然有相关的解决方法能够实现“项目模板”这个需求,但在具体操做时很不方便,从 .net core 1.0 开始,提供了“模板引擎”,增长了 dotnet new --install(-i) 命令和选项,经过该命令,可让你方便的建立属于你本身的项目模板。linux
本文原始地址:https://blog.zhuliang.ltd/2019/07/net-core/custom-template-using-dotnetcore.htmlgit
经过本文你能够了解和掌握:github
本次项目结构以下(DDD):json
你能够在个人 github 库:
https://github.com/ArtechChu/Template 直接下载该模板源码windows
Console 项目概要:api
WebApi 项目概要:框架
本次示例以 Console 为例,将控制台项目涉及到的项目拷贝一份到以下文件夹中:ide
手动建立一个名为“.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 }
安装该模板到本地模板库
# 经过以下命令查看当前本机已安装模板: dotnet new -l
# 模板安装命令:dotnet new i <path | nugetId> # 这里由于是安装本地模板,直接使用路径(绝对和相对都可) dotnet new -i .
安装该短名称为 CustomConsole 的模板
假定安装路径为 D:\TestTemplate
假定新起的项目名为“Company.Group”
# 这里使用 -n 和 -o 选项来分别指定新项目的名字以及输出目录 # 设定新项目的名字为“Company.Group”,由于当前定位已经在 TestTemplate 文件夹内,因此直接用“.”,以下: dotnet new CustomConsole -n Company.Group -o .
文件夹内容以下:
测试:
更多关于 template.json 的说明请参考:http://json.schemastore.org/template
- 在 template.json 中,你还能够指定 symbols 等,来实现更多的自定义功能,如联动预编译指令等等。
本次示例以 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 }
在 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>
使用 nuget pack 命令打包
# 注意路径的相对位置 nuget pack Custom.Template.NetCore.nuspec -OutputDirectory .
打包后的内容为:
发布该 nuget 包到 nuget server
这里用的是自建 nuget server,你能够按自身状况打包上传。
nuget push Custom.Template.NetCore.1.0.1.nupkg -Source "你的nuget 服务 url" -ApiKey "你的nuget api key"
经过 nuget 安装模板到本地
安装前本地已经安装的模板以下:
安装
dotnet new -i Custom.Template.NetCore::*
经过模板安装 CustomWebApi
安装路径为:D:\TestWebApiTemplate
dotnet new CustomWebApi -n Company.Group -o .
建立一个解决方案,并将全部的项目添加到解决方案 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
https://devblogs.microsoft.com/dotnet/how-to-create-your-own-templates-for-dotnet-new/
https://github.com/dotnet/dotnet-template-samples
https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new?tabs=netcore22
https://docs.microsoft.com/en-us/nuget/install-nuget-client-tools