Adds maven plugin. Switches class library test generation from main

class to maven goal
This commit is contained in:
konsoletyper 2013-12-17 12:03:27 +04:00
parent e5cb7a20d3
commit 6eb145e1d0
26 changed files with 583 additions and 144 deletions

10
pom.xml
View File

@ -12,6 +12,12 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>teavm-core</module>
<module>teavm-classlib</module>
<module>teavm-maven-plugin</module>
</modules>
<build>
<plugins>
<plugin>
@ -25,8 +31,4 @@
</plugin>
</plugins>
</build>
<modules>
<module>teavm-core</module>
<module>teavm-classlib</module>
</modules>
</project>

View File

@ -10,16 +10,76 @@
<artifactId>teavm-classlib</artifactId>
<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>${project.version}</version>
<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>generate-javascript-tests</id>
<goals>
<goal>build-junit</goal>
</goals>
<phase>process-test-classes</phase>
<configuration>
<minifiying>true</minifiying>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only.
It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<versionRange>[0.0.1-SNAPSHOT,)</versionRange>
<goals>
<goal>build-junit</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -1,123 +0,0 @@
package org.teavm.classlibgen;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.teavm.javascript.JavascriptBuilder;
import org.teavm.model.*;
import org.teavm.model.resource.ClasspathClassHolderSource;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class ClasslibTestGenerator {
private static File outputDir;
private static ClasspathClassHolderSource classSource;
private static List<MethodReference> testMethods = new ArrayList<>();
private static Map<String, List<MethodReference>> groupedMethods = new HashMap<>();
private static Map<MethodReference, String> fileNames = new HashMap<>();
private static String[] testClasses = { "java.lang.ObjectTests", "java.lang.SystemTests",
"java.lang.StringBuilderTests", "java.lang.ClassTests", "java.lang.StringTests",
"java.lang.VMTests" };
public static void main(String[] args) throws IOException {
outputDir = new File(args[0]);
outputDir.mkdirs();
new File(outputDir, "tests").mkdirs();
resourceToFile("org/teavm/javascript/runtime.js", "runtime.js");
resourceToFile("org/teavm/classlib/junit-support.js", "junit-support.js");
resourceToFile("org/teavm/classlib/junit.css", "junit.css");
resourceToFile("org/teavm/classlib/junit.html", "junit.html");
classSource = new ClasspathClassHolderSource();
for (int i = 0; i < testClasses.length; ++i) {
testClasses[i] = "org.teavm.classlib." + testClasses[i];
}
for (String testClass : testClasses) {
ClassHolder classHolder = classSource.getClassHolder(testClass);
findTests(classHolder);
}
File allTestsFile = new File(outputDir, "tests/all.js");
try (Writer allTestsWriter = new OutputStreamWriter(new FileOutputStream(allTestsFile), "UTF-8")) {
allTestsWriter.write("doRunTests = function() {\n");
allTestsWriter.write(" new JUnitServer(document.body).runAllTests([");
boolean first = true;
for (String testClass : testClasses) {
if (!first) {
allTestsWriter.append(",");
}
first = false;
allTestsWriter.append("\n { name : \"").append(testClass).append("\", methods : [");
boolean firstMethod = true;
for (MethodReference methodRef : groupedMethods.get(testClass)) {
String scriptName = "tests/" + fileNames.size() + ".js";
fileNames.put(methodRef, scriptName);
if (!firstMethod) {
allTestsWriter.append(",");
}
firstMethod = false;
allTestsWriter.append("\n { name : \"" + methodRef.getName() + "\", script : \"" +
scriptName + "\", expected : [");
MethodHolder methodHolder = classSource.getClassHolder(testClass).getMethod(
methodRef.getDescriptor());
AnnotationHolder annot = methodHolder.getAnnotations().get("org.junit.Test");
AnnotationValue expectedAnnot = annot.getValues().get("expected");
if (expectedAnnot != null) {
String className = ((ValueType.Object)expectedAnnot.getJavaClass()).getClassName();
allTestsWriter.append("\"" + className + "\"");
}
allTestsWriter.append("] }");
}
allTestsWriter.append("] }");
}
allTestsWriter.write("], function() {}); }");
}
for (MethodReference method : testMethods) {
System.out.println("Building test for " + method);
decompileClassesForTest(method, fileNames.get(method));
}
}
private static void decompileClassesForTest(MethodReference methodRef, String targetName) throws IOException {
JavascriptBuilder builder = new JavascriptBuilder();
builder.setMinifying(true);
@SuppressWarnings("resource")
Writer innerWriter = new OutputStreamWriter(new FileOutputStream(new File(outputDir, targetName)), "UTF-8");
MethodReference cons = new MethodReference(methodRef.getClassName(),
new MethodDescriptor("<init>", ValueType.VOID));
builder.entryPoint("initInstance", cons);
builder.entryPoint("runTest", methodRef).withValue(0, cons.getClassName());
builder.exportType("TestClass", cons.getClassName());
builder.build(innerWriter);
innerWriter.append("\n");
innerWriter.append("\nJUnitClient.run();");
innerWriter.close();
}
private static void findTests(ClassHolder cls) {
for (MethodHolder method : cls.getMethods()) {
if (method.getAnnotations().get("org.junit.Test") != null) {
MethodReference ref = new MethodReference(cls.getName(), method.getDescriptor());
testMethods.add(ref);
List<MethodReference> group = groupedMethods.get(cls.getName());
if (group == null) {
group = new ArrayList<>();
groupedMethods.put(cls.getName(), group);
}
group.add(ref);
}
}
}
private static void resourceToFile(String resource, String fileName) throws IOException {
try (InputStream input = ClasslibTestGenerator.class.getClassLoader().getResourceAsStream(resource)) {
try (OutputStream output = new FileOutputStream(new File(outputDir, fileName))) {
IOUtils.copy(input, output);
}
}
}
}

View File

@ -32,6 +32,7 @@ public class DependencyChecker {
private static Object dummyValue = new Object();
static final boolean shouldLog = System.getProperty("org.teavm.logDependencies", "false").equals("true");
private ClassHolderSource classSource;
private ClassLoader classLoader;
private ScheduledThreadPoolExecutor executor;
private ConcurrentMap<MethodReference, Object> abstractMethods = new ConcurrentHashMap<>();
private ConcurrentCachedMapper<MethodReference, MethodGraph> methodCache;
@ -40,12 +41,13 @@ public class DependencyChecker {
private ConcurrentMap<String, Object> initializedClasses = new ConcurrentHashMap<>();
private AtomicReference<RuntimeException> exceptionOccured = new AtomicReference<>();
public DependencyChecker(ClassHolderSource classSource) {
this(classSource, Runtime.getRuntime().availableProcessors());
public DependencyChecker(ClassHolderSource classSource, ClassLoader classLoader) {
this(classSource, classLoader, Runtime.getRuntime().availableProcessors());
}
public DependencyChecker(ClassHolderSource classSource, int numThreads) {
public DependencyChecker(ClassHolderSource classSource, ClassLoader classLoader, int numThreads) {
this.classSource = classSource;
this.classLoader = classLoader;
executor = new ScheduledThreadPoolExecutor(numThreads);
executor.setThreadFactory(new ThreadFactory() {
@Override public Thread newThread(Runnable r) {
@ -255,7 +257,7 @@ public class DependencyChecker {
String depClassName = ((ValueType.Object)depType).getClassName();
Class<?> depClass;
try {
depClass = Class.forName(depClassName);
depClass = Class.forName(depClassName, true, classLoader);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Dependency plugin not found: " + depClassName, e);
}

View File

@ -29,6 +29,7 @@ import org.teavm.model.util.ProgramUtils;
*/
public class Decompiler {
private ClassHolderSource classSource;
private ClassLoader classLoader;
private Graph graph;
private LoopGraph loopGraph;
private GraphIndexer indexer;
@ -40,8 +41,9 @@ public class Decompiler {
private RangeTree.Node currentNode;
private RangeTree.Node parentNode;
public Decompiler(ClassHolderSource classSource) {
public Decompiler(ClassHolderSource classSource, ClassLoader classLoader) {
this.classSource = classSource;
this.classLoader = classLoader;
}
public int getGraphSize() {
@ -125,7 +127,7 @@ public class Decompiler {
String generatorClassName = ((ValueType.Object)annotValue).getClassName();
Generator generator;
try {
Class<?> generatorClass = Class.forName(generatorClassName);
Class<?> generatorClass = Class.forName(generatorClassName, true, classLoader);
generator = (Generator)generatorClass.newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new DecompilationException("Error instantiating generator " + generatorClassName +

View File

@ -18,17 +18,23 @@ import org.teavm.optimization.ClassSetOptimizer;
public class JavascriptBuilder {
private ClassHolderSource classSource;
private DependencyChecker dependencyChecker;
private ClassLoader classLoader;
private boolean minifying = true;
private Map<String, JavascriptEntryPoint> entryPoints = new HashMap<>();
private Map<String, String> exportedClasses = new HashMap<>();
public JavascriptBuilder(ClassHolderSource classSource) {
public JavascriptBuilder(ClassHolderSource classSource, ClassLoader classLoader) {
this.classSource = classSource;
dependencyChecker = new DependencyChecker(classSource);
this.classLoader = classLoader;
dependencyChecker = new DependencyChecker(classSource, classLoader);
}
public JavascriptBuilder(ClassLoader classLoader) {
this(new ClasspathClassHolderSource(classLoader), classLoader);
}
public JavascriptBuilder() {
this(new ClasspathClassHolderSource());
this(JavascriptBuilder.class.getClassLoader());
}
public boolean isMinifying() {
@ -63,7 +69,7 @@ public class JavascriptBuilder {
}
public void build(Appendable writer) throws RenderingException {
Decompiler decompiler = new Decompiler(classSource);
Decompiler decompiler = new Decompiler(classSource, classLoader);
AliasProvider aliasProvider = minifying ? new MinifyingAliasProvider() : new DefaultAliasProvider();
DefaultNamingStrategy naming = new DefaultNamingStrategy(aliasProvider, classSource);
naming.setMinifying(minifying);

View File

@ -11,7 +11,7 @@ public class ClasspathClassHolderSource implements ClassHolderSource {
private MapperClassHolderSource innerClassSource;
public ClasspathClassHolderSource(ClassLoader classLoader) {
ClasspathResourceReader reader = new ClasspathResourceReader();
ClasspathResourceReader reader = new ClasspathResourceReader(classLoader);
ResourceClassHolderMapper rawMapper = new ResourceClassHolderMapper(reader);
ClasspathResourceMapper classPathMapper = new ClasspathResourceMapper(classLoader, rawMapper);
innerClassSource = new MapperClassHolderSource(classPathMapper);

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

1
teavm-maven-plugin/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>teavm-maven-plugin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false

View File

@ -0,0 +1,6 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.source=1.7

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

View File

@ -0,0 +1,63 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>teavm-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.0</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,249 @@
package org.teavm.maven;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
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;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.teavm.javascript.JavascriptBuilder;
import org.teavm.model.*;
import org.teavm.model.resource.ClasspathClassHolderSource;
/**
*
* @author Alexey Andreev
*/
@Mojo(name = "build-junit", requiresDependencyResolution = ResolutionScope.TEST,
requiresDependencyCollection = ResolutionScope.TEST)
public class BuildJavascriptJUnitMojo extends AbstractMojo {
private static Set<String> testScopes = new HashSet<>(Arrays.asList(
Artifact.SCOPE_COMPILE, Artifact.SCOPE_TEST, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_RUNTIME,
Artifact.SCOPE_PROVIDED));
private Map<String, List<MethodReference>> groupedMethods = new HashMap<>();
private Map<MethodReference, String> fileNames = new HashMap<>();
private List<MethodReference> testMethods = new ArrayList<>();
private List<String> testClasses = new ArrayList<>();
@Component
private MavenProject project;
@Parameter(defaultValue = "${project.build.directory}/javascript-junit")
private File outputDir;
@Parameter(defaultValue = "${project.build.outputDirectory}")
private File classFiles;
@Parameter(defaultValue = "${project.build.testOutputDirectory}")
private File testFiles;
@Parameter
private boolean minifiying = true;
public void setProject(MavenProject project) {
this.project = project;
}
public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}
public void setClassFiles(File classFiles) {
this.classFiles = classFiles;
}
public void setTestFiles(File testFiles) {
this.testFiles = testFiles;
}
public void setMinifiying(boolean minifiying) {
this.minifiying = minifiying;
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
ClassLoader classLoader = prepareClassLoader();
getLog().info("Searching for tests in the directory `" + testFiles.getAbsolutePath() + "'");
findTestClasses(classLoader, testFiles, "");
Log log = getLog();
new File(outputDir, "tests").mkdirs();
resourceToFile("org/teavm/javascript/runtime.js", "runtime.js");
resourceToFile("org/teavm/maven/junit-support.js", "junit-support.js");
resourceToFile("org/teavm/maven/junit.css", "junit.css");
resourceToFile("org/teavm/maven/junit.html", "junit.html");
ClassHolderSource classSource = new ClasspathClassHolderSource(classLoader);
for (String testClass : testClasses) {
ClassHolder classHolder = classSource.getClassHolder(testClass);
if (classHolder == null) {
throw new MojoFailureException("Could not find class " + testClass);
}
findTests(classHolder);
}
File allTestsFile = new File(outputDir, "tests/all.js");
try (Writer allTestsWriter = new OutputStreamWriter(new FileOutputStream(allTestsFile), "UTF-8")) {
allTestsWriter.write("doRunTests = function() {\n");
allTestsWriter.write(" new JUnitServer(document.body).runAllTests([");
boolean first = true;
for (String testClass : testClasses) {
if (!first) {
allTestsWriter.append(",");
}
first = false;
allTestsWriter.append("\n { name : \"").append(testClass).append("\", methods : [");
boolean firstMethod = true;
for (MethodReference methodRef : groupedMethods.get(testClass)) {
String scriptName = "tests/" + fileNames.size() + ".js";
fileNames.put(methodRef, scriptName);
if (!firstMethod) {
allTestsWriter.append(",");
}
firstMethod = false;
allTestsWriter.append("\n { name : \"" + methodRef.getName() + "\", script : \"" +
scriptName + "\", expected : [");
MethodHolder methodHolder = classSource.getClassHolder(testClass).getMethod(
methodRef.getDescriptor());
AnnotationHolder annot = methodHolder.getAnnotations().get("org.junit.Test");
AnnotationValue expectedAnnot = annot.getValues().get("expected");
if (expectedAnnot != null) {
String className = ((ValueType.Object)expectedAnnot.getJavaClass()).getClassName();
allTestsWriter.append("\"" + className + "\"");
}
allTestsWriter.append("] }");
}
allTestsWriter.append("] }");
}
allTestsWriter.write("], function() {}); }");
}
for (MethodReference method : testMethods) {
log.info("Building test for " + method);
decompileClassesForTest(classLoader, method, fileNames.get(method));
}
} catch (IOException e) {
throw new MojoFailureException("IO error occured generating JavaScript files", e);
}
}
private ClassLoader prepareClassLoader() throws MojoExecutionException {
try {
Log log = getLog();
log.info("Preparing classpath for JavaScript JUnit generation");
List<URL> urls = new ArrayList<>();
StringBuilder classpath = new StringBuilder();
for (Artifact artifact : project.getArtifacts()) {
if (!testScopes.contains(artifact.getScope())) {
continue;
}
File file = artifact.getFile();
if (classpath.length() > 0) {
classpath.append(':');
}
classpath.append(file.getPath());
urls.add(file.toURI().toURL());
}
if (classpath.length() > 0) {
classpath.append(':');
}
classpath.append(testFiles.getPath());
urls.add(testFiles.toURI().toURL());
classpath.append(':').append(classFiles.getPath());
urls.add(classFiles.toURI().toURL());
log.info("Using the following classpath for JavaScript JUnit generation: " + classpath);
return new URLClassLoader(urls.toArray(new URL[urls.size()]),
BuildJavascriptJUnitMojo.class.getClassLoader());
} catch (MalformedURLException e) {
throw new MojoExecutionException("Error gathering classpath information", e);
}
}
private void decompileClassesForTest(ClassLoader classLoader, MethodReference methodRef, String targetName)
throws IOException {
JavascriptBuilder builder = new JavascriptBuilder(classLoader);
builder.setMinifying(minifiying);
File file = new File(outputDir, targetName);
try (Writer innerWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
MethodReference cons = new MethodReference(methodRef.getClassName(),
new MethodDescriptor("<init>", ValueType.VOID));
builder.entryPoint("initInstance", cons);
builder.entryPoint("runTest", methodRef).withValue(0, cons.getClassName());
builder.exportType("TestClass", cons.getClassName());
builder.build(innerWriter);
innerWriter.append("\n");
innerWriter.append("\nJUnitClient.run();");
innerWriter.close();
}
}
private void findTestClasses(ClassLoader classLoader, File folder, String prefix) {
Class<? extends Annotation> testAnnot;
try {
testAnnot = Class.forName(Test.class.getName(), true, classLoader).asSubclass(Annotation.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Could not load `" + Test.class.getName() + "` annotation");
}
for (File file : folder.listFiles()) {
if (file.isDirectory()) {
String newPrefix = prefix.isEmpty() ? file.getName() : prefix + "." + file.getName();
findTestClasses(classLoader, file, newPrefix);
} else if (file.getName().endsWith(".class")) {
String className = file.getName().substring(0, file.getName().length() - ".class".length());
if (!prefix.isEmpty()) {
className = prefix + "." + className;
}
try {
Class<?> candidate = Class.forName(className, true, classLoader);
boolean hasTests = false;
for (Method method : candidate.getDeclaredMethods()) {
if (method.isAnnotationPresent(testAnnot)) {
hasTests = true;
break;
}
}
if (hasTests) {
testClasses.add(candidate.getName());
getLog().info("Test class detected: " + candidate.getName());
}
} catch (ClassNotFoundException e) {
getLog().info("Could not load class `" + className + "' to search for tests");
}
}
}
}
private void findTests(ClassHolder cls) {
for (MethodHolder method : cls.getMethods()) {
if (method.getAnnotations().get("org.junit.Test") != null) {
MethodReference ref = new MethodReference(cls.getName(), method.getDescriptor());
testMethods.add(ref);
List<MethodReference> group = groupedMethods.get(cls.getName());
if (group == null) {
group = new ArrayList<>();
groupedMethods.put(cls.getName(), group);
}
group.add(ref);
}
}
}
private void resourceToFile(String resource, String fileName) throws IOException {
try (InputStream input = BuildJavascriptJUnitMojo.class.getClassLoader().getResourceAsStream(resource)) {
try (OutputStream output = new FileOutputStream(new File(outputDir, fileName))) {
IOUtils.copy(input, output);
}
}
}
}

View File

@ -0,0 +1,94 @@
package org.teavm.maven;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.teavm.javascript.JavascriptBuilder;
/**
*
* @author Alexey Andreev
*/
@Mojo(name = "build-javascript")
public class BuildJavascriptMojo extends AbstractMojo {
private static Set<String> compileScopes = new HashSet<>(Arrays.asList(
Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_SYSTEM));
@Component
private MavenProject project;
@Parameter(defaultValue = "${project.build.directory}/javascript/classes.js")
private File targetFile;
@Parameter(defaultValue = "${project.build.outputDirectory}")
private File classFiles;
@Parameter
private boolean minifiying = true;
public void setProject(MavenProject project) {
this.project = project;
}
public void setTargetFile(File targetFile) {
this.targetFile = targetFile;
}
public void setClassFiles(File classFiles) {
this.classFiles = classFiles;
}
public void setMinifiying(boolean minifiying) {
this.minifiying = minifiying;
}
@Override
public void execute() throws MojoExecutionException {
Log log = getLog();
try {
ClassLoader classLoader = prepareClassLoader();
log.info("Building JavaScript file");
JavascriptBuilder builder = new JavascriptBuilder(classLoader);
builder.setMinifying(minifiying);
builder.build(targetFile);
log.info("JavaScript file successfully built");
} catch (RuntimeException e) {
throw new MojoExecutionException("Unexpected error occured", e);
}
}
private ClassLoader prepareClassLoader() throws MojoExecutionException {
try {
Log log = getLog();
log.info("Preparing classpath for JavaScript generation");
List<URL> urls = new ArrayList<>();
StringBuilder classpath = new StringBuilder();
for (Artifact artifact : project.getArtifacts()) {
if (!compileScopes.contains(artifact.getScope())) {
continue;
}
File file = artifact.getFile();
if (classpath.length() > 0) {
classpath.append(':');
}
classpath.append(file.getPath());
urls.add(file.toURI().toURL());
}
log.info("Using the following classpath for JavaScript generation: " + classpath);
urls.add(classFiles.toURI().toURL());
return new URLClassLoader(urls.toArray(new URL[urls.size()]));
} catch (MalformedURLException e) {
throw new MojoExecutionException("Error gathering classpath information", e);
}
}
}