From 305c54b72d0e22e49494f50117b10d6e43ad73bc Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Wed, 18 Dec 2013 17:31:59 +0400 Subject: [PATCH] Adds automatic copying of runtime.js and creation of main.html --- teavm-classlib/pom.xml | 2 +- .../teavm/maven/BuildJavascriptJUnitMojo.java | 8 +-- .../org/teavm/maven/BuildJavascriptMojo.java | 63 +++++++++++++++---- .../main/resources/org/teavm/maven/main.html | 9 +++ teavm-samples/pom.xml | 3 +- 5 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 teavm-maven-plugin/src/main/resources/org/teavm/maven/main.html diff --git a/teavm-classlib/pom.xml b/teavm-classlib/pom.xml index b52353f47..845a04d64 100644 --- a/teavm-classlib/pom.xml +++ b/teavm-classlib/pom.xml @@ -37,7 +37,7 @@ process-test-classes - true + true diff --git a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptJUnitMojo.java b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptJUnitMojo.java index 0780cbe0e..aa8564a95 100644 --- a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptJUnitMojo.java +++ b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptJUnitMojo.java @@ -51,7 +51,7 @@ public class BuildJavascriptJUnitMojo extends AbstractMojo { private File testFiles; @Parameter - private boolean minifiying = true; + private boolean minifying = true; public void setProject(MavenProject project) { this.project = project; @@ -69,8 +69,8 @@ public class BuildJavascriptJUnitMojo extends AbstractMojo { this.testFiles = testFiles; } - public void setMinifiying(boolean minifiying) { - this.minifiying = minifiying; + public void setMinifying(boolean minifying) { + this.minifying = minifying; } @Override @@ -177,7 +177,7 @@ public class BuildJavascriptJUnitMojo extends AbstractMojo { private void decompileClassesForTest(ClassLoader classLoader, MethodReference methodRef, String targetName) throws IOException { JavascriptBuilder builder = new JavascriptBuilder(classLoader); - builder.setMinifying(minifiying); + builder.setMinifying(minifying); File file = new File(outputDir, targetName); try (Writer innerWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) { MethodReference cons = new MethodReference(methodRef.getClassName(), diff --git a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java index a6e9e979e..0937a63cd 100644 --- a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java +++ b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java @@ -1,10 +1,11 @@ package org.teavm.maven; -import java.io.File; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.*; +import org.apache.commons.io.IOUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -32,32 +33,48 @@ public class BuildJavascriptMojo extends AbstractMojo { @Component private MavenProject project; - @Parameter(defaultValue = "${project.build.directory}/javascript/classes.js") - private File targetFile; + @Parameter(defaultValue = "${project.build.directory}/javascript") + private File targetDirectory; @Parameter(defaultValue = "${project.build.outputDirectory}") private File classFiles; + private String targetFileName = "classes.js"; + @Parameter - private boolean minifiying = true; + private boolean minifying = true; @Parameter private String mainClass; + @Parameter + private boolean runtimeSuppressed; + + @Parameter + private boolean mainPageIncluded; + public void setProject(MavenProject project) { this.project = project; } - public void setTargetFile(File targetFile) { - this.targetFile = targetFile; + public void setTargetDirectory(File targetDirectory) { + this.targetDirectory = targetDirectory; } public void setClassFiles(File classFiles) { this.classFiles = classFiles; } - public void setMinifiying(boolean minifiying) { - this.minifiying = minifiying; + public void setMinifying(boolean minifying) { + this.minifying = minifying; + } + + public void setRuntimeSuppressed(boolean runtimeSuppressed) { + this.runtimeSuppressed = runtimeSuppressed; + } + + public void setMainPageIncluded(boolean mainPageIncluded) { + this.mainPageIncluded = mainPageIncluded; } @Override @@ -67,16 +84,32 @@ public class BuildJavascriptMojo extends AbstractMojo { ClassLoader classLoader = prepareClassLoader(); log.info("Building JavaScript file"); JavascriptBuilder builder = new JavascriptBuilder(classLoader); - builder.setMinifying(minifiying); + builder.setMinifying(minifying); MethodDescriptor mainMethodDesc = new MethodDescriptor("main", ValueType.arrayOf( ValueType.object("java.lang.String")), ValueType.VOID); builder.entryPoint("main", new MethodReference(mainClass, mainMethodDesc)) .withValue(1, "java.lang.String"); - targetFile.getParentFile().mkdirs(); - builder.build(targetFile); + targetDirectory.mkdirs(); + builder.build(new File(targetDirectory, targetFileName)); log.info("JavaScript file successfully built"); + if (!runtimeSuppressed) { + resourceToFile("org/teavm/javascript/runtime.js", "runtime.js"); + } + if (mainPageIncluded) { + String text; + try (Reader reader = new InputStreamReader(classLoader.getResourceAsStream( + "org/teavm/maven/main.html"), "UTF-8")) { + text = IOUtils.toString(reader).replace("${classes.js}", targetFileName); + } + File mainPageFile = new File(targetDirectory, "main.html"); + try (Writer writer = new OutputStreamWriter(new FileOutputStream(mainPageFile), "UTF-8")) { + writer.append(text); + } + } } catch (RuntimeException e) { throw new MojoExecutionException("Unexpected error occured", e); + } catch (IOException e) { + throw new MojoExecutionException("IO error occured", e); } } @@ -108,4 +141,12 @@ public class BuildJavascriptMojo extends AbstractMojo { throw new MojoExecutionException("Error gathering classpath information", e); } } + + private void resourceToFile(String resource, String fileName) throws IOException { + try (InputStream input = BuildJavascriptJUnitMojo.class.getClassLoader().getResourceAsStream(resource)) { + try (OutputStream output = new FileOutputStream(new File(targetDirectory, fileName))) { + IOUtils.copy(input, output); + } + } + } } diff --git a/teavm-maven-plugin/src/main/resources/org/teavm/maven/main.html b/teavm-maven-plugin/src/main/resources/org/teavm/maven/main.html new file mode 100644 index 000000000..7c57785e6 --- /dev/null +++ b/teavm-maven-plugin/src/main/resources/org/teavm/maven/main.html @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/teavm-samples/pom.xml b/teavm-samples/pom.xml index 01e9c22e4..0138fde8b 100644 --- a/teavm-samples/pom.xml +++ b/teavm-samples/pom.xml @@ -31,8 +31,9 @@ process-classes - false + false org.teavm.samples.HelloWorld + true