Commit fcf097847a5bd5b45c68db3cebf3968f7b8bbe53
1 parent
3e2a809f
reactor:【INFRA 基础设施】代码生成时,区分 boot 还是 cloud 项目
reactor:【INFRA 基础设施】代码生成时,更严格的区分 master 还是 master-jdk17 分支
Showing
2 changed files
with
31 additions
and
9 deletions
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java
| @@ -40,6 +40,7 @@ import jakarta.annotation.PostConstruct; | @@ -40,6 +40,7 @@ import jakarta.annotation.PostConstruct; | ||
| 40 | import jakarta.annotation.Resource; | 40 | import jakarta.annotation.Resource; |
| 41 | import lombok.Setter; | 41 | import lombok.Setter; |
| 42 | import org.springframework.stereotype.Component; | 42 | import org.springframework.stereotype.Component; |
| 43 | +import org.springframework.util.ClassUtils; | ||
| 43 | 44 | ||
| 44 | import java.util.*; | 45 | import java.util.*; |
| 45 | 46 | ||
| @@ -64,7 +65,7 @@ public class CodegenEngine { | @@ -64,7 +65,7 @@ public class CodegenEngine { | ||
| 64 | * value:生成的路径 | 65 | * value:生成的路径 |
| 65 | */ | 66 | */ |
| 66 | private static final Map<String, String> SERVER_TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序 | 67 | private static final Map<String, String> SERVER_TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序 |
| 67 | - // Java module-biz Main | 68 | + // Java module-biz(server) Main |
| 68 | .put(javaTemplatePath("controller/vo/pageReqVO"), javaModuleImplVOFilePath("PageReqVO")) | 69 | .put(javaTemplatePath("controller/vo/pageReqVO"), javaModuleImplVOFilePath("PageReqVO")) |
| 69 | .put(javaTemplatePath("controller/vo/listReqVO"), javaModuleImplVOFilePath("ListReqVO")) | 70 | .put(javaTemplatePath("controller/vo/listReqVO"), javaModuleImplVOFilePath("ListReqVO")) |
| 70 | .put(javaTemplatePath("controller/vo/respVO"), javaModuleImplVOFilePath("RespVO")) | 71 | .put(javaTemplatePath("controller/vo/respVO"), javaModuleImplVOFilePath("RespVO")) |
| @@ -83,7 +84,7 @@ public class CodegenEngine { | @@ -83,7 +84,7 @@ public class CodegenEngine { | ||
| 83 | javaModuleImplMainFilePath("service/${table.businessName}/${table.className}ServiceImpl")) | 84 | javaModuleImplMainFilePath("service/${table.businessName}/${table.className}ServiceImpl")) |
| 84 | .put(javaTemplatePath("service/service"), | 85 | .put(javaTemplatePath("service/service"), |
| 85 | javaModuleImplMainFilePath("service/${table.businessName}/${table.className}Service")) | 86 | javaModuleImplMainFilePath("service/${table.businessName}/${table.className}Service")) |
| 86 | - // Java module-biz Test | 87 | + // Java module-biz(server) Test |
| 87 | .put(javaTemplatePath("test/serviceTest"), | 88 | .put(javaTemplatePath("test/serviceTest"), |
| 88 | javaModuleImplTestFilePath("service/${table.businessName}/${table.className}ServiceImplTest")) | 89 | javaModuleImplTestFilePath("service/${table.businessName}/${table.className}ServiceImplTest")) |
| 89 | // Java module-api Main | 90 | // Java module-api Main |
| @@ -195,6 +196,15 @@ public class CodegenEngine { | @@ -195,6 +196,15 @@ public class CodegenEngine { | ||
| 195 | private Boolean jakartaEnable; | 196 | private Boolean jakartaEnable; |
| 196 | 197 | ||
| 197 | /** | 198 | /** |
| 199 | + * 是否为 yudao-cloud 项目,用于解决 Boot 和 Cloud 的 api 模块兼容性问题 | ||
| 200 | + * | ||
| 201 | + * true - 需要有 yudao-module-xxx-api 模块 | ||
| 202 | + * false - 不需要有,使用 api、enum 包即可 | ||
| 203 | + */ | ||
| 204 | + @Setter | ||
| 205 | + private Boolean cloudEnable; | ||
| 206 | + | ||
| 207 | + /** | ||
| 198 | * 模板引擎,由 hutool 实现 | 208 | * 模板引擎,由 hutool 实现 |
| 199 | */ | 209 | */ |
| 200 | private final TemplateEngine templateEngine; | 210 | private final TemplateEngine templateEngine; |
| @@ -209,7 +219,11 @@ public class CodegenEngine { | @@ -209,7 +219,11 @@ public class CodegenEngine { | ||
| 209 | config.setResourceMode(TemplateConfig.ResourceMode.CLASSPATH); | 219 | config.setResourceMode(TemplateConfig.ResourceMode.CLASSPATH); |
| 210 | this.templateEngine = new VelocityEngine(config); | 220 | this.templateEngine = new VelocityEngine(config); |
| 211 | // 设置 javaxEnable,按照是否使用 JDK17 来判断 | 221 | // 设置 javaxEnable,按照是否使用 JDK17 来判断 |
| 212 | - this.jakartaEnable = SystemUtil.getJavaInfo().isJavaVersionAtLeast(1700); // 17.00 * 100 | 222 | + this.jakartaEnable = SystemUtil.getJavaInfo().isJavaVersionAtLeast(1700) // 17.00 * 100 |
| 223 | + && ClassUtils.isPresent("jakarta.annotation.Resource", ClassUtils.getDefaultClassLoader()); | ||
| 224 | + // 设置 cloudEnable,按照是否使用 Spring Cloud 来判断 | ||
| 225 | + this.cloudEnable = ClassUtils.isPresent("cn.iocoder.yudao.module.infra.framework.rpc.config.RpcConfiguration", | ||
| 226 | + ClassUtils.getDefaultClassLoader()); | ||
| 213 | } | 227 | } |
| 214 | 228 | ||
| 215 | @PostConstruct | 229 | @PostConstruct |
| @@ -434,6 +448,14 @@ public class CodegenEngine { | @@ -434,6 +448,14 @@ public class CodegenEngine { | ||
| 434 | Map<String, String> templates = new LinkedHashMap<>(); | 448 | Map<String, String> templates = new LinkedHashMap<>(); |
| 435 | templates.putAll(SERVER_TEMPLATES); | 449 | templates.putAll(SERVER_TEMPLATES); |
| 436 | templates.putAll(FRONT_TEMPLATES.row(frontType)); | 450 | templates.putAll(FRONT_TEMPLATES.row(frontType)); |
| 451 | + // 如果是 Boot 项目,则不使用 api/server 模块 | ||
| 452 | + if (Boolean.FALSE.equals(cloudEnable)) { | ||
| 453 | + SERVER_TEMPLATES.forEach((templatePath, filePath) -> { | ||
| 454 | + filePath = StrUtil.replace(filePath, "/yudao-module-${table.moduleName}-api", ""); | ||
| 455 | + filePath = StrUtil.replace(filePath, "/yudao-module-${table.moduleName}-server", ""); | ||
| 456 | + templates.put(templatePath, filePath); | ||
| 457 | + }); | ||
| 458 | + } | ||
| 437 | // 如果禁用单元测试,则移除对应的模版 | 459 | // 如果禁用单元测试,则移除对应的模版 |
| 438 | if (Boolean.FALSE.equals(codegenProperties.getUnitTestEnable())) { | 460 | if (Boolean.FALSE.equals(codegenProperties.getUnitTestEnable())) { |
| 439 | templates.remove(javaTemplatePath("test/serviceTest")); | 461 | templates.remove(javaTemplatePath("test/serviceTest")); |
| @@ -480,16 +502,16 @@ public class CodegenEngine { | @@ -480,16 +502,16 @@ public class CodegenEngine { | ||
| 480 | 502 | ||
| 481 | private static String javaModuleImplVOFilePath(String path) { | 503 | private static String javaModuleImplVOFilePath(String path) { |
| 482 | return javaModuleFilePath("controller/${sceneEnum.basePackage}/${table.businessName}/" + | 504 | return javaModuleFilePath("controller/${sceneEnum.basePackage}/${table.businessName}/" + |
| 483 | - "vo/${sceneEnum.prefixClass}${table.className}" + path, "biz", "main"); | 505 | + "vo/${sceneEnum.prefixClass}${table.className}" + path, "server", "main"); |
| 484 | } | 506 | } |
| 485 | 507 | ||
| 486 | private static String javaModuleImplControllerFilePath() { | 508 | private static String javaModuleImplControllerFilePath() { |
| 487 | return javaModuleFilePath("controller/${sceneEnum.basePackage}/${table.businessName}/" + | 509 | return javaModuleFilePath("controller/${sceneEnum.basePackage}/${table.businessName}/" + |
| 488 | - "${sceneEnum.prefixClass}${table.className}Controller", "biz", "main"); | 510 | + "${sceneEnum.prefixClass}${table.className}Controller", "server", "main"); |
| 489 | } | 511 | } |
| 490 | 512 | ||
| 491 | private static String javaModuleImplMainFilePath(String path) { | 513 | private static String javaModuleImplMainFilePath(String path) { |
| 492 | - return javaModuleFilePath(path, "biz", "main"); | 514 | + return javaModuleFilePath(path, "server", "main"); |
| 493 | } | 515 | } |
| 494 | 516 | ||
| 495 | private static String javaModuleApiMainFilePath(String path) { | 517 | private static String javaModuleApiMainFilePath(String path) { |
| @@ -497,7 +519,7 @@ public class CodegenEngine { | @@ -497,7 +519,7 @@ public class CodegenEngine { | ||
| 497 | } | 519 | } |
| 498 | 520 | ||
| 499 | private static String javaModuleImplTestFilePath(String path) { | 521 | private static String javaModuleImplTestFilePath(String path) { |
| 500 | - return javaModuleFilePath(path, "biz", "test"); | 522 | + return javaModuleFilePath(path, "server", "test"); |
| 501 | } | 523 | } |
| 502 | 524 | ||
| 503 | private static String javaModuleFilePath(String path, String module, String src) { | 525 | private static String javaModuleFilePath(String path, String module, String src) { |
| @@ -508,7 +530,7 @@ public class CodegenEngine { | @@ -508,7 +530,7 @@ public class CodegenEngine { | ||
| 508 | 530 | ||
| 509 | private static String mapperXmlFilePath() { | 531 | private static String mapperXmlFilePath() { |
| 510 | return "yudao-module-${table.moduleName}/" + // 顶级模块 | 532 | return "yudao-module-${table.moduleName}/" + // 顶级模块 |
| 511 | - "yudao-module-${table.moduleName}-biz/" + // 子模块 | 533 | + "yudao-module-${table.moduleName}-server/" + // 子模块 |
| 512 | "src/main/resources/mapper/${table.businessName}/${table.className}Mapper.xml"; | 534 | "src/main/resources/mapper/${table.businessName}/${table.className}Mapper.xml"; |
| 513 | } | 535 | } |
| 514 | 536 |
yudao-module-infra/src/main/resources/codegen/java/enums/errorcode.vm
| 1 | -// TODO 待办:请将下面的错误码复制到 yudao-module-${table.moduleName}-api 模块的 ErrorCodeConstants 类中。注意,请给“TODO 补充编号”设置一个错误码编号!!! | 1 | +// TODO 待办:请将下面的错误码复制到 yudao-module-${table.moduleName} 模块的 ErrorCodeConstants 类中。注意,请给“TODO 补充编号”设置一个错误码编号!!! |
| 2 | // ========== ${table.classComment} TODO 补充编号 ========== | 2 | // ========== ${table.classComment} TODO 补充编号 ========== |
| 3 | ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在"); | 3 | ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在"); |
| 4 | ## 特殊:树表专属逻辑 | 4 | ## 特殊:树表专属逻辑 |