From c1f5a142b68aaaffb0c62d4295d94dc29a73c674 Mon Sep 17 00:00:00 2001 From: Jia Fan Date: Wed, 1 Oct 2025 22:18:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20maven=20plugin=20protoSourceRoot?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81=E8=AF=BB=E5=8F=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../trpc/codegen/TRpcCodeGenerator.java | 2 +- .../maven/plugin/TRpcMavenPluginTest.java | 20 ++++++++++++++ .../src/test/resources/TEST-5/pom.xml | 26 +++++++++++++++++++ .../TEST-5/src/main/proto/module1/hello.proto | 16 ++++++++++++ .../TEST-5/src/main/proto/module2/hello.proto | 16 ++++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 trpc-maven-plugin/src/test/resources/TEST-5/pom.xml create mode 100644 trpc-maven-plugin/src/test/resources/TEST-5/src/main/proto/module1/hello.proto create mode 100644 trpc-maven-plugin/src/test/resources/TEST-5/src/main/proto/module2/hello.proto diff --git a/trpc-code-generator/src/main/java/com/tencent/trpc/codegen/TRpcCodeGenerator.java b/trpc-code-generator/src/main/java/com/tencent/trpc/codegen/TRpcCodeGenerator.java index c999d0a62b..8da46b67f7 100644 --- a/trpc-code-generator/src/main/java/com/tencent/trpc/codegen/TRpcCodeGenerator.java +++ b/trpc-code-generator/src/main/java/com/tencent/trpc/codegen/TRpcCodeGenerator.java @@ -122,7 +122,7 @@ public void generateCode() throws TRpcCodeGenerateException { try { Files.createDirectories(tmpPath); List importPaths = prepareImportPaths(); - Path descriptorFile = generateDescriptorFile(getProtoFiles(true), importPaths); + Path descriptorFile = generateDescriptorFile(getProtoFiles(false), importPaths); Files.createDirectories(tmpOutPath); List fdList = compileDescriptorSet(descriptorFile); Map customVariables = codeGeneratorHook.getCustomVariables(fdList); diff --git a/trpc-maven-plugin/src/test/java/com/tencent/trpc/maven/plugin/TRpcMavenPluginTest.java b/trpc-maven-plugin/src/test/java/com/tencent/trpc/maven/plugin/TRpcMavenPluginTest.java index 8aaff15853..046052ea51 100644 --- a/trpc-maven-plugin/src/test/java/com/tencent/trpc/maven/plugin/TRpcMavenPluginTest.java +++ b/trpc-maven-plugin/src/test/java/com/tencent/trpc/maven/plugin/TRpcMavenPluginTest.java @@ -107,6 +107,26 @@ public void test4() throws Exception { assertTrue(Files.exists(base.resolve("pom.xml"))); } + /** + * Simulate 'mvn trpc:gen-code' on maven project at src/test/resources/TEST-5. + */ + public void test5() throws Exception { + executeTest("TEST-5"); + Path base = Paths.get("src", "test", "resources", "TEST-5", "target", "generated-sources", + "trpc", "java").toAbsolutePath(); + Path output = base.resolve(Paths.get("com", "tencent", "test", "helloworld")); + assertTrue(Files.exists(output)); + assertTrue(Files.exists(output.resolve("module1/GreeterAPI.java"))); + assertTrue(Files.exists(output.resolve("module1/GreeterAsyncAPI.java"))); + assertTrue(Files.exists(output.resolve("module1/GreeterSvr.java"))); + assertTrue(Files.exists(output.resolve("module1/GreeterSvrValidator.java"))); + assertTrue(Files.exists(output.resolve("module2/GreeterAPI.java"))); + assertTrue(Files.exists(output.resolve("module2/GreeterAsyncAPI.java"))); + assertTrue(Files.exists(output.resolve("module2/GreeterSvr.java"))); + assertTrue(Files.exists(output.resolve("module2/GreeterSvrValidator.java"))); + assertTrue(Files.exists(base.resolve("pom.xml"))); + } + private void executeTest(String root) throws Exception { MavenProject project = readMavenProject(new File("src/test/resources/" + root)); MavenSession session = newMavenSession(project); diff --git a/trpc-maven-plugin/src/test/resources/TEST-5/pom.xml b/trpc-maven-plugin/src/test/resources/TEST-5/pom.xml new file mode 100644 index 0000000000..96c70c0cda --- /dev/null +++ b/trpc-maven-plugin/src/test/resources/TEST-5/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + org.example + trpc-maven-plugin-test + 1.0-SNAPSHOT + + + 8 + 8 + + + + + + com.tencent.trpc + trpc-maven-plugin + + + + + + \ No newline at end of file diff --git a/trpc-maven-plugin/src/test/resources/TEST-5/src/main/proto/module1/hello.proto b/trpc-maven-plugin/src/test/resources/TEST-5/src/main/proto/module1/hello.proto new file mode 100644 index 0000000000..1251c6b4b7 --- /dev/null +++ b/trpc-maven-plugin/src/test/resources/TEST-5/src/main/proto/module1/hello.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; +package trpc.test.helloworld.module1; +import "validate.proto"; +option java_package = "com.tencent.test.helloworld.module1"; +option java_outer_classname = "GreeterSvr"; +// 请求协议头 +message HelloRequest { + string message = 1 [(validate.rules).string.min_len = 2]; +} +// 响应协议头 +message HelloResponse { + string message = 1; +} +service Greeter { + rpc sayHello (HelloRequest) returns (HelloResponse); +} \ No newline at end of file diff --git a/trpc-maven-plugin/src/test/resources/TEST-5/src/main/proto/module2/hello.proto b/trpc-maven-plugin/src/test/resources/TEST-5/src/main/proto/module2/hello.proto new file mode 100644 index 0000000000..f59605a2ef --- /dev/null +++ b/trpc-maven-plugin/src/test/resources/TEST-5/src/main/proto/module2/hello.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; +package trpc.test.helloworld.module2; +import "validate.proto"; +option java_package = "com.tencent.test.helloworld.module2"; +option java_outer_classname = "GreeterSvr"; +// 请求协议头 +message HelloRequest { + string message = 1 [(validate.rules).string.min_len = 2]; +} +// 响应协议头 +message HelloResponse { + string message = 1; +} +service Greeter { + rpc sayHello (HelloRequest) returns (HelloResponse); +} \ No newline at end of file