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>
<phase>process-test-classes</phase>
<configuration>
<minifiying>true</minifiying>
<minifying>true</minifying>
</configuration>
</execution>
</executions>

View File

@ -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(),

View File

@ -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);
}
}
}
}

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>
<phase>process-classes</phase>
<configuration>
<minifiying>false</minifiying>
<minifying>false</minifying>
<mainClass>org.teavm.samples.HelloWorld</mainClass>
<mainPageIncluded>true</mainPageIncluded>
</configuration>
</execution>
</executions>