基于Springboot的服务端开发脚手架-自动生成工具

继之前的

后带来一款项目自动手成工具(由于包路径等原因,完整demo想应用在实际开发中需要改很多代码),可以设置成自己公司的一些规定包路径等。

此为一工具,默认生成java服务工程,但可经过配置生成其它类型的工程或目录结构,如python、recat等。此工具中自带了之前课程中提到的dao操作、http拦截、tracklog日志跟踪、exception切面处理等所有功能。可节省大量的搭建工程和基础工作的开发时间。而且生成的项目文件源文件,可进行二次修改。

一、如何使用

建议下载ReleasesV1.0,解压后先运行包中自带的test.sh脚本,会在解压目录的/output下生成三个预置好的三个工程,restful、grpc、fixed(restful+grpc)。

复制代码
sh test.sh

截图如下:

二、生成自定义的工程

这一步建议选择一个预置的.setting文件进行修改,配置文件说明如下:

2.1、修改project.setting文件

复制代码
# project config
project.name = base-grpc-framework   #工程名称
project.groupId = com.zd   #对应pom文件的groupId,全工程所有模块统一
project.version = 1.0-SNAPSHOT #对应pom文件的groupId,全工程所有模块统一
project.packaging = jar  #工程打包类型

project.basePackage = com.zd.baseframework  #源码根目录

project.app.tokenKey = token  #token的key,用于http拦截器访问端验证用
project.app.apiPath = /api/** #用于http拦截器使用

#module config
project.modules = api, proto, app, common, core, dao #所有模块名称

#模块类型,可选的有 proto, api, application, common, restful, grpc, fixed,persistence
#PS:一个完整工程最好的实践是由API, SERVER, COMMON, APP, DAO 5个大类型的模块组成,所以在同一个配置文件中会有如下互斥约束
#API模块:(proto, api)二选一,
#SERVER模块:restful, grpc, fixed 三选一,
#必要模块:application,启动
#可选模块:persistence, common 持久化和工具类
api.module.type = api 
api.module.artifactId = base-grpc-framework-api

proto.module.type = proto
proto.module.artifactId = base-grpc-framework-proto

common.module.type = common
common.module.artifactId = base-grpc-framework-common

app.module.type = application
app.module.artifactId = base-grpc-framework-app

core.module.type = fixed
core.module.artifactId = base-grpc-framework-core
core.moudle.port = 18080 #http服务端口
core.moudle.grpc.port = 9090 #grpc服务端口

dao.module.type = persistence
dao.module.artifactId = base-grpc-framework-dao
dao.module.mysql.dbIp = 127.0.0.1
dao.module.mysql.dbPort = 3306
dao.moudle.mysql.dbName = badCase
dao.module.mysql.dbUsername = root
dao.module.mysql.dbPassword = 12345678
dao.module.mysql.mapper = mybatis #mapper文件存放位置

2.2、生成自定义项目

复制代码
java -jar grpc-framework-project-generator-1.0-SNAPSHOT-jar-with-dependencies.jar [setting=project_fixed.setting] [slt=/slt/source/]
setting默认值为:project.setting
slt默认值为:/slt/source/ 注意最后的/不要丢掉

2.3、打包运行

需要本机装有maven,执行以下命令:

复制代码
cd /output/xxxxProject
mvn clean install -Dmaven.test.skip=true
mvn clean package -Dmaven.test.skip=true
java -jar -Dspring.profiles.active=dev xxxxProject.jar

# Step3: Test
open http://localhost:18080/swagger-ui.html

比如上述文件中

  1. project.name = base-grpc-framework
  2. project.version = 1.0-SNAPSHOT
  3. app.module.artifactId = base-grpc-framework-app

则执行以下命令:

复制代码
cd /output/base-grpc-framework
mvn clean install -Dmaven.test.skip=true
mvn clean package -Dmaven.test.skip=true
java -jar -Dspring.profiles.active=dev 1. base-grpc-framework-app-1. 1.0-SNAPSHOT.jar

# Step3: Test
open http://localhost:18080/swagger-ui.html

三、配置符合公司个性的默认工程

上述工具中笔者内置了一些类和文件,这些文件是可以被编辑和替换的,也可基于笔者内置的类来扩展。

3.1、扩展基础文件

扩展配置文件 slt/source/sltext.json,因工具本身内置了一些java实现,基于规范的述求一般公司对这些文件是不允许修改的,如果使用部门有个性化需求的话,比如增加一个基础类,就可以通过这个功能来实现个性化和扩展但又不影响基础版本。

复制代码
{
  "proto": [
    {"name": "GrpcAccessInterceptor.java", "packagePath": "/grpc/interceptor", "fileType": "source", "fileOperatorType": "create"}
  ]
}

[options]
moduleType: proto, application, persistence, common, restful, grpc, fixed
name: fileName
packagePath: file path 
fileType:  source, yml, xml or config
fileOperatorType: create, copy or append

3.2、重新制定基础文件

基础文件存储路径:slt/source,笔者现在的版本有一个限制就是内置的文件不允许缺失只能改写,否则会报错。如果想配置成公司内部的文件或对基础文件有修改,可以下载笔者的源码后进行修改,或是把源码中所有的配置全部删除,全部用sltext.json的方式来实现

  1. 源码修改位置:

    com.zd.tools.project.generator.model.module 包中的所有文件

    下面类用于生成src/main和src/resources目录,如果不需要的话可以注释掉其中的代码
    com.zd.tools.project.generator.analysis.process.SettingFileConvert.configOwnAttr()

2、源码修改说明,可根据文件名对应各个类型模块的配置

复制代码
ModuleApi.java          :对应api类型的模块
ModuleApplication.java  :对应application类型的模块
ModuleCommon.java        :对应common类型的模块
ModuleFixed.java        :对应fixed类型的模块
ModuleGrpc.java          :对应grpc类型的模块
ModulePersistence.java  :对应persistence类型的模块
ModuleProto.java        :对应proto类型的模块
ModuleRestful.java      :对应restful类型的模块

源码如下:

复制代码
public class ModuleRestful extends AbstractModule {

    private String port;
   
    //预生成目录结构
    @Override
    public void configOwnDir() {
        super.configOwnDir();
        getDirs().add(getPackagePath() + File.separator + "server");
        getDirs().add(getPackagePath() + File.separator + "restful");
        getDirs().add(getPackagePath() + File.separator + StrFormatterUtil.replaceSlash("restful/config"));
        getDirs().add(getPackagePath() + File.separator + StrFormatterUtil.replaceSlash("restful/advice"));
        getDirs().add(getPackagePath() + File.separator + StrFormatterUtil.replaceSlash("restful/interceptor"));
        getDirs().add(getPackagePath() + File.separator + StrFormatterUtil.replaceSlash("restful/model"));
    }

    //预生成文件
    @Override
    public void configOwnSourceFile(){
        super.configOwnSourceFile();
        getSourceFiles().add(new SourceFile("log4j2.xml",                      getResourcesPath(), "", GenEnum.fileType.config, GenEnum.fileOperatorType.copy));

        getSourceFiles().add(new SourceFile("RestfulHttpExceptionAdvice.java", getPackagePath(), StrFormatterUtil.replaceSlash("/restful/advice"),      GenEnum.fileType.source, GenEnum.fileOperatorType.create));
        getSourceFiles().add(new SourceFile("RestfulAccessInterceptor.java",   getPackagePath(), StrFormatterUtil.replaceSlash("/restful/interceptor"), GenEnum.fileType.source, GenEnum.fileOperatorType.create));
        getSourceFiles().add(new SourceFile("RestfulInterceptorRegister.java", getPackagePath(), StrFormatterUtil.replaceSlash("/restful/interceptor"), GenEnum.fileType.source, GenEnum.fileOperatorType.create));
        getSourceFiles().add(new SourceFile("RestfulTokenInterceptor.java",    getPackagePath(), StrFormatterUtil.replaceSlash("/restful/interceptor"), GenEnum.fileType.source, GenEnum.fileOperatorType.create));
        getSourceFiles().add(new SourceFile("SwaggerConfig.java",              getPackagePath(), StrFormatterUtil.replaceSlash("/restful/config"),      GenEnum.fileType.source, GenEnum.fileOperatorType.copy));

        getSourceFiles().add(new SourceFile("application-restful.properties",getResourcesPath(), "", GenEnum.fileType.yml, GenEnum.fileOperatorType.append));

        getSourceFiles().add(new SourceFile("pom_restful.xml", getBasePath() ,"", GenEnum.fileType.xml, GenEnum.fileOperatorType.copy));
    }
}

3.3、编写基础文件

基础文件可参考笔者预置的文件进行修改,主要是用占位符替换.java文件中的package, import和className。示例如下:

复制代码
package ${package};  #固定写法

#common为模块类型(proto, api, application, common, restful, grpc, fixed,persistence)中选一个, 
# "Module"为固定后缀,例子中${commonModule}表示引入common模块的一个类
import ${commonModule}.spring.ApperProperties;  
import ${commonModule}.spring.grpc.AbstractGrpcAccessInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Title: ${package}.${className}
 * @Description
 * @author liudong
 * @date 2022/1/13 4:40 PM
 */
@Slf4j
public class ${className} extends AbstractGrpcAccessInterceptor { #固定写法

    @Autowired
    private ApperProperties apperProperties;

    @Override
    protected String tokenKey() {
        return apperProperties.getTokenKey();
    }

}
相关推荐
whoarethenext3 分钟前
使用 C++ 和 OpenCV 构建智能答题卡识别系统
开发语言·c++·opencv
秋山落叶万岭花开ღ10 分钟前
树的基本概念与操作:构建数据结构的层级世界
数据结构·python·算法
m0_5164846726 分钟前
C#winform多选框代码
开发语言·c#
山花31 分钟前
什么是“Pythonic”?——写出更优雅的Python代码(上)
后端·python
啾啾Fun1 小时前
Java反射操作百倍性能优化
java·性能优化·反射·缓存思想
20岁30年经验的码农1 小时前
若依微服务Openfeign接口调用超时问题
java·微服务·架构
曲莫终1 小时前
SpEl表达式之强大的集合选择(Collection Selection)和集合投影(Collection Projection)
java·spring boot·spring
ajassi20001 小时前
开源 java android app 开发(十二)封库.aar
android·java·linux·开源
Humbunklung1 小时前
全连接层和卷积层
人工智能·python·深度学习·神经网络·机器学习·cnn
q567315231 小时前
Java使用Selenium反爬虫优化方案
java·开发语言·分布式·爬虫·selenium