update(REQ-2516) - 测试 Javaparser 生成代码的逻辑,并控制 Maven 在打包时,能自动先生成,再 package starter 包

This commit is contained in:
wangli 2024-06-12 11:05:40 +08:00
parent 997773182d
commit 386cbf9597
9 changed files with 105 additions and 73 deletions

View File

@ -159,7 +159,7 @@
<module>workflow-engine-common</module>
<module>workflow-engine-core</module>
<module>workflow-engine-server</module>
<module>workflow-engine-spring-boot-starter</module>
<module>workflow-engine-support</module>
<module>workflow-engine-spring-boot-starter</module>
</modules>
</project>

View File

@ -172,5 +172,5 @@ public interface ProcessModelApi {
*/
@Operation(summary = "查询模型的租户集合")
@GetMapping("/api/process/model/tenant/ids")
CommonResponse<List<String>> getTenantIds();
CommonResponse<List<String>> getModelTenantIds();
}

View File

@ -14,6 +14,7 @@ import cn.axzo.workflow.core.service.BpmnProcessInstanceService;
import cn.axzo.workflow.core.service.support.FlowNodeForecastService;
import cn.axzo.workflow.server.common.annotation.RepeatSubmit;
import cn.axzo.workflow.starter.api.WorkflowCoreService;
import cn.axzo.workflow.starter.api.WorkflowManageService;
import cn.azxo.framework.common.model.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.FlowElement;
@ -69,8 +70,8 @@ public class TestController {
private BpmnProcessInstanceService bpmnProcessInstanceService;
@Autowired
private WorkflowCoreService workflowCoreService;
// @Autowired
// private WorkflowManageService workflowCoreService;
@Autowired
private WorkflowManageService workflowManageService;
@RepeatSubmit
@GetMapping("/test")
@ -247,8 +248,7 @@ public class TestController {
@GetMapping("/tenant/get/ids")
public CommonResponse<List<String>> test11() {
workflowCoreService.sync();
// List<String> tenantIds = workflowCoreService.getTenantIds();
return CommonResponse.success();
List<String> tenantIds = workflowManageService.sync().getTenantIds();
return CommonResponse.success(tenantIds);
}
}

View File

@ -274,7 +274,7 @@ public class BpmnProcessModelController implements ProcessModelApi {
@Operation(summary = "查询模型的租户集合")
@GetMapping("/tenant/ids")
@Override
public CommonResponse<List<String>> getTenantIds() {
public CommonResponse<List<String>> getModelTenantIds() {
log.info("查询模型的租户集合getTenantIds");
return success(bpmnProcessModelService.getTenantIds());
}

View File

@ -7,7 +7,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
/**
* TODO
* 根据参数动态实例化可用 Bean
*
* @author wangli
* @since 2024/6/11 21:26
@ -16,16 +16,13 @@ import org.springframework.context.annotation.Configuration;
public class StarterFeignClientConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "workflow.engine.starter", value = "manageable", havingValue = "true", matchIfMissing = true)
@EnableFeignClients(clients = WorkflowCoreService.class)
public static class WorkflowCoreServiceClient {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "workflow.engine.starter", value = "manageable", havingValue = "false")
@ConditionalOnProperty(prefix = "workflow.engine.starter", value = "manageable", havingValue = "true")
@EnableFeignClients(clients = WorkflowManageService.class)
public static class WorkflowManageServiceClient {
}
}

View File

@ -15,9 +15,30 @@
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
</dependency>
<dependency>
<groupId>cn.axzo.workflow</groupId>
<artifactId>workflow-engine-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.3.0</version> <!-- 请检查并使用最新版本 -->
<executions>
<execution>
<id>run-custom-code</id>
<phase>process-classes</phase> <!-- 在 process-classes 阶段运行,可以根据需要调整 -->
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>cn.axzo.workflow.support.api.CodeGenerator</mainClass> <!-- 指定包含你要运行方法的类 -->
<arguments>
<!-- 如果需要传递参数给 main 方法,可以在这里添加 -->
</arguments>
<classpathScope>compile</classpathScope> <!-- 使用编译时的类路径 -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,17 @@
package cn.axzo.workflow.support.api;
/**
* Maven plugin execution entrance
*
* @author wangli
* @since 2024/6/12 10:56
*/
public class CodeGenerator {
public static void main(String[] args) throws Exception {
CoreServiceCodeGeneration.generate();
ManageServiceCodeGeneration.generate();
}
}

View File

@ -1,9 +1,5 @@
package cn.axzo.workflow.support.api;
import cn.axzo.workflow.client.config.WorkflowEngineClientAutoConfiguration;
import cn.axzo.workflow.common.annotation.Manageable;
import cn.axzo.workflow.starter.WorkflowEngineStarterAutoConfiguration;
import cn.axzo.workflow.starter.feign.ext.WorkflowEngineStarterFeignConfiguration;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
@ -24,10 +20,7 @@ import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.printer.DefaultPrettyPrinter;
import com.github.javaparser.printer.Printer;
import com.github.javaparser.utils.CodeGenerationUtils;
import com.github.javaparser.utils.SourceRoot;
import lombok.SneakyThrows;
import org.springframework.cloud.openfeign.FeignClient;
import java.io.FileWriter;
import java.io.IOException;
@ -42,21 +35,19 @@ import java.util.Optional;
* @author wangli
* @since 2024/6/7 17:14
*/
public class CoreServiceCodeGenerator {
public class CoreServiceCodeGeneration {
@SneakyThrows
public static void main(String[] args) {
public static void generate() throws Exception {
String newClassName = "WorkflowCoreService";
CompilationUnit originFile = parseOriginFile(newClassName);
// CompilationUnit originFile = parseOriginFile(newClassName);
CompilationUnit testGeneric = genericCode("cn.axzo.workflow.starter.api", newClassName);
writeToStarter(testGeneric, newClassName);
}
@SneakyThrows
private static CompilationUnit parseOriginFile(String newClassName) {
Path sourceCodeRoot = CodeGenerationUtils.mavenModuleRoot(WorkflowEngineStarterAutoConfiguration.class).resolve("src/main/java/cn/axzo/workflow/starter/api/");
private static CompilationUnit parseOriginFile(String newClassName) throws Exception {
String projectRootDir = System.getProperty("user.dir");
Path sourceCodeRoot = Paths.get(projectRootDir, "/workflow-engine-spring-boot-starter/src/main/java/cn/axzo/workflow/starter/api/");
SourceRoot sourceRoot = new SourceRoot(sourceCodeRoot);
sourceRoot.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8);
Printer printer = new DefaultPrettyPrinter();
@ -84,7 +75,7 @@ public class CoreServiceCodeGenerator {
}
}
public static CompilationUnit genericCode(String pkg, String newClassName) {
private static CompilationUnit genericCode(String pkg, String newClassName) throws Exception {
CompilationUnit cu = new CompilationUnit(pkg);
ClassOrInterfaceDeclaration interfaceDeclaration = setCommon(cu, newClassName);
@ -97,13 +88,13 @@ public class CoreServiceCodeGenerator {
MethodDeclaration sync = createDefaultMethod("sync", interfaceDeclaration);
sync.setJavadocComment("强制使用‘同步’模式调用该方法,请在调用真实方法前调用该方法\r\n" +
"<pre>\r\n" +
"workflowCoreService.sync().createProcessInstance();\r\n" +
" workflowCoreService.sync().createProcessInstance();\r\n" +
"</pre>");
MethodDeclaration async = createDefaultMethod("async", interfaceDeclaration);
sync.setJavadocComment("强制使用‘异步’模式调用该方法,请在调用真实方法前调用该方法\r\n" +
"<pre>\r\n" +
"workflowCoreService.async().createProcessInstance();\r\n" +
" workflowCoreService.async().createProcessInstance();\r\n" +
"</pre>");
}
@ -129,9 +120,9 @@ public class CoreServiceCodeGenerator {
return methodDeclaration;
}
@SneakyThrows
private static void addMethods(ClassOrInterfaceDeclaration interfaceDeclaration, CompilationUnit cu) {
Path sourceCodeRoot = CodeGenerationUtils.mavenModuleRoot(WorkflowEngineClientAutoConfiguration.class).resolve("src/main/java/cn/axzo/workflow/client/feign/");
private static void addMethods(ClassOrInterfaceDeclaration interfaceDeclaration, CompilationUnit cu) throws Exception {
String projectRootDir = System.getProperty("user.dir");
Path sourceCodeRoot = Paths.get(projectRootDir, "/workflow-engine-api/src/main/java/cn/axzo/workflow/client/feign/");
SourceRoot sourceRoot = new SourceRoot(sourceCodeRoot);
sourceRoot.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8);
Printer printer = new DefaultPrettyPrinter();
@ -141,13 +132,13 @@ public class CoreServiceCodeGenerator {
List<CompilationUnit> compilationUnits = sourceRoot.getCompilationUnits();
NodeList<BodyDeclaration<?>> targetMethods = interfaceDeclaration.getMembers();
for (CompilationUnit apiCU : compilationUnits) {
if (!apiCU.getPrimaryType().filter(e -> e.getAnnotationByClass(Manageable.class).isPresent()).isPresent()) {
if (!apiCU.getPrimaryType().filter(e -> e.getAnnotationByName("Manageable").isPresent()).isPresent()) {
addImports(apiCU, cu);
// 类上含有 @Manageable不进行解析
String interfaceName = apiCU.getPrimaryTypeName().get();
List<MethodDeclaration> methods = apiCU.getInterfaceByName(interfaceName).get().findAll(MethodDeclaration.class);
for (MethodDeclaration method : methods) {
if (!method.getAnnotationByClass(Manageable.class).isPresent()) {
if (!method.getAnnotationByName("Manageable").isPresent()) {
MethodDeclaration methodDeclaration = method.clone();
methodDeclaration.setType(changeReturnType(methodDeclaration.getType()));
targetMethods.add(methodDeclaration);
@ -177,10 +168,10 @@ public class CoreServiceCodeGenerator {
private static ClassOrInterfaceDeclaration setCommon(CompilationUnit cu, String newClassName) {
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = cu.addInterface(newClassName).setPublic(true);
classOrInterfaceDeclaration.setJavadocComment("Workflow Engine Starter Core Service");
classOrInterfaceDeclaration.addAndGetAnnotation(FeignClient.class)
.addPair("name", new StringLiteralExpr("workflow-engine-starter"))
classOrInterfaceDeclaration.addAndGetAnnotation("FeignClient")
.addPair("name", new StringLiteralExpr("workflow-engine-starter-core"))
.addPair("url", new StringLiteralExpr("${axzo.service.workflow-engine:workflow-engine:8080}"))
.addPair("configuration", new ClassExpr(new ClassOrInterfaceType(null, WorkflowEngineStarterFeignConfiguration.class.getSimpleName())));
.addPair("configuration", new ClassExpr(new ClassOrInterfaceType("WorkflowEngineStarterFeignConfiguration")));
cu.addImport("cn.axzo.workflow.starter.feign.ext.WorkflowEngineStarterFeignConfiguration", false, false);
cu.addImport("cn.axzo.workflow.common.util.ThreadUtil", false, false);
cu.addImport("cn.axzo.workflow.common.enums.RpcInvokeModeEnum.ASYNC", true, false);

View File

@ -1,8 +1,5 @@
package cn.axzo.workflow.support.api;
import cn.axzo.workflow.client.config.WorkflowEngineClientAutoConfiguration;
import cn.axzo.workflow.common.annotation.Manageable;
import cn.axzo.workflow.starter.feign.ext.WorkflowEngineStarterFeignConfiguration;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
@ -23,10 +20,7 @@ import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.printer.DefaultPrettyPrinter;
import com.github.javaparser.printer.Printer;
import com.github.javaparser.utils.CodeGenerationUtils;
import com.github.javaparser.utils.SourceRoot;
import lombok.SneakyThrows;
import org.springframework.cloud.openfeign.FeignClient;
import java.io.FileWriter;
import java.io.IOException;
@ -40,9 +34,9 @@ import java.util.List;
* @author wangli
* @since 2024/6/11 17:38
*/
public class ManageServiceCodeGenerator {
@SneakyThrows
public static void main(String[] args) {
public class ManageServiceCodeGeneration {
public static void generate() throws Exception {
String newClassName = "WorkflowManageService";
CompilationUnit testGeneric = genericCode("cn.axzo.workflow.starter.api", newClassName);
@ -60,7 +54,7 @@ public class ManageServiceCodeGenerator {
}
}
public static CompilationUnit genericCode(String pkg, String newClassName) {
private static CompilationUnit genericCode(String pkg, String newClassName) throws Exception {
CompilationUnit cu = new CompilationUnit(pkg);
ClassOrInterfaceDeclaration interfaceDeclaration = setCommon(cu, newClassName);
@ -73,20 +67,20 @@ public class ManageServiceCodeGenerator {
MethodDeclaration sync = createDefaultMethod("sync", interfaceDeclaration);
sync.setJavadocComment("强制使用‘同步’模式调用该方法,请在调用真实方法前调用该方法\r\n" +
"<pre>\r\n" +
"workflowCoreService.sync().createProcessInstance();\r\n" +
" workflowManageService.sync().getTenantIds();\r\n" +
"</pre>");
MethodDeclaration async = createDefaultMethod("async", interfaceDeclaration);
sync.setJavadocComment("强制使用‘异步’模式调用该方法,请在调用真实方法前调用该方法\r\n" +
async.setJavadocComment("强制使用‘异步’模式调用该方法,请在调用真实方法前调用该方法\r\n" +
"<pre>\r\n" +
"workflowCoreService.async().createProcessInstance();\r\n" +
" workflowManageService.async().getTenantIds();\r\n" +
"</pre>");
}
private static MethodDeclaration createDefaultMethod(String methodName, ClassOrInterfaceDeclaration interfaceDeclaration) {
// 将sync方法添加到WorkflowCoreService类中
MethodDeclaration methodDeclaration = interfaceDeclaration.addMethod(methodName, Modifier.Keyword.DEFAULT);
methodDeclaration.setType(new ClassOrInterfaceType("WorkflowCoreService"));
methodDeclaration.setType(new ClassOrInterfaceType("WorkflowManageService"));
// 创建方法体
BlockStmt methodBody = new BlockStmt();
@ -105,9 +99,9 @@ public class ManageServiceCodeGenerator {
return methodDeclaration;
}
@SneakyThrows
private static void addMethods(ClassOrInterfaceDeclaration interfaceDeclaration, CompilationUnit cu) {
Path sourceCodeRoot = CodeGenerationUtils.mavenModuleRoot(WorkflowEngineClientAutoConfiguration.class).resolve("src/main/java/cn/axzo/workflow/client/feign/");
private static void addMethods(ClassOrInterfaceDeclaration interfaceDeclaration, CompilationUnit cu) throws Exception {
String projectRootDir = System.getProperty("user.dir");
Path sourceCodeRoot = Paths.get(projectRootDir, "/workflow-engine-api/src/main/java/cn/axzo/workflow/client/feign/");
SourceRoot sourceRoot = new SourceRoot(sourceCodeRoot);
sourceRoot.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_8);
Printer printer = new DefaultPrettyPrinter();
@ -117,18 +111,30 @@ public class ManageServiceCodeGenerator {
List<CompilationUnit> compilationUnits = sourceRoot.getCompilationUnits();
NodeList<BodyDeclaration<?>> targetMethods = interfaceDeclaration.getMembers();
for (CompilationUnit apiCU : compilationUnits) {
if (!apiCU.getPrimaryType().filter(e -> e.getAnnotationByClass(Manageable.class).isPresent()).isPresent()) {
// 如果类上包含 @Manageable 注解则直接将所有方法解析进新目标接口中
if (apiCU.getPrimaryType().filter(e -> e.getAnnotationByName("Manageable").isPresent()).isPresent()) {
addImports(apiCU, cu);
// 类上含有 @Manageable不进行解析
String interfaceName = apiCU.getPrimaryTypeName().get();
List<MethodDeclaration> methods = apiCU.getInterfaceByName(interfaceName).get().findAll(MethodDeclaration.class);
for (MethodDeclaration method : methods) {
if (!method.getAnnotationByClass(Manageable.class).isPresent()) {
MethodDeclaration methodDeclaration = method.clone();
methodDeclaration.setType(changeReturnType(methodDeclaration.getType()));
targetMethods.add(methodDeclaration);
}
}
addMethod0(apiCU, targetMethods, false);
} else {
addImports(apiCU, cu);
addMethod0(apiCU, targetMethods, true);
}
}
}
private static void addMethod0(CompilationUnit apiCU, NodeList<BodyDeclaration<?>> targetMethods, boolean containAnnotation) {
String interfaceName = apiCU.getPrimaryTypeName().get();
List<MethodDeclaration> methods = apiCU.getInterfaceByName(interfaceName).get().findAll(MethodDeclaration.class);
for (MethodDeclaration method : methods) {
if (containAnnotation && method.getAnnotationByName("Manageable").isPresent()) {
MethodDeclaration methodDeclaration = method.clone();
methodDeclaration.setType(changeReturnType(methodDeclaration.getType()));
targetMethods.add(methodDeclaration);
} else if (!containAnnotation && !method.getAnnotationByName("Manageable").isPresent()) {
MethodDeclaration methodDeclaration = method.clone();
methodDeclaration.setType(changeReturnType(methodDeclaration.getType()));
targetMethods.add(methodDeclaration);
}
}
}
@ -153,10 +159,10 @@ public class ManageServiceCodeGenerator {
private static ClassOrInterfaceDeclaration setCommon(CompilationUnit cu, String newClassName) {
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = cu.addInterface(newClassName).setPublic(true);
classOrInterfaceDeclaration.setJavadocComment("Workflow Engine Starter Management Service");
classOrInterfaceDeclaration.addAndGetAnnotation(FeignClient.class)
.addPair("name", new StringLiteralExpr("workflow-engine-starter"))
classOrInterfaceDeclaration.addAndGetAnnotation("FeignClient")
.addPair("name", new StringLiteralExpr("workflow-engine-starter-manage"))
.addPair("url", new StringLiteralExpr("${axzo.service.workflow-engine:workflow-engine:8080}"))
.addPair("configuration", new ClassExpr(new ClassOrInterfaceType(null, WorkflowEngineStarterFeignConfiguration.class.getSimpleName())));
.addPair("configuration", new ClassExpr(new ClassOrInterfaceType("WorkflowEngineStarterFeignConfiguration")));
cu.addImport("cn.axzo.workflow.starter.feign.ext.WorkflowEngineStarterFeignConfiguration", false, false);
cu.addImport("cn.axzo.workflow.common.util.ThreadUtil", false, false);
cu.addImport("cn.axzo.workflow.common.enums.RpcInvokeModeEnum.ASYNC", true, false);