Adds automatic copying of runtime.js and creation of main.html

This commit is contained in:
konsoletyper 2013-12-18 17:31:59 +04:00
parent 514becf949
commit 305c54b72d
5 changed files with 68 additions and 17 deletions

View File

@ -37,7 +37,7 @@
</goals> </goals>
<phase>process-test-classes</phase> <phase>process-test-classes</phase>
<configuration> <configuration>
<minifiying>true</minifiying> <minifying>true</minifying>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>

View File

@ -51,7 +51,7 @@ public class BuildJavascriptJUnitMojo extends AbstractMojo {
private File testFiles; private File testFiles;
@Parameter @Parameter
private boolean minifiying = true; private boolean minifying = true;
public void setProject(MavenProject project) { public void setProject(MavenProject project) {
this.project = project; this.project = project;
@ -69,8 +69,8 @@ public class BuildJavascriptJUnitMojo extends AbstractMojo {
this.testFiles = testFiles; this.testFiles = testFiles;
} }
public void setMinifiying(boolean minifiying) { public void setMinifying(boolean minifying) {
this.minifiying = minifiying; this.minifying = minifying;
} }
@Override @Override
@ -177,7 +177,7 @@ public class BuildJavascriptJUnitMojo extends AbstractMojo {
private void decompileClassesForTest(ClassLoader classLoader, MethodReference methodRef, String targetName) private void decompileClassesForTest(ClassLoader classLoader, MethodReference methodRef, String targetName)
throws IOException { throws IOException {
JavascriptBuilder builder = new JavascriptBuilder(classLoader); JavascriptBuilder builder = new JavascriptBuilder(classLoader);
builder.setMinifying(minifiying); builder.setMinifying(minifying);
File file = new File(outputDir, targetName); File file = new File(outputDir, targetName);
try (Writer innerWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) { try (Writer innerWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
MethodReference cons = new MethodReference(methodRef.getClassName(), MethodReference cons = new MethodReference(methodRef.getClassName(),

View File

@ -1,10 +1,11 @@
package org.teavm.maven; package org.teavm.maven;
import java.io.File; import java.io.*;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
@ -32,32 +33,48 @@ public class BuildJavascriptMojo extends AbstractMojo {
@Component @Component
private MavenProject project; private MavenProject project;
@Parameter(defaultValue = "${project.build.directory}/javascript/classes.js") @Parameter(defaultValue = "${project.build.directory}/javascript")
private File targetFile; private File targetDirectory;
@Parameter(defaultValue = "${project.build.outputDirectory}") @Parameter(defaultValue = "${project.build.outputDirectory}")
private File classFiles; private File classFiles;
private String targetFileName = "classes.js";
@Parameter @Parameter
private boolean minifiying = true; private boolean minifying = true;
@Parameter @Parameter
private String mainClass; private String mainClass;
@Parameter
private boolean runtimeSuppressed;
@Parameter
private boolean mainPageIncluded;
public void setProject(MavenProject project) { public void setProject(MavenProject project) {
this.project = project; this.project = project;
} }
public void setTargetFile(File targetFile) { public void setTargetDirectory(File targetDirectory) {
this.targetFile = targetFile; this.targetDirectory = targetDirectory;
} }
public void setClassFiles(File classFiles) { public void setClassFiles(File classFiles) {
this.classFiles = classFiles; this.classFiles = classFiles;
} }
public void setMinifiying(boolean minifiying) { public void setMinifying(boolean minifying) {
this.minifiying = minifiying; this.minifying = minifying;
}
public void setRuntimeSuppressed(boolean runtimeSuppressed) {
this.runtimeSuppressed = runtimeSuppressed;
}
public void setMainPageIncluded(boolean mainPageIncluded) {
this.mainPageIncluded = mainPageIncluded;
} }
@Override @Override
@ -67,16 +84,32 @@ public class BuildJavascriptMojo extends AbstractMojo {
ClassLoader classLoader = prepareClassLoader(); ClassLoader classLoader = prepareClassLoader();
log.info("Building JavaScript file"); log.info("Building JavaScript file");
JavascriptBuilder builder = new JavascriptBuilder(classLoader); JavascriptBuilder builder = new JavascriptBuilder(classLoader);
builder.setMinifying(minifiying); builder.setMinifying(minifying);
MethodDescriptor mainMethodDesc = new MethodDescriptor("main", ValueType.arrayOf( MethodDescriptor mainMethodDesc = new MethodDescriptor("main", ValueType.arrayOf(
ValueType.object("java.lang.String")), ValueType.VOID); ValueType.object("java.lang.String")), ValueType.VOID);
builder.entryPoint("main", new MethodReference(mainClass, mainMethodDesc)) builder.entryPoint("main", new MethodReference(mainClass, mainMethodDesc))
.withValue(1, "java.lang.String"); .withValue(1, "java.lang.String");
targetFile.getParentFile().mkdirs(); targetDirectory.mkdirs();
builder.build(targetFile); builder.build(new File(targetDirectory, targetFileName));
log.info("JavaScript file successfully built"); 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) { } catch (RuntimeException e) {
throw new MojoExecutionException("Unexpected error occured", 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); 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);
}
}
}
} }

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="runtime.js"></script>
<script type="text/javascript" src="${classes.js}"></script>
</head>
<body onload="main()">
</body>
</html>

View File

@ -31,8 +31,9 @@
</goals> </goals>
<phase>process-classes</phase> <phase>process-classes</phase>
<configuration> <configuration>
<minifiying>false</minifiying> <minifying>false</minifying>
<mainClass>org.teavm.samples.HelloWorld</mainClass> <mainClass>org.teavm.samples.HelloWorld</mainClass>
<mainPageIncluded>true</mainPageIncluded>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>