SbloodyS commented on code in PR #16542:
URL:
https://github.com/apache/dolphinscheduler/pull/16542#discussion_r1825337433
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
+ }
+
+ }
+ private static void getJar() {
+ String classPath = "/tmp/common/";
+ compileJavaFile("common/Fat.java");
+ compileJavaFile("common/Normal1.java");
+ compileJavaFile("common/Normal2.java");
+ createJar("Fat.class", classPath,
+ "common/Fat.class",
+ "common.Fat",
+ "fat.jar");
+ createJar("Normal1.class",
+ classPath,
+ "common/Normal1.class",
+ "common.Normal1",
+ "normal1.jar");
+ createJar("Normal2.class",
+ classPath,
+ "common/Normal2.class",
+ "common.Normal2",
+ "normal2.jar");
+
+ }
+
+ public static void compileJavaFile(String sourceFilePath) {
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ if (compiler == null) {
+ log.error("Cannot find the system Java compiler.", new
IllegalStateException());
+ }
+
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ URL resourceUrl = classLoader.getResource(sourceFilePath);
+ String absolutePath = "";
+
+ try {
+ File resourceFile = new File(resourceUrl.toURI());
+ absolutePath = resourceFile.getAbsolutePath();
+ } catch (Exception e) {
+ log.error(" java file cannot find:", e);
+ }
+
+ String outputDirPath = "/tmp";
Review Comment:
Using `Constants` instead.
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
+ }
+
+ }
+ private static void getJar() {
+ String classPath = "/tmp/common/";
+ compileJavaFile("common/Fat.java");
+ compileJavaFile("common/Normal1.java");
+ compileJavaFile("common/Normal2.java");
+ createJar("Fat.class", classPath,
+ "common/Fat.class",
+ "common.Fat",
+ "fat.jar");
+ createJar("Normal1.class",
+ classPath,
+ "common/Normal1.class",
+ "common.Normal1",
+ "normal1.jar");
+ createJar("Normal2.class",
+ classPath,
+ "common/Normal2.class",
+ "common.Normal2",
+ "normal2.jar");
+
+ }
+
+ public static void compileJavaFile(String sourceFilePath) {
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ if (compiler == null) {
+ log.error("Cannot find the system Java compiler.", new
IllegalStateException());
+ }
+
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ URL resourceUrl = classLoader.getResource(sourceFilePath);
+ String absolutePath = "";
+
+ try {
+ File resourceFile = new File(resourceUrl.toURI());
+ absolutePath = resourceFile.getAbsolutePath();
+ } catch (Exception e) {
+ log.error(" java file cannot find:", e);
Review Comment:
Same here.
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
Review Comment:
Need to throw exception here.
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
+ }
+
+ }
+ private static void getJar() {
+ String classPath = "/tmp/common/";
+ compileJavaFile("common/Fat.java");
+ compileJavaFile("common/Normal1.java");
+ compileJavaFile("common/Normal2.java");
+ createJar("Fat.class", classPath,
+ "common/Fat.class",
+ "common.Fat",
+ "fat.jar");
+ createJar("Normal1.class",
+ classPath,
+ "common/Normal1.class",
+ "common.Normal1",
+ "normal1.jar");
+ createJar("Normal2.class",
+ classPath,
+ "common/Normal2.class",
+ "common.Normal2",
+ "normal2.jar");
+
+ }
+
+ public static void compileJavaFile(String sourceFilePath) {
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ if (compiler == null) {
+ log.error("Cannot find the system Java compiler.", new
IllegalStateException());
+ }
+
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ URL resourceUrl = classLoader.getResource(sourceFilePath);
+ String absolutePath = "";
+
+ try {
+ File resourceFile = new File(resourceUrl.toURI());
+ absolutePath = resourceFile.getAbsolutePath();
+ } catch (Exception e) {
+ log.error(" java file cannot find:", e);
+ }
+
+ String outputDirPath = "/tmp";
+
+ try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(null, null, null)) {
+
+ File sourceFile = new File(absolutePath);
+ if (!sourceFile.exists()) {
+ log.error("java file not exist", new
IllegalArgumentException());
+ }
+
+ Iterable<? extends JavaFileObject> compilationUnits =
+
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile));
+
+ List<String> options = Arrays.asList(
+ "--release", "8",
+ "-d", outputDirPath);
+
+ JavaCompiler.CompilationTask task = compiler.getTask(
+ null,
+ fileManager,
+ null,
+ options,
+ null,
+ compilationUnits);
+
+ boolean success = task.call();
+
+ if (!success) {
+ throw new RuntimeException("Compilation failed.");
Review Comment:
```suggestion
throw new RuntimeException("Jar compilation failed.");
```
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -121,35 +247,47 @@ public static void cleanup() {
@Test
@Order(1)
- void testCreateWorkflow() {
- WorkflowDefinitionTab workflowDefinitionPage =
- new ProjectPage(browser)
- .goTo(project)
- .goToTab(WorkflowDefinitionTab.class);
+ void testCreateFatWorkflow() {
+ FileManagePage file = new NavBarPage(browser)
+ .goToNav(ResourcePage.class)
+ .goToTab(FileManagePage.class)
+ .uploadFile(filePath + "/fat.jar");
+
+ WebDriverWait wait = WebDriverWaitFactory.createWebDriverWait(browser);
- workflowDefinitionPage
- .createWorkflow()
- .<JavaTaskForm>addTask(WorkflowForm.TaskType.JAVA)
- .script(javaContent)
+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='fat.jar']")));
+
+ ProjectPage projectPage = new NavBarPage(browser)
+ .goToNav(ProjectPage.class);
+
+
wait.until(ExpectedConditions.visibilityOfAllElements(projectPage.projectList()));
+
+ WorkflowDefinitionTab workflowDefinitionPage = projectPage
+ .goTo(project)
+ .goToTab(WorkflowDefinitionTab.class);
+
+ WorkflowForm workflow1 = workflowDefinitionPage.createWorkflow();
+ workflow1.<JavaTaskForm>addTask(WorkflowForm.TaskType.JAVA)
+ .selectRunType("FAT_JAR")
+ .selectMainPackage("fat.jar")
+ .selectJavaResource("fat.jar")
.name("test-1")
- .addParam("today", "${system.datetime}")
.selectEnv(environmentName)
.submit()
.submit()
.name(workflow)
- .addGlobalParam("global_param", "hello world")
.submit();
Awaitility.await().untilAsserted(() ->
assertThat(workflowDefinitionPage.workflowList())
.as("Workflow list should contain newly-created workflow")
- .anyMatch(
- it -> it.getText().contains(workflow)));
+ .anyMatch(it -> it.getText().contains(workflow)));
+
workflowDefinitionPage.publish(workflow);
}
@Test
@Order(30)
- void testRunWorkflow() {
+ void testRunFatWorkflow() {
Review Comment:
```suggestion
void testRunFatJarWorkflow() {
```
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
+ }
+
+ }
+ private static void getJar() {
Review Comment:
```suggestion
private static void createAndBuildJars() {
```
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/resources/common/Fat.java:
##########
Review Comment:
Please put these file into
`dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/resources/docker/java-task/Fat.java`
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -121,35 +247,47 @@ public static void cleanup() {
@Test
@Order(1)
- void testCreateWorkflow() {
- WorkflowDefinitionTab workflowDefinitionPage =
- new ProjectPage(browser)
- .goTo(project)
- .goToTab(WorkflowDefinitionTab.class);
+ void testCreateFatWorkflow() {
+ FileManagePage file = new NavBarPage(browser)
+ .goToNav(ResourcePage.class)
+ .goToTab(FileManagePage.class)
+ .uploadFile(filePath + "/fat.jar");
+
+ WebDriverWait wait = WebDriverWaitFactory.createWebDriverWait(browser);
- workflowDefinitionPage
- .createWorkflow()
- .<JavaTaskForm>addTask(WorkflowForm.TaskType.JAVA)
- .script(javaContent)
+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='fat.jar']")));
+
+ ProjectPage projectPage = new NavBarPage(browser)
+ .goToNav(ProjectPage.class);
+
+
wait.until(ExpectedConditions.visibilityOfAllElements(projectPage.projectList()));
+
+ WorkflowDefinitionTab workflowDefinitionPage = projectPage
+ .goTo(project)
+ .goToTab(WorkflowDefinitionTab.class);
+
+ WorkflowForm workflow1 = workflowDefinitionPage.createWorkflow();
+ workflow1.<JavaTaskForm>addTask(WorkflowForm.TaskType.JAVA)
+ .selectRunType("FAT_JAR")
+ .selectMainPackage("fat.jar")
+ .selectJavaResource("fat.jar")
.name("test-1")
- .addParam("today", "${system.datetime}")
.selectEnv(environmentName)
.submit()
.submit()
.name(workflow)
- .addGlobalParam("global_param", "hello world")
.submit();
Awaitility.await().untilAsserted(() ->
assertThat(workflowDefinitionPage.workflowList())
.as("Workflow list should contain newly-created workflow")
- .anyMatch(
- it -> it.getText().contains(workflow)));
+ .anyMatch(it -> it.getText().contains(workflow)));
+
workflowDefinitionPage.publish(workflow);
}
@Test
@Order(30)
- void testRunWorkflow() {
+ void testRunFatWorkflow() {
Review Comment:
Please add normal jar test case too.
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -121,35 +247,47 @@ public static void cleanup() {
@Test
@Order(1)
- void testCreateWorkflow() {
- WorkflowDefinitionTab workflowDefinitionPage =
- new ProjectPage(browser)
- .goTo(project)
- .goToTab(WorkflowDefinitionTab.class);
+ void testCreateFatWorkflow() {
Review Comment:
```suggestion
void testCreateFatJarWorkflow() {
```
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/pages/project/workflow/task/JavaTaskForm.java:
##########
@@ -17,27 +17,124 @@
package org.apache.dolphinscheduler.e2e.pages.project.workflow.task;
-import org.apache.dolphinscheduler.e2e.pages.common.CodeEditor;
+import org.apache.dolphinscheduler.e2e.core.WebDriverWaitFactory;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.WorkflowForm;
+import java.util.List;
+
+import lombok.Getter;
+
+import org.openqa.selenium.By;
+import org.openqa.selenium.JavascriptExecutor;
+import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.FindBy;
+import org.openqa.selenium.support.FindBys;
+import org.openqa.selenium.support.PageFactory;
+import org.openqa.selenium.support.ui.ExpectedConditions;
+import org.openqa.selenium.support.ui.WebDriverWait;
+@Getter
public class JavaTaskForm extends TaskNodeForm {
- private CodeEditor codeEditor;
-
private WebDriver driver;
+ @FindBys({
+ @FindBy(className = "resource-select"),
+ @FindBy(className = "n-base-selection"),
+ })
+ private WebElement selectResource;
+
+ @FindBys({
+
+ @FindBy(className = "n-tree-select"),
+ @FindBy(className = "n-base-selection"),
+ })
+ private WebElement selectMainPackage;
+
+ @FindBys({
+ @FindBy(xpath = "//div[contains(@class,'n-form-item') and
.//span[text()='Run Type']]"),
+ @FindBy(className = "n-select"),
+ @FindBy(className = "n-base-selection")
+
+ })
+ private WebElement selectRunType;
+
public JavaTaskForm(WorkflowForm parent) {
super(parent);
- this.codeEditor = new CodeEditor(parent.driver());
-
this.driver = parent.driver();
+
+ PageFactory.initElements(driver, this);
}
- public JavaTaskForm script(String script) {
- codeEditor.content(script);
+ public JavaTaskForm selectJavaResource(String resourceName) {
+ WebDriverWait wait =
WebDriverWaitFactory.createWebDriverWait(driver());
+ wait.until(ExpectedConditions.elementToBeClickable(selectResource));
+ ((JavascriptExecutor)
driver).executeScript("arguments[0].scrollIntoView(true);
arguments[0].click();",
+ selectResource);
+ By optionsLocator = By.className("n-tree-node-content__text");
+
wait.until(ExpectedConditions.visibilityOfElementLocated(optionsLocator));
+
+ List<WebElement> options = driver.findElements(optionsLocator);
+ boolean found = false;
+ for (WebElement option : options) {
+ if (option.getText().trim().startsWith(resourceName)) {
+ ((JavascriptExecutor)
driver).executeScript("arguments[0].scrollIntoView(true);
arguments[0].click();",
+ option);
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ throw new RuntimeException("Cannot Found: " + resourceName);
Review Comment:
```suggestion
throw new RuntimeException("File " + resourceName + " can not be
found.");
```
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -163,17 +301,21 @@ void testRunWorkflow() {
.run(workflow)
.submit();
- Awaitility.await().untilAsserted(() -> {
- browser.navigate().refresh();
+ Awaitility.await()
+ .atMost(Duration.ofMinutes(5))
Review Comment:
```suggestion
```
The default timeout is 2 minutes is enough.
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
Review Comment:
Using `Constants.HOST_TMP_PATH` instead.
##########
dolphinscheduler-e2e/dolphinscheduler-e2e-case/src/test/java/org/apache/dolphinscheduler/e2e/cases/WorkflowJavaTaskE2ETest.java:
##########
@@ -70,16 +97,110 @@ public class WorkflowJavaTaskE2ETest {
private static final String environmentWorkerGroup = "default";
- private static final String javaContent = "public class Test {" +
- " public static void main(String[] args) {" +
- " System.out.println(\"hello world\");" +
- " }" +
- "}";
+ private static final String filePath = "/tmp";
private static RemoteWebDriver browser;
+ private static void createJar(String className, String classFilePath,
String entryName, String mainPackage,
+ String jarName) {
+
+ String jarFilePath = "/tmp/" + jarName;
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
mainPackage);
+
+ try (
+ FileOutputStream fos = new FileOutputStream(jarFilePath);
+ JarOutputStream jos = new JarOutputStream(fos, manifest)) {
+ Path path = new File(classFilePath + className).toPath();
+ JarEntry entry = new JarEntry(entryName);
+ jos.putNextEntry(entry);
+ byte[] bytes = Files.readAllBytes(path);
+ jos.write(bytes, 0, bytes.length);
+ jos.closeEntry();
+ } catch (IOException e) {
+ log.error("create jar failed:", e);
+ }
+
+ }
+ private static void getJar() {
+ String classPath = "/tmp/common/";
+ compileJavaFile("common/Fat.java");
+ compileJavaFile("common/Normal1.java");
+ compileJavaFile("common/Normal2.java");
+ createJar("Fat.class", classPath,
+ "common/Fat.class",
+ "common.Fat",
+ "fat.jar");
+ createJar("Normal1.class",
+ classPath,
+ "common/Normal1.class",
+ "common.Normal1",
+ "normal1.jar");
+ createJar("Normal2.class",
+ classPath,
+ "common/Normal2.class",
+ "common.Normal2",
+ "normal2.jar");
+
+ }
+
+ public static void compileJavaFile(String sourceFilePath) {
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ if (compiler == null) {
+ log.error("Cannot find the system Java compiler.", new
IllegalStateException());
+ }
+
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ URL resourceUrl = classLoader.getResource(sourceFilePath);
+ String absolutePath = "";
+
+ try {
+ File resourceFile = new File(resourceUrl.toURI());
+ absolutePath = resourceFile.getAbsolutePath();
+ } catch (Exception e) {
+ log.error(" java file cannot find:", e);
+ }
+
+ String outputDirPath = "/tmp";
+
+ try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(null, null, null)) {
+
+ File sourceFile = new File(absolutePath);
+ if (!sourceFile.exists()) {
+ log.error("java file not exist", new
IllegalArgumentException());
+ }
+
+ Iterable<? extends JavaFileObject> compilationUnits =
+
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile));
+
+ List<String> options = Arrays.asList(
+ "--release", "8",
+ "-d", outputDirPath);
+
+ JavaCompiler.CompilationTask task = compiler.getTask(
+ null,
+ fileManager,
+ null,
+ options,
+ null,
+ compilationUnits);
+
+ boolean success = task.call();
+
+ if (!success) {
+ throw new RuntimeException("Compilation failed.");
+ }
+ } catch (IOException e) {
+ log.error("compile java file failed:", e);
Review Comment:
Same here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]