首先說明,尊重原創,本文是參考https://www.cnblogs.com/myshowtime/p/14507675.html這篇文章寫的,里面內容有部分是直接拷貝過來的。感謝作者分享!!!
前期准備工作
使用命令 abp new LS.Template --template module --no-ui --version 4.3.0 創建基於abp vnext 4.3.0的項目。然后在這個解決方案的基礎上,刪除多余的東東,並加入自己團隊的元素。
這部分不是本文重點,所以不做過多介紹。
模板項目位置
模板項目結構
構建模板
先在“\template\templates”目錄下創建“.template.config”目錄,具體命令為:mkdir .template.config ,
然后在該目錄下添加“template.json”文件,具體內容為:
{ "$schema": "http://json.schemastore.org/template", "author": "Ben", "classifications": [ "Template" ], "name": "LS.Template", "identity": "LS.Template", "shortName": "ls-template", "tags": { "language": "C#" }, "sourceName": "Template" }
上面是一些基本的描述信息,需要注意的是 "sourceName" 屬性,它相當於一個變量,我們通過這個屬性,可以創建LS.BaseInfo,LS.Trace這樣的解決方案和項目。
另外,特別說明一點:如果我的模板項目是LS.Equipment,那么sourceName的值改為Equipment也是可以生成腳手架的。
打包模板
在“\template”目錄下添加“template-pack.csproj”文件
具體內容為:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <PackageType>Template</PackageType> <PackageVersion>1.0.0</PackageVersion> <PackageId>LS.Template</PackageId> <Title>LS.Template</Title> <Authors>Ben</Authors> <Description>LS.Template</Description> <PackageTags>dotnet-new;templates;LS.Template</PackageTags> <TargetFramework>netstandard2.1</TargetFramework> <IncludeContentInPack>true</IncludeContentInPack> <IncludeBuildOutput>false</IncludeBuildOutput> <ContentTargetFolders>content</ContentTargetFolders> <NoWarn>$(NoWarn);NU5128</NoWarn> </PropertyGroup> <ItemGroup> <Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**;templates\.git" /> <Compile Remove="**\*" /> </ItemGroup> </Project>
我們指定了程序包的基礎信息,版本ID, 描述信息,包含了 templates 文件夾下的所有文件,然后排除了 bin\ 和 obj\ 文件夾的dll文件。
運行 dotnet pack 命令進行打包, 你可以在 /bin/debug/ 文件夾找到 生成的 nupkg 文件
推送到Nuget服務器
從網上下載一個nuget.exe文件,放在 /template 目錄下,我的nuget.exe的版本是:5.6.0
然后執行以下命令,輸入賬號、密碼,將代碼推送到nuget倉庫:
nuget.exe push bin/debug/LS.Template.0.1.0.nupkg ben111 -Source http://192.168.3.222:8081/repository/nuget-hosted/
說明:我的nuget倉庫是使用nexus搭建的。上面命令中的ben111相當於是這個倉庫的key,這么理解就可以了。不加也沒關系,我的另一篇文章有說明。linux下 docker + nexus 搭建.net core能使用的nuget服務器
下面看下效果:
安裝模板
安裝前,你的nuget包源,必須事先添加了自己的nuget倉庫地址,不然無法安裝。
至於如何添加nuget包源,有2種方式,第一種是在vs2019 nuget包管理器里面添加;第二種是通過dotnet nuget命令添加。
在終端中運行 dotnet new --install LS.Template 命令安裝,安裝成功后,應該可以看到下邊的輸出,里邊包含了我們的自定義模板。
或者使用 dotnet new --list 命令查看,沒問題的話,列表里面應該會有。
使用模板
運行 dotnet new LS.Template --name=BaseInfo ,--name 指定了變量值,它會自動幫我們生成 BaseInfo 項目,這很棒!牛逼!
至此,大功告成。概括一下:
打包: dotnet pack
推送: nuget.exe push bin/debug/LS.Template.0.1.0.nupkg ben111 -Source http://192.168.3.222:8081/repository/nuget-hosted/
安裝: dotnet new --install LS.Template
使用: dotnet new LS.Template --name=BaseInfo
如果說模板改了后,重新推送到nuget服務器,那么我們下次根據模板生成的時候,需要先安裝模板 dotnet new --install LS.Template ,否則使用的還是之前的模板。
再次說明,尊重原創,本文是參考https://www.cnblogs.com/myshowtime/p/14507675.html這篇文章寫的,里面內容有部分是直接拷貝過來的。感謝作者分享!!!