diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaConstants.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaConstants.java index e9bfd581c6492..243a0edae8855 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaConstants.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaConstants.java @@ -29,14 +29,14 @@ private JavaConstants() { public static final String JAVA_HOME_VAR = "${JAVA_HOME}"; /** - * this constant represents the use of the java command to run a task + * This constant represents the use of the java -jar command to run a task **/ - public static final String RUN_TYPE_JAVA = "JAVA"; + public static final String RUN_TYPE_FAT_JAR = "FATJAR"; /** - * this constant represents the use of the java -jar command to run a task + * This constant represents the use of the java -cp command to run a task **/ - public static final String RUN_TYPE_JAR = "JAR"; + public static final String RUN_TYPE_NORMAL_JAR = "NORMALJAR"; /** * This constant is the Classpath or module path delimiter for different operating systems @@ -48,13 +48,4 @@ private JavaConstants() { **/ public static final String CLASSPATH_CURRENT_DIR = "."; - /** - * This constant is used to construct the pre-pathname of the Java source file - **/ - public static final String JAVA_SOURCE_CODE_NAME_TEMPLATE = "%s/%s.java"; - - /** - * This constant is the regular expression to get the class name of the source file - **/ - public static final String PUBLIC_CLASS_NAME_REGEX = "(.*\\s*public\\s+class\\s+)([a-zA-Z_]+[//w_]*)([.\\s\\S]*)"; } diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaParameters.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaParameters.java index 8ac5dc5fe7ad5..26d143e47dca1 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaParameters.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/JavaParameters.java @@ -20,8 +20,6 @@ import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo; import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters; -import org.apache.commons.lang3.StringUtils; - import java.util.List; import lombok.Data; @@ -29,11 +27,6 @@ @Data public class JavaParameters extends AbstractParameters { - /** - * origin java script - */ - private String rawScript; - /** * run in jar file */ @@ -67,11 +60,12 @@ public class JavaParameters extends AbstractParameters { /** * Check that the parameters are valid * - * @returnboolean + * @return boolean */ @Override public boolean checkParameters() { - return runType != null && (StringUtils.isNotBlank(rawScript) || mainJar != null); + return runType != null && (runType.equals(JavaConstants.RUN_TYPE_FAT_JAR) || + runType.equals(JavaConstants.RUN_TYPE_NORMAL_JAR)); } /** 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 6423452a58750..53de0068bb8b1 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 @@ -18,7 +18,6 @@ package org.apache.dolphinscheduler.plugin.task.java; import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.JAVA_HOME_VAR; -import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.PUBLIC_CLASS_NAME_REGEX; import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.plugin.task.api.AbstractTask; @@ -27,35 +26,19 @@ 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.MapUtils; -import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; -import org.apache.dolphinscheduler.plugin.task.java.exception.JavaSourceFileExistException; -import org.apache.dolphinscheduler.plugin.task.java.exception.PublicClassNotFoundException; import org.apache.dolphinscheduler.plugin.task.java.exception.RunTypeNotFoundException; -import org.apache.commons.io.FileUtils; - import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import lombok.extern.slf4j.Slf4j; - import com.google.common.base.Preconditions; + @Slf4j public class JavaTask extends AbstractTask { @@ -74,10 +57,6 @@ public class JavaTask extends AbstractTask { */ private TaskExecutionContext taskRequest; - /** - * class name regex pattern - */ - private static final Pattern classNamePattern = Pattern.compile(PUBLIC_CLASS_NAME_REGEX); public JavaTask(TaskExecutionContext taskRequest) { super(taskRequest); @@ -109,19 +88,20 @@ public void init() { public void handle(TaskCallBack taskCallBack) throws TaskException { try { // Step 1: judge if is java or jar run type. - // Step 2 case1: the jar run type builds the command directly, adding resource to the java -jar class when + // Step 2 case1: the fat jar run type builds the command directly, adding resource to the java -jar class when + // building the command + // Step 2 case2: the normal jar run type builds the command directly, adding resource to the java -cp class when // building the command - // Step 2 case2: the java run type, first replace the custom parameters, then compile the code, and then - // build the command will add resource // Step 3: to run the command String command = null; switch (javaParameters.getRunType()) { - case JavaConstants.RUN_TYPE_JAVA: - command = buildJavaCommand(); - break; - case JavaConstants.RUN_TYPE_JAR: + + case JavaConstants.RUN_TYPE_FAT_JAR: command = buildJarCommand(); break; + case JavaConstants.RUN_TYPE_NORMAL_JAR: + command = buildNormalJarCommand(); + break; default: throw new RunTypeNotFoundException("run type is required, but it is null now."); } @@ -149,44 +129,52 @@ public void handle(TaskCallBack taskCallBack) throws TaskException { } } + /** - * Construct a shell command for the java Run mode + * Construct a shell command for the java -jar Run mode * * @return String - * @throws Exception **/ - protected String buildJavaCommand() throws Exception { + protected String buildJarCommand() { + ResourceContext resourceContext = taskRequest.getResourceContext(); + String mainJarAbsolutePathInLocal = resourceContext + .getResourceItem(javaParameters.getMainJar().getResourceName()) + .getResourceAbsolutePathInLocal(); StringBuilder builder = new StringBuilder(); - String sourceCode = buildJavaSourceContent(); - builder.append(buildJavaCompileCommand(sourceCode)) - .append(";") - .append(getJavaCommandPath()) + builder.append(getJavaCommandPath()) .append("java").append(" ") - .append(buildResourcePath()) - .append(" ") - .append(getPublicClassName(sourceCode)) - .append(" ") + .append(buildResourcePath()).append(" ") + .append("-jar").append(" ") + .append(mainJarAbsolutePathInLocal).append(" ") .append(javaParameters.getMainArgs().trim()).append(" ") .append(javaParameters.getJvmArgs().trim()); return builder.toString(); } + /** - * Construct a shell command for the java -jar Run mode + * Construct a shell command for the java -cp run mode * * @return String **/ - protected String buildJarCommand() { + protected String buildNormalJarCommand() { ResourceContext resourceContext = taskRequest.getResourceContext(); - String mainJarAbsolutePathInLocal = resourceContext - .getResourceItem(javaParameters.getMainJar().getResourceName()) + String mainJarAbsolutePathInLocal = resourceContext.getResourceItem( + javaParameters.getMainJar() + .getResourceName()) .getResourceAbsolutePathInLocal(); + String mainJarName=null; + try{ + mainJarName = MainClassExtractor.getMainClassName(mainJarAbsolutePathInLocal); + } + catch (Exception e){ + e.printStackTrace(); + } StringBuilder builder = new StringBuilder(); builder.append(getJavaCommandPath()) .append("java").append(" ") .append(buildResourcePath()).append(" ") - .append("-jar").append(" ") - .append(mainJarAbsolutePathInLocal).append(" ") + .append(mainJarName).append(" ") .append(javaParameters.getMainArgs().trim()).append(" ") .append(javaParameters.getJvmArgs().trim()); return builder.toString(); @@ -207,35 +195,6 @@ public AbstractParameters getParameters() { return javaParameters; } - /** - * Creates a Java source file when it does not exist - * - * @param sourceCode - * @param fileName - * @return String - **/ - protected void createJavaSourceFileIfNotExists(String sourceCode, String fileName) throws IOException { - log.info("tenantCode: {}, task dir:{}", taskRequest.getTenantCode(), taskRequest.getExecutePath()); - if (!Files.exists(Paths.get(fileName))) { - log.info("the java source code:{}, will be write to the file: {}", fileName, sourceCode); - // write data to file - FileUtils.writeStringToFile(new File(fileName), - sourceCode, - StandardCharsets.UTF_8); - } else { - throw new JavaSourceFileExistException("java source file exists, please report an issue on official."); - } - } - - /** - * Construct the full path name of the Java source file from the temporary execution path of the task - * - * @return String - **/ - protected String buildJavaSourceCodeFileFullName(String publicClassName) { - return String.format(JavaConstants.JAVA_SOURCE_CODE_NAME_TEMPLATE, taskRequest.getExecutePath(), - publicClassName); - } /** * Construct a Classpath or module path based on isModulePath @@ -262,46 +221,6 @@ protected String buildResourcePath() { return builder.toString(); } - /** - * Constructs a shell command compiled from a Java source file - * - * @param sourceCode - * @return String - * @throws IOException - **/ - protected String buildJavaCompileCommand(String sourceCode) throws IOException { - String publicClassName = getPublicClassName(sourceCode); - String fileName = buildJavaSourceCodeFileFullName(publicClassName); - createJavaSourceFileIfNotExists(sourceCode, fileName); - - StringBuilder compilerCommand = new StringBuilder() - .append(getJavaCommandPath()) - .append("javac").append(" ") - .append(buildResourcePath()).append(" ") - .append(fileName); - return compilerCommand.toString(); - } - - /** - * Work with Java source file content, such as replacing local variables - * - * @return String - **/ - protected String buildJavaSourceContent() { - String rawJavaScript = javaParameters.getRawScript().replaceAll("\\r\\n", "\n"); - // replace placeholder - - Map paramsMap = taskRequest.getPrepareParamsMap(); - if (MapUtils.isEmpty(paramsMap)) { - paramsMap = new HashMap<>(); - } - if (MapUtils.isNotEmpty(taskRequest.getParamsMap())) { - paramsMap.putAll(taskRequest.getParamsMap()); - } - log.info("The current java source code will begin to replace the placeholder: {}", rawJavaScript); - rawJavaScript = ParameterUtils.convertParameterPlaceholders(rawJavaScript, ParameterUtils.convert(paramsMap)); - return rawJavaScript; - } /** * Gets the operating system absolute path to the Java command @@ -312,17 +231,5 @@ private String getJavaCommandPath() { return JAVA_HOME_VAR + File.separator + "bin" + File.separator; } - /** - * Gets the public class name from the Java source file - * - * @param sourceCode - * @return String - **/ - public String getPublicClassName(String sourceCode) { - Matcher matcher = classNamePattern.matcher(sourceCode); - if (!matcher.find()) { - throw new PublicClassNotFoundException("public class is not be found in source code : " + sourceCode); - } - return matcher.group(2).trim(); - } + } diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/MainClassExtractor.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/MainClassExtractor.java new file mode 100644 index 0000000000000..adb324e7115c4 --- /dev/null +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/MainClassExtractor.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dolphinscheduler.plugin.task.java; + +import java.io.File; +import java.io.IOException; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +public class MainClassExtractor { + + public static String getMainClassName(String jarFilePath) throws IOException { + JarFile jarFile = new JarFile(new File(jarFilePath)); + Manifest manifest = jarFile.getManifest(); + String mainClassName = manifest.getMainAttributes().getValue("Main-Class"); + jarFile.close(); + return mainClassName; + } +} 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 b8af840c69f1d..1c0131dfb318a 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 @@ -20,61 +20,20 @@ import static com.google.common.truth.Truth.assertThat; import static org.apache.dolphinscheduler.plugin.task.api.enums.DataType.VARCHAR; import static org.apache.dolphinscheduler.plugin.task.api.enums.Direct.IN; -import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.RUN_TYPE_JAR; -import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.RUN_TYPE_JAVA; +import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.*; -import org.apache.dolphinscheduler.common.utils.FileUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils; -import org.apache.dolphinscheduler.plugin.task.api.TaskCallBack; import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; -import org.apache.dolphinscheduler.plugin.task.api.model.ApplicationInfo; 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.resource.ResourceContext; -import org.apache.dolphinscheduler.plugin.task.java.exception.JavaSourceFileExistException; -import org.apache.dolphinscheduler.plugin.task.java.exception.PublicClassNotFoundException; -import org.apache.dolphinscheduler.plugin.task.java.exception.RunTypeNotFoundException; -import java.io.IOException; -import java.lang.reflect.Field; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class JavaTaskTest { - private TaskCallBack taskCallBack = new TaskCallBack() { - - @Override - public void updateRemoteApplicationInfo(int taskInstanceId, ApplicationInfo applicationInfo) { - - } - - @Override - public void updateTaskInstanceInfo(int taskInstanceId) { - - } - }; - - @Test - public void testGetPubllicClassName() { - JavaTask javaTask = runJavaType(); - Assertions.assertEquals(javaTask.getPublicClassName("import java.io.IOException;\n" + - "public class JavaTaskTest {\n" + - " public static void main(String[] args) throws IOException {\n" + - " StringBuilder builder = new StringBuilder(\"Hello: \");\n" + - " for (String arg : args) {\n" + - " builder.append(arg).append(\" \");\n" + - " }\n" + - " System.out.println(builder);\n" + - " }\n" + - "}\n"), "JavaTaskTest"); - } - /** * Construct a java -jar command * @@ -89,125 +48,63 @@ public void buildJarCommand() { } /** - * Construct the compile command - * - * @return void - **/ - @Test - public void buildJavaCompileCommand() throws IOException { - JavaTask javaTask = runJavaType(); - String sourceCode = javaTask.buildJavaSourceContent(); - String publicClassName = javaTask.getPublicClassName(sourceCode); - Assertions.assertEquals("JavaTaskTest", publicClassName); - String fileName = javaTask.buildJavaSourceCodeFileFullName(publicClassName); - try { - Path path = Paths.get(fileName); - if (Files.exists(path)) { - Files.delete(path); - } - assertThat(javaTask.buildJavaCompileCommand(sourceCode)) - .isEqualTo( - "${JAVA_HOME}/bin/javac -classpath .:/tmp/dolphinscheduler/test/executepath:/tmp/dolphinscheduler/test/executepath/opt/share/jar/resource2.jar /tmp/dolphinscheduler/test/executepath/JavaTaskTest.java"); - } finally { - Path path = Paths.get(fileName); - if (Files.exists(path)) { - Files.delete(path); - } - } - - } - - /** - * Construct java to run the command + * Construct a java -cp command * * @return void - **/ + */ @Test - public void buildJavaCommand() throws Exception { - JavaTask javaTask = runJavaType(); - String sourceCode = javaTask.buildJavaSourceContent(); - String publicClassName = javaTask.getPublicClassName(sourceCode); - - Assertions.assertEquals("JavaTaskTest", publicClassName); - - String fileName = javaTask.buildJavaSourceCodeFileFullName(publicClassName); - Path path = Paths.get(fileName); - if (Files.exists(path)) { - Files.delete(path); + public void buildNormalJarCommand() { + JavaTask javaTask = runNormalJarType(); + try{ + assertThat(javaTask.buildNormalJarCommand()) + .isEqualTo("${JAVA_HOME}/bin/java -classpath .:/tmp/dolphinscheduler/test/executepath:/tmp/dolphinscheduler/test/executepath/opt/share/jar/resource2.jar:/tmp/dolphinscheduler/test/executepath/opt/share/jar/main.jar HelloWorldWithGuava -host 127.0.0.1 -port 8080 -xms:50m"); + } + catch(Exception e){ + e.printStackTrace(); } - assertThat(javaTask.buildJavaCommand()) - .isEqualTo( - "${JAVA_HOME}/bin/javac -classpath .:/tmp/dolphinscheduler/test/executepath:/tmp/dolphinscheduler/test/executepath/opt/share/jar/resource2.jar /tmp/dolphinscheduler/test/executepath/JavaTaskTest.java;${JAVA_HOME}/bin/java -classpath .:/tmp/dolphinscheduler/test/executepath:/tmp/dolphinscheduler/test/executepath/opt/share/jar/resource2.jar JavaTaskTest -host 127.0.0.1 -port 8080 -xms:50m"); - } - /** - * There is no exception to overwriting the Java source file - * - * @return void - * @throws IOException - **/ - @Test - public void coverJavaSourceFileExistException() throws IOException { - JavaTask javaTask = runJavaType(); - String sourceCode = javaTask.buildJavaSourceContent(); - String publicClassName = javaTask.getPublicClassName(sourceCode); - Assertions.assertEquals("JavaTaskTest", publicClassName); - String fileName = javaTask.buildJavaSourceCodeFileFullName(publicClassName); - - Assertions.assertThrows(JavaSourceFileExistException.class, () -> { - try { - Path path = Paths.get(fileName); - if (!Files.exists(path)) { - FileUtils.createDirectoryWith755(path); - } - javaTask.createJavaSourceFileIfNotExists(sourceCode, fileName); - } finally { - Path path = Paths.get(fileName); - if (Files.exists(path)) { - Files.delete(path); - } - } - }); } /** - * The override class name could not find an exception + * add the Normal Jar parameters * - * @return void - **/ - @Test - public void coverPublicClassNotFoundException() { - Assertions.assertThrows(PublicClassNotFoundException.class, () -> { - JavaTask javaTask = runJavaType(); - javaTask.getPublicClassName(""); - }); + * @param runType + * @return + */ + public JavaParameters createNormalJarJavaParameters(String runType) { + JavaParameters javaParameters = new JavaParameters(); + javaParameters.setRunType(runType); + javaParameters.setModulePath(false); + javaParameters.setJvmArgs("-xms:50m"); + javaParameters.setMainArgs("-host 127.0.0.1 -port 8080"); + ResourceInfo resourceJar1 = new ResourceInfo(); + resourceJar1.setResourceName("/opt/share/jar/resource2.jar"); + ResourceInfo resourceJar2 = new ResourceInfo(); + resourceJar2.setResourceName("/opt/share/jar/main.jar"); + ArrayList resourceInfoArrayList = new ArrayList<>(); + resourceInfoArrayList.add(resourceJar1); + resourceInfoArrayList.add(resourceJar2); + javaParameters.setResourceList(resourceInfoArrayList); + ArrayList localParams = new ArrayList<>(); + Property property = new Property(); + property.setProp("name"); + property.setValue("zhangsan"); + property.setDirect(IN); + property.setType(VARCHAR); + javaParameters.setLocalParams(localParams); + ResourceInfo mainJar = new ResourceInfo(); + mainJar.setResourceName("/opt/share/jar/main.jar"); + javaParameters.setMainJar(mainJar); + return javaParameters; } - /** - * The override run mode could not find an exception - * - * @return void - * @throws Exception - **/ - @Test - public void coverRunTypeNotFoundException() throws Exception { - JavaTask javaTask = runJavaType(); - Field javaParameters = JavaTask.class.getDeclaredField("javaParameters"); - javaParameters.setAccessible(true); - ((JavaParameters) (javaParameters.get(javaTask))).setRunType(""); - - Assertions.assertThrows(RunTypeNotFoundException.class, () -> { - javaTask.handle(taskCallBack); - javaTask.getPublicClassName(""); - }); - } /** - * Create a Java task parameter mock object + * Add the fat jar parameters * * @param runType - * @return JavaParameters - **/ + * @return + */ public JavaParameters createJavaParametersObject(String runType) { JavaParameters javaParameters = new JavaParameters(); javaParameters.setRunType(runType); @@ -219,16 +116,6 @@ public JavaParameters createJavaParametersObject(String runType) { ArrayList resourceInfoArrayList = new ArrayList<>(); resourceInfoArrayList.add(resourceJar); javaParameters.setResourceList(resourceInfoArrayList); - javaParameters.setRawScript( - "import java.io.IOException;\n" + - "public class JavaTaskTest {\n" + - " public static void main(String[] args) throws IOException {\n" + - " StringBuilder builder = new StringBuilder(\"Hello: \");\n" + - " for (String arg : args) {\n" + - " builder.append(arg).append(\" \");\n" + - " }\n" + " System.out.println(builder);\n" + - " }\n" + - "}\n"); ArrayList localParams = new ArrayList<>(); Property property = new Property(); property.setProp("name"); @@ -243,13 +130,13 @@ public JavaParameters createJavaParametersObject(String runType) { } /** - * A Java task that constructs the Java runtime pattern + * The Java task to construct the jar run mode * * @return JavaTask **/ - public JavaTask runJavaType() { + private JavaTask runJarType() { TaskExecutionContext taskExecutionContext = new TaskExecutionContext(); - taskExecutionContext.setTaskParams(JSONUtils.toJsonString(createJavaParametersObject(RUN_TYPE_JAVA))); + taskExecutionContext.setTaskParams(JSONUtils.toJsonString(createJavaParametersObject(RUN_TYPE_FAT_JAR))); taskExecutionContext.setExecutePath("/tmp/dolphinscheduler/test/executepath"); taskExecutionContext.setTaskAppId("runJavaType"); ResourceContext.ResourceItem resourceItem1 = new ResourceContext.ResourceItem(); @@ -261,28 +148,24 @@ public JavaTask runJavaType() { resourceItem2.setResourceAbsolutePathInStorage("/opt/share/jar/main.jar"); resourceItem2.setResourceAbsolutePathInLocal("/tmp/dolphinscheduler/test/executepath/opt/share/jar/main.jar"); - ResourceContext.ResourceItem resourceItem3 = new ResourceContext.ResourceItem(); - resourceItem3.setResourceAbsolutePathInStorage("/JavaTaskTest.java"); - resourceItem3.setResourceAbsolutePathInLocal("/tmp/dolphinscheduler/test/executepath/JavaTaskTest.java"); - ResourceContext resourceContext = new ResourceContext(); resourceContext.addResourceItem(resourceItem1); resourceContext.addResourceItem(resourceItem2); - resourceContext.addResourceItem(resourceItem3); taskExecutionContext.setResourceContext(resourceContext); + JavaTask javaTask = new JavaTask(taskExecutionContext); javaTask.init(); return javaTask; } /** - * The Java task to construct the jar run mode + * The Java task to construct the normal jar run mode * * @return JavaTask - **/ - private JavaTask runJarType() { + */ + private JavaTask runNormalJarType() { TaskExecutionContext taskExecutionContext = new TaskExecutionContext(); - taskExecutionContext.setTaskParams(JSONUtils.toJsonString(createJavaParametersObject(RUN_TYPE_JAR))); + taskExecutionContext.setTaskParams(JSONUtils.toJsonString(createNormalJarJavaParameters(RUN_TYPE_NORMAL_JAR))); taskExecutionContext.setExecutePath("/tmp/dolphinscheduler/test/executepath"); taskExecutionContext.setTaskAppId("runJavaType"); ResourceContext.ResourceItem resourceItem1 = new ResourceContext.ResourceItem(); diff --git a/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java-task-main-jar.ts b/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java-task-main-jar.ts index 826d709fad985..1804c9729e394 100644 --- a/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java-task-main-jar.ts +++ b/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java-task-main-jar.ts @@ -26,7 +26,7 @@ export function useJavaTaskMainJar(model: { [field: string]: any }): IJsonItem { const mainJarOptions = ref([] as IMainJar[]) const taskStore = useTaskNodeStore() - const mainJarSpan = computed(() => (model.runType === 'JAVA' ? 0 : 24)) + const mainJarSpan = computed(() => (model.runType === 'FATJAR' ? 24 : 0)) const getMainJars = async (programType: ProgramType) => { const storeMainJar = taskStore.getMainJar(programType) if (storeMainJar) { diff --git a/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java-task-normal-jar.ts b/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java-task-normal-jar.ts new file mode 100644 index 0000000000000..09967e699d485 --- /dev/null +++ b/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java-task-normal-jar.ts @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { computed, ref, onMounted, watch } from 'vue' +import { useI18n } from 'vue-i18n' +import { queryResourceByProgramType } from '@/service/modules/resources' +import { useTaskNodeStore } from '@/store/project/task-node' +import utils from '@/utils' +import type { IJsonItem, ProgramType, IMainJar } from '../types' + +export function useJavaTaskNormalJar(model: { [field: string]: any }): IJsonItem { + const { t } = useI18n() + const mainJarOptions = ref([] as IMainJar[]) + const taskStore = useTaskNodeStore() + + const mainJarSpan = computed(() => (model.runType === 'NORMALJAR' ? 24 : 0)) + const getMainJars = async (programType: ProgramType) => { + const storeMainJar = taskStore.getMainJar(programType) + if (storeMainJar) { + mainJarOptions.value = storeMainJar + return + } + const res = await queryResourceByProgramType({ + type: 'FILE', + programType + }) + utils.removeUselessChildren(res) + mainJarOptions.value = res || [] + taskStore.updateMainJar(programType, res) + } + + + onMounted(() => { + getMainJars(model.programType) + }) + + watch( + () => model.programType, + (value) => { + getMainJars(value) + } + ) + + return { + type: 'tree-select', + field: 'mainJar', + name: t('project.node.main_package'), + span: mainJarSpan, + props: { + checkable: true, + cascade: true, + showPath: true, + checkStrategy: 'child', + placeholder: t('project.node.main_package_tips'), + keyField: 'fullName', + labelField: 'name' + }, + validate: { + trigger: ['input', 'blur'], + required: true, + validator(validate: any, value: string) { + if (!value) { + return new Error(t('project.node.main_package_tips')) + } + } + }, + options: mainJarOptions + } +} diff --git a/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java.ts b/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java.ts index b8a47c8935d2b..36f2896a249f1 100644 --- a/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java.ts +++ b/dolphinscheduler-ui/src/views/projects/task/components/node/fields/use-java.ts @@ -18,10 +18,10 @@ import { computed } from 'vue' import { useI18n } from 'vue-i18n' import { useCustomParams, useResources, useJavaTaskMainJar } from '.' import type { IJsonItem } from '../types' +import {useJavaTaskNormalJar} from "@/views/projects/task/components/node/fields/use-java-task-normal-jar"; export function useJava(model: { [field: string]: any }): IJsonItem[] { const { t } = useI18n() - const rawScriptSpan = computed(() => (model.runType === 'JAR' ? 0 : 24)) return [ { type: 'select', @@ -57,17 +57,7 @@ export function useJava(model: { [field: string]: any }): IJsonItem[] { } }, useJavaTaskMainJar(model), - { - type: 'editor', - field: 'rawScript', - span: rawScriptSpan, - name: t('project.node.script'), - validate: { - trigger: ['input', 'trigger'], - required: true, - message: t('project.node.script_tips') - } - }, + useJavaTaskNormalJar(model), useResources(), ...useCustomParams({ model, field: 'localParams', isSimple: false }) ] @@ -75,11 +65,12 @@ export function useJava(model: { [field: string]: any }): IJsonItem[] { export const RUN_TYPES = [ { - label: 'JAVA', - value: 'JAVA' + label: 'FATJAR', + value: 'FATJAR' }, { - label: 'JAR', - value: 'JAR' + label: 'NORMALJAR', + value: 'NORMALJAR' } + ]