快速上手

三步快速初始化项目

目录

步骤1 : 初始化新项目

注意:建议删除系统环境变量 JAVA_HOMEM2_HOMEMAVEN_HOME,不删除可能会导致项目初始化脚本执行失败。
2025-05-01 之后初始化的项目都有集成项目级别的 mvnw 包装器,java 和 maven 环境都在包装器内。在这之前的项目可以将代码模板仓库的 mvnw 文件和 .mvn 目录复制到老项目,就能自动适配环境。

  • 方法1(二选一)
# curl + bash 命令直接访问远程脚本,并执行项目初始化步骤;windows 系统可以用 git bash 命令窗口执行 bash 命令
curl https://java-doc.vinlion.cn/cli.sh>cli.sh && bash ./cli.sh
# 执行下载好的脚本,开始初始化项目;windows 系统可以用 git bash 命令窗口执行 bash 命令
bash cli.sh

应用名 和 新项目 git 地址必填,其他配置项可以在项目初始化完成之后修改。 脚本执行完成后会自动推送代码到新仓库。执行过程截图如下:

项目结构说明

  • doc:项目相关的文档和材料
  • gulp:代码生成及接口测试脚本
  • starter:集成组件封装、依赖
  • app-starter[jar]:单体应用、微服务都需要依赖的组件
  • app-demo[jar]:单体应用
  • gateway[jar]:微服务:网关
  • examples:使用案例
  • app-mysql:mysql代码模板生成测试
  • app.common.starter.util.Dates 仔细阅读这个日期工具类,使用了比较友好的链式调用,底部有 main 方法参考
  • app.common.starter.util.Util 这个类聚合了一些工具类,有一些方法标记为弃用,因为可以使用第三方的工具类(guava、spring、apache)替代,不需要自己封装,尽量避免自己造轮子,用好现有的框架即可

步骤2 :mysql 数据库初始化

-- 建库
DROP DATABASE IF EXISTS local_mysql_db;
CREATE DATABASE local_mysql_db CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';
-- 创建用户 local_mysql_db_admin, 任意IP即可登录; 设置密码为 local_mysql_db@ADMIN
CREATE USER 'local_mysql_db_admin' @'%' IDENTIFIED BY 'local_mysql_db@ADMIN';
-- 为 local_mysql_db_admin 授权 local_mysql_db 数据库的全部增删改查权限
GRANT ALL PRIVILEGES ON local_mysql_db.* TO 'local_mysql_db_admin' @'%';

项目首次启动时,会执行 app-demo/src/main/resources/db/liquibase.xml 自动创建表结构。
注意:

  • 建表语句参考 app-demo/src/main/resources/db/mysql.sql ,业务表建表语句一定要包含字段【id,createTime,createUserId,updateTime,updateUserId,deleted】,中间表可以只有【id】

步骤3 : 启动项目

项目初始化完成后,本地启动项目需要修改 application-local.yml 配置。更多配置信息参考:配置

spring.redis.host: # redis 服务ip
spring.redis.port: # redis 端口
spring.redis.password: # redis 密码
spring.datasource.url: # 数据库连接
spring.datasource.username: # 数据库账号
spring.datasource.password: # 数据库密码

单体应用启动 ****Application.java

问题

大家可以收集脚本运行的问题和解决方案补充文档中。

  • Could not acquire lock(s) 这个异常是因为 mvn 并行编译和多模块有共同的依赖,多执行几次脚本,等所有 maven 依赖都下载完就不会有问题了。
[ERROR] Could not acquire lock(s)
[ERROR] java.lang.IllegalStateException: Could not acquire lock(s)
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.14.0:compile (default-compile) on project app-ip-region-starter: Resolution of annotationProcessorPath dependencies failed: Could not acquire lock(s) -> [Help 1]
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.14.0:compile (default-compile) on project app-enums-starter: Resolution of annotationProcessorPath dependencies failed: Could not acquire lock(s) -> [Help 1]
[ERROR] Could not acquire lock(s)
[ERROR] Could not acquire lock(s)
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :app-ip-region-starter