This is an automated email from the ASF dual-hosted git repository.

wenjun pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new 6823d26ef1 [Improvement-17855] Supports the custom and built-in 
variables for java task (#17860)
6823d26ef1 is described below

commit 6823d26ef1725c0d49a0fc9fa15a6872e894f9c4
Author: huangsheng <[email protected]>
AuthorDate: Thu Jan 8 20:55:22 2026 +0800

    [Improvement-17855] Supports the custom and built-in variables for java 
task (#17860)
---
 docs/docs/en/guide/task/java.md                    |   1 +
 docs/docs/zh/guide/task/java.md                    |   1 +
 .../plugin/task/java/JavaTask.java                 |  11 ++-
 .../plugin/task/java/JavaTaskTest.java             | 100 ++++++++++++++++-----
 4 files changed, 89 insertions(+), 24 deletions(-)

diff --git a/docs/docs/en/guide/task/java.md b/docs/docs/en/guide/task/java.md
index 6ff23d9d43..f399fb1d99 100644
--- a/docs/docs/en/guide/task/java.md
+++ b/docs/docs/en/guide/task/java.md
@@ -23,6 +23,7 @@ This node is used to execute tasks of the `Java` type and 
supports running `jar`
 | Main Class Name    | Fully Qualified Name of the Main Class (Optional)       
                                                                                
      |
 | Main Package       | Select the main program package to run the application. 
                                                                                
      |
 | Resources          | External JAR packages or other resource files that are 
added to the classpath or module path and can be easily retrieved in your JAVA 
script. |
+| Custom Parameters  | User-defined parameters local to JAVA, will replace 
content in the main program parameters and JVM parameters like ${variable}.     
          |
 
 ## Example
 
diff --git a/docs/docs/zh/guide/task/java.md b/docs/docs/zh/guide/task/java.md
index 36d9d155ed..a81ff6acca 100644
--- a/docs/docs/zh/guide/task/java.md
+++ b/docs/docs/zh/guide/task/java.md
@@ -25,6 +25,7 @@
 | 主类名      | 启动类的完整主类名(可选)                                                 |
 | 主程序包     | 选择要运行程序的主程序包                                                  |
 | 资源       | 可以是外部JAR包也可以是其他资源文件,它们都会被加入到类路径或模块路径中,您可以在自己的JAVA脚本中轻松获取      |
+| 自定义参数    | 是 Java 任务局部的用户自定义参数,会替换主程序参数和JVM参数中以 ${变量} 的内容。               |
 
 ## 任务样例
 
diff --git 
a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaTask.java
 
b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaTask.java
index 8ee2a341db..6668da91aa 100644
--- 
a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaTask.java
+++ 
b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaTask.java
@@ -27,17 +27,20 @@ import 
org.apache.dolphinscheduler.plugin.task.api.TaskCallBack;
 import org.apache.dolphinscheduler.plugin.task.api.TaskConstants;
 import org.apache.dolphinscheduler.plugin.task.api.TaskException;
 import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
+import org.apache.dolphinscheduler.plugin.task.api.model.Property;
 import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
 import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse;
 import 
org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
 import org.apache.dolphinscheduler.plugin.task.api.resource.ResourceContext;
 import 
org.apache.dolphinscheduler.plugin.task.api.shell.IShellInterceptorBuilder;
 import 
org.apache.dolphinscheduler.plugin.task.api.shell.ShellInterceptorBuilderFactory;
+import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils;
 import 
org.apache.dolphinscheduler.plugin.task.java.exception.RunTypeNotFoundException;
 
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.File;
+import java.util.Map;
 
 import lombok.extern.slf4j.Slf4j;
 
@@ -141,7 +144,7 @@ public class JavaTask extends AbstractTask {
                 .append("-jar").append(Constants.SPACE)
                 .append(mainJarAbsolutePathInLocal).append(Constants.SPACE)
                 .append(javaParameters.getMainArgs().trim());
-        return builder.toString();
+        return parseParameter(builder.toString());
     }
 
     /**
@@ -169,7 +172,7 @@ public class JavaTask extends AbstractTask {
                 .append(buildResourcePath()).append(Constants.SPACE)
                 .append(mainJarName).append(Constants.SPACE)
                 .append(javaParameters.getMainArgs().trim());
-        return builder.toString();
+        return parseParameter(builder.toString());
     }
 
     @Override
@@ -221,4 +224,8 @@ public class JavaTask extends AbstractTask {
         return JAVA_HOME_VAR + File.separator + "bin" + File.separator;
     }
 
+    private String parseParameter(String script) {
+        Map<String, Property> paramsMap = taskRequest.getPrepareParamsMap();
+        return ParameterUtils.convertParameterPlaceholders(script, 
ParameterUtils.convert(paramsMap));
+    }
 }
diff --git 
a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/test/java/org/apache/dolphinscheduler/plugin/task/java/JavaTaskTest.java
 
b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/test/java/org/apache/dolphinscheduler/plugin/task/java/JavaTaskTest.java
index 4027cb7df9..c8a7ecd95a 100644
--- 
a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/test/java/org/apache/dolphinscheduler/plugin/task/java/JavaTaskTest.java
+++ 
b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/test/java/org/apache/dolphinscheduler/plugin/task/java/JavaTaskTest.java
@@ -33,6 +33,7 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
@@ -71,6 +72,14 @@ class JavaTaskTest {
                         "${JAVA_HOME}/bin/java -xms:50m -classpath 
.:/tmp/dolphinscheduler/test/executepath:/tmp/dolphinscheduler/test/executepath/opt/share/jar/resource2.jar:/tmp/dolphinscheduler/test/executepath/opt/share/jar/main.jar
 Test -host 127.0.0.1 -port 8080");
     }
 
+    @Test
+    void testParameterPass() {
+        JavaTask javaTask = createJavaTaskWithCusVar();
+        assertThat(javaTask.buildNormalJarCommand())
+                .isEqualTo(
+                        "${JAVA_HOME}/bin/java -xms:1G -classpath 
.:/tmp/dolphinscheduler/test/executepath:/tmp/dolphinscheduler/test/executepath/opt/share/jar/resource2.jar
 Test -host 0.0.0.0 -port 12345");
+    }
+
     /**
      * add the Normal Jar parameters
      *
@@ -134,16 +143,26 @@ class JavaTaskTest {
         return javaParameters;
     }
 
-    /**
-     * The Java task to construct the jar run mode
-     *
-     * @return JavaTask
-     **/
-    private JavaTask runJarType() {
-        TaskExecutionContext taskExecutionContext = new TaskExecutionContext();
-        
taskExecutionContext.setTaskParams(JSONUtils.toJsonString(createJavaParametersObject(RUN_TYPE_FAT_JAR)));
-        
taskExecutionContext.setExecutePath("/tmp/dolphinscheduler/test/executepath");
-        taskExecutionContext.setTaskAppId("runJavaType");
+    private JavaParameters createJavaTaskWithCusVar(String runType) {
+        JavaParameters javaParameters = new JavaParameters();
+        javaParameters.setRunType(runType);
+        javaParameters.setModulePath(false);
+        javaParameters.setJvmArgs("-xms:${max_mem}");
+        javaParameters.setMainArgs("-host ${host} -port ${port}");
+        ResourceInfo resourceJar = new ResourceInfo();
+        resourceJar.setResourceName("/opt/share/jar/resource2.jar");
+        ArrayList<ResourceInfo> resourceInfoArrayList = new ArrayList<>();
+        resourceInfoArrayList.add(resourceJar);
+        javaParameters.setResourceList(resourceInfoArrayList);
+        ArrayList<Property> localParams = new ArrayList<>();
+        javaParameters.setLocalParams(localParams);
+        ResourceInfo mainJar = new ResourceInfo();
+        mainJar.setResourceName("/opt/share/jar/main.jar");
+        javaParameters.setMainJar(mainJar);
+        return javaParameters;
+    }
+
+    private ResourceContext createResourceContext() {
         ResourceContext.ResourceItem resourceItem1 = new 
ResourceContext.ResourceItem();
         
resourceItem1.setResourceAbsolutePathInStorage("/opt/share/jar/resource2.jar");
         resourceItem1
@@ -156,8 +175,21 @@ class JavaTaskTest {
         ResourceContext resourceContext = new ResourceContext();
         resourceContext.addResourceItem(resourceItem1);
         resourceContext.addResourceItem(resourceItem2);
-        taskExecutionContext.setResourceContext(resourceContext);
+        return resourceContext;
+    }
 
+    /**
+     * The Java task to construct the jar run mode
+     *
+     * @return JavaTask
+     **/
+    private JavaTask runJarType() {
+        TaskExecutionContext taskExecutionContext = new TaskExecutionContext();
+        
taskExecutionContext.setTaskParams(JSONUtils.toJsonString(createJavaParametersObject(RUN_TYPE_FAT_JAR)));
+        
taskExecutionContext.setExecutePath("/tmp/dolphinscheduler/test/executepath");
+        taskExecutionContext.setTaskAppId("runJavaType");
+        ResourceContext resourceContext = createResourceContext();
+        taskExecutionContext.setResourceContext(resourceContext);
         JavaTask javaTask = new JavaTask(taskExecutionContext);
         javaTask.init();
         return javaTask;
@@ -174,20 +206,44 @@ class JavaTaskTest {
         
taskExecutionContext.setTaskParams(JSONUtils.toJsonString(createNormalJarJavaParameters(RUN_TYPE_NORMAL_JAR)));
         
taskExecutionContext.setExecutePath("/tmp/dolphinscheduler/test/executepath");
         taskExecutionContext.setTaskAppId("runJavaType");
-        ResourceContext.ResourceItem resourceItem1 = new 
ResourceContext.ResourceItem();
-        
resourceItem1.setResourceAbsolutePathInStorage("/opt/share/jar/resource2.jar");
-        resourceItem1
-                
.setResourceAbsolutePathInLocal("/tmp/dolphinscheduler/test/executepath/opt/share/jar/resource2.jar");
+        ResourceContext resourceContext = createResourceContext();
+        taskExecutionContext.setResourceContext(resourceContext);
+        JavaTask javaTask = new JavaTask(taskExecutionContext);
+        javaTask.init();
+        return javaTask;
+    }
 
-        ResourceContext.ResourceItem resourceItem2 = new 
ResourceContext.ResourceItem();
-        
resourceItem2.setResourceAbsolutePathInStorage("/opt/share/jar/main.jar");
-        
resourceItem2.setResourceAbsolutePathInLocal("/tmp/dolphinscheduler/test/executepath/opt/share/jar/main.jar");
+    private JavaTask createJavaTaskWithCusVar() {
+        TaskExecutionContext taskExecutionContext = new TaskExecutionContext();
+        
taskExecutionContext.setTaskParams(JSONUtils.toJsonString(createJavaTaskWithCusVar(RUN_TYPE_NORMAL_JAR)));
+        
taskExecutionContext.setExecutePath("/tmp/dolphinscheduler/test/executepath");
+        taskExecutionContext.setTaskAppId("runJavaType");
+        Property property = new Property();
+        property.setProp("max_mem");
+        property.setValue("1G");
+        property.setDirect(IN);
+        property.setType(VARCHAR);
 
-        ResourceContext resourceContext = new ResourceContext();
-        resourceContext.addResourceItem(resourceItem1);
-        resourceContext.addResourceItem(resourceItem2);
-        taskExecutionContext.setResourceContext(resourceContext);
+        Property property2 = new Property();
+        property2.setProp("host");
+        property2.setValue("0.0.0.0");
+        property2.setDirect(IN);
+        property2.setType(VARCHAR);
+
+        Property property3 = new Property();
+        property3.setProp("port");
+        property3.setValue("12345");
+        property3.setDirect(IN);
+        property3.setType(VARCHAR);
 
+        HashMap<String, Property> prepareParamsMap = new HashMap<>();
+        prepareParamsMap.put("max_mem", property);
+        prepareParamsMap.put("host", property2);
+        prepareParamsMap.put("port", property3);
+
+        taskExecutionContext.setPrepareParamsMap(prepareParamsMap);
+        ResourceContext resourceContext = createResourceContext();
+        taskExecutionContext.setResourceContext(resourceContext);
         JavaTask javaTask = new JavaTask(taskExecutionContext);
         javaTask.init();
         return javaTask;

Reply via email to