Skip to content

Commit

Permalink
Update the code details of the JavaTask
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiujiarui committed Oct 23, 2024
1 parent 07ac1a8 commit b498932
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.apache.dolphinscheduler.plugin.task.java.JavaConstants.JAVA_HOME_VAR;

import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.plugin.task.api.AbstractTask;
import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor;
Expand Down Expand Up @@ -143,11 +144,11 @@ protected String buildJarCommand() {
.getResourceAbsolutePathInLocal();
StringBuilder builder = new StringBuilder();
builder.append(getJavaCommandPath())
.append("java").append(" ")
.append(buildResourcePath()).append(" ")
.append("-jar").append(" ")
.append(mainJarAbsolutePathInLocal).append(" ")
.append(javaParameters.getMainArgs().trim()).append(" ")
.append("java").append(Constants.SPACE)
.append(buildResourcePath()).append(Constants.SPACE)
.append("-jar").append(Constants.SPACE)
.append(mainJarAbsolutePathInLocal).append(Constants.SPACE)
.append(javaParameters.getMainArgs().trim()).append(Constants.SPACE)
.append(javaParameters.getJvmArgs().trim());
return builder.toString();
}
Expand All @@ -163,18 +164,14 @@ protected String buildNormalJarCommand() {
javaParameters.getMainJar()
.getResourceName())
.getResourceAbsolutePathInLocal();
String mainJarName = null;
try {
mainJarName = MainClassExtractor.getMainClassName(mainJarAbsolutePathInLocal);
} catch (Exception e) {
e.printStackTrace();
}
String mainJarName = MainClassExtractor.getMainClassName(mainJarAbsolutePathInLocal);

StringBuilder builder = new StringBuilder();
builder.append(getJavaCommandPath())
.append("java").append(" ")
.append(buildResourcePath()).append(" ")
.append(mainJarName).append(" ")
.append(javaParameters.getMainArgs().trim()).append(" ")
.append("java").append(Constants.SPACE)
.append(buildResourcePath()).append(Constants.SPACE)
.append(mainJarName).append(Constants.SPACE)
.append(javaParameters.getMainArgs().trim()).append(Constants.SPACE)
.append(javaParameters.getJvmArgs().trim());
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@
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;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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();
private static final Logger logger = LoggerFactory.getLogger(MainClassExtractor.class);

private MainClassExtractor() {
}
public static String getMainClassName(String jarFilePath) {
String mainClassName = null;
try (JarFile jarFile = new JarFile(new File(jarFilePath))) {

Manifest manifest = jarFile.getManifest();
mainClassName = manifest.getMainAttributes().getValue("Main-Class");

} catch (Exception e) {
logger.error("get mainJarName failed:", e);
}
return mainClassName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class JavaTaskTest {
* @return void
**/
@Test
public void buildJarCommand() {
void buildJarCommand() {
JavaTask javaTask = runJarType();
assertThat(javaTask.buildJarCommand())
.isEqualTo(
Expand All @@ -54,25 +54,20 @@ public void buildJarCommand() {
* @return void
*/
@Test
public void buildNormalJarCommand() {
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.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");
}

/**
* add the Normal Jar parameters
*
* @param runType
* @return
* @return JavaParameters
*/
public JavaParameters createNormalJarJavaParameters(String runType) {
private JavaParameters createNormalJarJavaParameters(String runType) {
JavaParameters javaParameters = new JavaParameters();
javaParameters.setRunType(runType);
javaParameters.setModulePath(false);
Expand Down Expand Up @@ -103,9 +98,9 @@ public JavaParameters createNormalJarJavaParameters(String runType) {
* Add the fat jar parameters
*
* @param runType
* @return
* @return JavaParameters
*/
public JavaParameters createJavaParametersObject(String runType) {
private JavaParameters createJavaParametersObject(String runType) {
JavaParameters javaParameters = new JavaParameters();
javaParameters.setRunType(runType);
javaParameters.setModulePath(false);
Expand Down

0 comments on commit b498932

Please sign in to comment.