香河老人,空地导弹,夫君谋
场景:日常工作中,你可能会碰到需要新建一个全新的解决方案的情况(如公司新起了一个新项目,需要有全新配套的后台程序),如果公司内部基础框架较多、解决方案需要ddd模式等,那么从新起项目到各种依赖引用到能实际可用,一大堆的配置都需要重新设定、测试,耗时耗力,根据项目的大小,往往可能需要 1-2 小时甚至更久。
在 .net core 之前,虽然有相关的解决方法可以实现“项目模板”这个需求,但在具体操作时很不方便,从 .net core 1.0 开始,提供了“模板引擎”,增加了 dotnet new --install(-i) 命令和选项,通过该命令,可以让你方便的创建属于你自己的项目模板。
通过本文你可以了解和掌握:
本次项目结构如下(ddd):
你可以在我的 github 库:
https://github.com/artechchu/template 直接下载该模板源码
console 项目概要:
webapi 项目概要:
本次示例以 console 为例,将控制台项目涉及到的项目拷贝一份到如下文件夹中:
手动创建一个名为“.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 的说明请参考:
- 在 template.json 中,你还可以指定 symbols 等,来实现更多的自定义功能,如联动预编译指令等等。
本次示例以 console +webapi 为例,在 templates\nuget 文件夹中,建立 content 文件夹用于存放 nuget 包内容,具体如下:
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
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论