Refactoring kotlin-maven-plugin:

* cleanup KotlinCompileMojoBase moved JVM specific things to KotlinCompileMojo;
* add support noInline in JS.
This commit is contained in:
Zalim Bashorov
2014-08-18 19:53:36 +04:00
parent d986b0e565
commit 23fa0ba6ab
10 changed files with 395 additions and 339 deletions
@@ -17,16 +17,16 @@
package org.jetbrains.kotlin.maven.doc;
import org.apache.maven.plugin.MojoExecutionException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.CLICompiler;
import org.jetbrains.jet.cli.common.ExitCode;
import org.jetbrains.jet.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import org.jetbrains.kotlin.doc.KDocArguments;
import org.jetbrains.kotlin.doc.KDocCompiler;
import org.jetbrains.kotlin.doc.KDocConfig;
import org.jetbrains.kotlin.maven.KotlinCompileMojoBase;
import org.jetbrains.kotlin.maven.K2JVMCompileMojo;
import java.util.List;
import java.util.Map;
@@ -39,45 +39,7 @@ import java.util.Map;
* @requiresDependencyResolution test
* @noinspection UnusedDeclaration
*/
public class KDocMojo extends KotlinCompileMojoBase {
/**
* Project classpath.
*
* @parameter default-value="${project.compileClasspathElements}"
* @required
* @readonly
*/
public List<String> classpath;
/**
* Project test classpath.
*
* @parameter default-value="${project.testClasspathElements}"
* @required
* @readonly
*/
protected List<String> testClasspath;
/**
* The directory for compiled apidoc classes.
*
* @parameter default-value="${project.build.outputDirectory}"
* @required
* @readonly
*/
public String output;
// TODO not sure why this doesn't work :(
// * @parameter default-value="$(project.basedir}/src/main/resources"
/**
* The directories used to scan for annotation.xml files for Kotlin annotations
*
* @parameter
*/
public List<String> annotationPaths;
public class KDocMojo extends K2JVMCompileMojo {
// TODO not sure why default is stopping us passing this value in via a config
// default-value="${project.compileSourceRoots}"
@@ -176,6 +138,11 @@ public class KDocMojo extends KotlinCompileMojoBase {
*/
private Map<String, String> packageSummaryText;
@Override
public List<String> getSources() {
return sources;
}
@Override
protected K2JVMCompiler createCompiler() {
return new KDocCompiler();
@@ -187,10 +154,11 @@ public class KDocMojo extends KotlinCompileMojoBase {
}
@Override
@NotNull
protected ExitCode executeCompiler(
CLICompiler compiler,
CommonCompilerArguments arguments,
MessageCollector messageCollector
@NotNull CLICompiler<K2JVMCompilerArguments> compiler,
@NotNull K2JVMCompilerArguments arguments,
@NotNull MessageCollector messageCollector
) {
ExitCode exitCode = super.executeCompiler(compiler, arguments, messageCollector);
if (exitCode == ExitCode.COMPILATION_ERROR) {
@@ -202,11 +170,7 @@ public class KDocMojo extends KotlinCompileMojoBase {
}
@Override
protected void configureCompilerArguments(CommonCompilerArguments arguments) throws MojoExecutionException {
if (arguments instanceof K2JVMCompilerArguments) {
configureBaseCompilerArguments(getLog(), (K2JVMCompilerArguments) arguments, docModule, sources, classpath, output);
}
protected void configureSpecificCompilerArguments(K2JVMCompilerArguments arguments) throws MojoExecutionException {
if (arguments instanceof KDocArguments) {
KDocArguments kdoc = (KDocArguments) arguments;
KDocConfig docConfig = kdoc.getDocConfig();
@@ -215,8 +179,6 @@ public class KDocMojo extends KotlinCompileMojoBase {
kdoc.noJdkAnnotations = true;
kdoc.annotations = getFullAnnotationsPath(getLog(), annotationPaths);
if (ignorePackages != null) {
docConfig.getIgnorePackages().addAll(ignorePackages);
}
@@ -235,19 +197,20 @@ public class KDocMojo extends KotlinCompileMojoBase {
docConfig.setWarnNoComments(warnNoComments);
docConfig.setSourceRootHref(sourceRootHref);
docConfig.setProjectRootDir(projectRootDir);
getLog().info("API docs output to: " + docConfig.getDocOutputDir());
getLog().info("classpath: " + classpath);
getLog().info("title: " + title);
getLog().info("sources: " + sources);
getLog().info("sourceRootHref: " + sourceRootHref);
getLog().info("projectRootDir: " + projectRootDir);
getLog().info("kotlin annotations: " + kdoc.annotations);
getLog().info("packageDescriptionFiles: " + packageDescriptionFiles);
getLog().info("packagePrefixToUrls: " + packagePrefixToUrls);
getLog().info("API docs ignore packages: " + ignorePackages);
LOG.info("API docs output to: " + docConfig.getDocOutputDir());
LOG.info("classpath: " + classpath);
LOG.info("title: " + title);
LOG.info("sources: " + sources);
LOG.info("sourceRootHref: " + sourceRootHref);
LOG.info("projectRootDir: " + projectRootDir);
LOG.info("kotlin annotations: " + kdoc.annotations);
LOG.info("packageDescriptionFiles: " + packageDescriptionFiles);
LOG.info("packagePrefixToUrls: " + packagePrefixToUrls);
LOG.info("API docs ignore packages: " + ignorePackages);
}
else {
getLog().warn("No KDocArguments available!");
LOG.warn("No KDocArguments available!");
}
}
@@ -23,6 +23,11 @@
<artifactId>kotlin-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-js-library</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>test-js-extraArguments</artifactId>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-js-library</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>js</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-Xno-inline</arg>
</args>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,9 @@
package org.jetbrains
fun main(args : Array<String>) {
println(getGreeting())
}
fun getGreeting() : String {
return "Hello, World!"
}
@@ -0,0 +1,6 @@
import java.io.*;
File file = new File(basedir, "target/js/test-js-extraArguments.js");
if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException("Could not find generated JS : " + file);
}
@@ -21,8 +21,7 @@ import com.google.common.io.InputSupplier;
import com.intellij.openapi.util.io.FileUtil;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.jetbrains.jet.cli.common.CLICompiler;
import org.jetbrains.jet.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.arguments.K2JSCompilerArguments;
import org.jetbrains.jet.cli.js.K2JSCompiler;
import org.jetbrains.k2js.config.MetaInfServices;
@@ -31,7 +30,6 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
/**
* Converts Kotlin to JavaScript code
@@ -40,11 +38,10 @@ import java.util.List;
* @phase compile
* @noinspection UnusedDeclaration
*/
public class K2JSCompilerMojo extends KotlinCompileMojo {
public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArguments> {
public static final String KOTLIN_JS_MAPS = "kotlin-maps.js";
public static final String KOTLIN_JS_LONG = "kotlin-long.js";
public static final String KOTLIN_JS_LIB = "kotlin-lib.js";
public static final String KOTLIN_JS_LIB_ECMA3 = "kotlin-lib-ecma3.js";
public static final String KOTLIN_JS_LIB_ECMA5 = "kotlin-lib-ecma5.js";
/**
@@ -83,13 +80,14 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
super.execute();
if (appendLibraryJS != null && appendLibraryJS) {
try {
Charset charset = Charset.defaultCharset();
File file = new File(outputFile);
String text = Files.toString(file, charset);
StringBuilder builder = new StringBuilder();
appendFile(KOTLIN_JS_LIB_ECMA3, builder);
appendFile(KOTLIN_JS_LIB_ECMA5, builder);
appendFile(KOTLIN_JS_LIB, builder);
appendFile(KOTLIN_JS_MAPS, builder);
appendFile(KOTLIN_JS_LONG, builder);
@@ -101,22 +99,21 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
}
}
if (copyLibraryJS != null && copyLibraryJS) {
getLog().info("Copying kotlin JS library to " + outputKotlinJSDir);
LOG.info("Copying kotlin JS library to " + outputKotlinJSDir);
copyJsLibraryFile(KOTLIN_JS_MAPS);
copyJsLibraryFile(KOTLIN_JS_LONG);
copyJsLibraryFile(KOTLIN_JS_LIB);
copyJsLibraryFile(KOTLIN_JS_LIB_ECMA3);
copyJsLibraryFile(KOTLIN_JS_LIB_ECMA5);
}
}
private static void appendFile(String jsLib, StringBuilder builder) throws MojoExecutionException {
private void appendFile(String jsLib, StringBuilder builder) throws MojoExecutionException {
// lets copy the kotlin library into the output directory
try {
final InputStream inputStream = MetaInfServices.loadClasspathResource(jsLib);
if (inputStream == null) {
System.out.println("WARNING: Could not find " + jsLib + " on the classpath!");
LOG.warn("Could not find " + jsLib + " on the classpath!");
} else {
InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() {
@Override
@@ -132,14 +129,16 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
}
}
protected void copyJsLibraryFile(String jsLib) throws MojoExecutionException {
// lets copy the kotlin library into the output directory
private void copyJsLibraryFile(String jsLib) throws MojoExecutionException {
try {
outputKotlinJSDir.mkdirs();
final InputStream inputStream = MetaInfServices.loadClasspathResource(jsLib);
if (inputStream == null) {
System.out.println("WARNING: Could not find " + jsLib + " on the classpath!");
LOG.warn("Could not find " + jsLib + " on the classpath!");
} else {
if (!outputKotlinJSDir.exists() && !outputKotlinJSDir.mkdirs()) {
throw new MojoExecutionException("Could not create output directory '" + outputKotlinJSDir + "'.");
}
InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
@@ -154,28 +153,19 @@ public class K2JSCompilerMojo extends KotlinCompileMojo {
}
@Override
protected void configureCompilerArguments(CommonCompilerArguments arguments) throws MojoExecutionException {
super.configureCompilerArguments(arguments);
if (arguments instanceof K2JSCompilerArguments) {
K2JSCompilerArguments k2jsArgs = (K2JSCompilerArguments)arguments;
k2jsArgs.outputFile = outputFile;
if (getLog().isDebugEnabled()) {
k2jsArgs.verbose = true;
}
List<String> sources = getSources();
k2jsArgs.freeArgs.addAll(sources);
getLog().info("Compiling Kotlin src from " + sources + " to JavaScript at: " + outputFile);
}
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException {
arguments.outputFile = outputFile;
}
@NotNull
@Override
protected CommonCompilerArguments createCompilerArguments() {
protected K2JSCompilerArguments createCompilerArguments() {
return new K2JSCompilerArguments();
}
@NotNull
@Override
protected CLICompiler createCompiler() {
protected K2JSCompiler createCompiler() {
return new K2JSCompiler();
}
}
@@ -0,0 +1,243 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.maven;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.sampullara.cli.Args;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import static com.intellij.openapi.util.text.StringUtil.join;
/**
* Compiles kotlin sources
*
* @goal compile
* @phase compile
* @requiresDependencyResolution compile
* @noinspection UnusedDeclaration
*/
public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArguments> {
/**
* The directories used to scan for annotation.xml files for Kotlin annotations
*
* @parameter
*/
public List<String> annotationPaths;
/**
* @parameter default-value="true"
*/
public boolean scanForAnnotations;
/**
* Project classpath.
*
* @parameter default-value="${project.compileClasspathElements}"
* @required
* @readonly
*/
public List<String> classpath;
/**
* Project test classpath.
*
* @parameter default-value="${project.testClasspathElements}"
* @required
* @readonly
*/
protected List<String> testClasspath;
@NotNull
@Override
protected K2JVMCompiler createCompiler() {
return new K2JVMCompiler();
}
@NotNull
@Override
protected K2JVMCompilerArguments createCompilerArguments() {
return new K2JVMCompilerArguments();
}
@Override
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments) throws MojoExecutionException {
LOG.info("Classes directory is " + output);
arguments.destination = output;
// don't include runtime, it should be in maven dependencies
arguments.noStdlib = true;
ArrayList<String> classpathList = new ArrayList<String>();
if (module != null) {
LOG.info("Compiling Kotlin module " + module);
arguments.module = module;
}
else {
// TODO: Move it compiler
classpathList.addAll(getSources());
}
classpathList.addAll(classpath);
if (classpathList.remove(output)) {
LOG.debug("Removed target directory from compiler classpath (" + output + ")");
}
if (classpathList.size() > 0) {
String classPathString = join(classpathList, File.pathSeparator);
LOG.info("Classpath: " + classPathString);
arguments.classpath = classPathString;
}
LOG.info("Classes directory is " + output);
arguments.destination = output;
arguments.noJdkAnnotations = true;
arguments.annotations = getFullAnnotationsPath(LOG, annotationPaths);
LOG.info("Using kotlin annotations from " + arguments.annotations);
try {
Args.parse(arguments, ArrayUtil.toStringArray(args));
}
catch (IllegalArgumentException e) {
throw new MojoExecutionException(e.getMessage());
}
if (arguments.noOptimize) {
LOG.info("Optimization is turned off");
}
}
protected String getFullAnnotationsPath(Log log, List<String> annotations) {
String jdkAnnotation = getJdkAnnotations().getPath();
List<String> list = new ArrayList<String>();
list.add(jdkAnnotation);
if (annotations != null) {
for (String annotationPath : annotations) {
if (new File(annotationPath).exists()) {
list.add(annotationPath);
} else {
log.info("annotation path " + annotationPath + " does not exist");
}
}
}
if (scanForAnnotations) {
for (String path : scanAnnotations(log)) {
if (!list.contains(path)) {
list.add(path);
}
}
}
return join(list, File.pathSeparator);
}
@NotNull
private static File getJdkAnnotations() {
ClassLoader classLoader = KotlinCompileMojoBase.class.getClassLoader();
if (!(classLoader instanceof URLClassLoader)) {
throw new RuntimeException("Kotlin plugin`s class loader is not URLClassLoader");
}
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
String path = url.getPath();
if (StringUtil.isEmpty(path)) {
continue;
}
File file = new File(path);
if (file.getName().startsWith("kotlin-jdk-annotations")) {
return file;
}
}
throw new RuntimeException("Could not get jdk annotations from Kotlin plugin`s classpath");
}
private List<String> scanAnnotations(Log log) {
List<String> annotations = new ArrayList<String>();
Set<Artifact> artifacts = project.getArtifacts();
for (Artifact artifact : artifacts) {
File file = artifact.getFile();
if (containsAnnotations(file, log)) {
log.info("Discovered kotlin annotations in: " + file);
try {
annotations.add(file.getCanonicalPath());
}
catch (IOException e) {
log.warn("Error extracting canonical path from: " + file, e);
}
}
}
return annotations;
}
private static boolean containsAnnotations(File file, Log log) {
log.debug("Scanning for kotlin annotations in " + file);
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if (name.endsWith("/annotations.xml")) {
return true;
}
}
}
catch (IOException e) {
log.warn("Error reading contents of jar: " + file, e);
}
finally {
if (zipFile != null) {
try {
zipFile.close();
}
catch (IOException e) {
log.warn("Error closing: " + zipFile, e);
}
}
}
return false;
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.maven;
import org.apache.maven.plugin.MojoExecutionException;
import org.jetbrains.jet.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
/**
* Compiles kotlin sources
*
* @goal compile
* @phase compile
* @requiresDependencyResolution compile
* @noinspection UnusedDeclaration
*/
public class KotlinCompileMojo extends KotlinCompileMojoBase {
@Override
protected void configureCompilerArguments(CommonCompilerArguments arguments) throws MojoExecutionException {
if (arguments instanceof K2JVMCompilerArguments) {
configureBaseCompilerArguments(getLog(), (K2JVMCompilerArguments) arguments, module, getSources(), classpath, output);
}
}
}
@@ -16,10 +16,8 @@
package org.jetbrains.kotlin.maven;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.sampullara.cli.Args;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -30,25 +28,17 @@ import org.jetbrains.jet.cli.common.CLICompiler;
import org.jetbrains.jet.cli.common.ExitCode;
import org.jetbrains.jet.cli.common.KotlinVersion;
import org.jetbrains.jet.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.common.messages.CompilerMessageLocation;
import org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import org.jetbrains.jet.config.Services;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Arrays;
import java.util.List;
import static com.intellij.openapi.util.text.StringUtil.join;
public abstract class KotlinCompileMojoBase extends AbstractMojo {
public abstract class KotlinCompileMojoBase<A extends CommonCompilerArguments> extends AbstractMojo {
// TODO it would be nice to avoid using 2 injected fields for sources
// but I've not figured out how to have a defaulted parameter value
// which is also customisable inside an <execution> in a maven pom.xml
@@ -74,13 +64,6 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
return defaultSourceDirs;
}
/**
* The directories used to scan for annotation.xml files for Kotlin annotations
*
* @parameter
*/
public List<String> annotationPaths;
// TODO not sure why this doesn't work :(
// * @parameter default-value="$(project.basedir}/src/main/resources"
@@ -91,29 +74,6 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
*/
public MavenProject project;
/**
* @parameter default-value="true"
*/
public boolean scanForAnnotations;
/**
* Project classpath.
*
* @parameter default-value="${project.compileClasspathElements}"
* @required
* @readonly
*/
public List<String> classpath;
/**
* Project test classpath.
*
* @parameter default-value="${project.testClasspathElements}"
* @required
* @readonly
*/
protected List<String> testClasspath;
/**
* The directory for compiled classes.
*
@@ -153,9 +113,12 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
*/
public List<String> args;
protected final Log LOG = getLog();
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Kotlin Compiler version " + KotlinVersion.VERSION);
LOG.info("Kotlin Compiler version " + KotlinVersion.VERSION);
// Check sources
List<String> sources = getSources();
@@ -170,18 +133,17 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
}
if (!sourcesExists) {
getLog().warn( "No sources found skipping Kotlin compile" );
LOG.warn("No sources found skipping Kotlin compile");
return;
}
}
CommonCompilerArguments arguments = createCompilerArguments();
A arguments = createCompilerArguments();
configureCompilerArguments(arguments);
CLICompiler compiler = createCompiler();
CLICompiler<A> compiler = createCompiler();
printCompilerArgumentsIfDebugEnabled(arguments, compiler);
final Log log = getLog();
MessageCollector messageCollector = new MessageCollector() {
@Override
public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
@@ -191,13 +153,13 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
String text = position + message;
if (CompilerMessageSeverity.VERBOSE.contains(severity)) {
log.debug(text);
LOG.debug(text);
} else if (CompilerMessageSeverity.ERRORS.contains(severity)) {
log.error(text);
LOG.error(text);
} else if (severity == CompilerMessageSeverity.INFO) {
log.info(text);
LOG.info(text);
} else {
log.warn(text);
LOG.warn(text);
}
}
};
@@ -213,7 +175,7 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
}
}
private void printCompilerArgumentsIfDebugEnabled(CommonCompilerArguments arguments, CLICompiler compiler) {
private void printCompilerArgumentsIfDebugEnabled(@NotNull A arguments, @NotNull CLICompiler<A> compiler) {
if (getLog().isDebugEnabled()) {
getLog().debug("Invoking compiler " + compiler + " with arguments:");
try {
@@ -242,22 +204,20 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
}
}
protected CLICompiler createCompiler() {
return new K2JVMCompiler();
}
@NotNull
protected abstract CLICompiler<A> createCompiler();
/**
* Derived classes can create custom compiler argument implementations
* such as for KDoc
*/
protected CommonCompilerArguments createCompilerArguments() {
return new K2JVMCompilerArguments();
}
@NotNull
protected abstract A createCompilerArguments();
@NotNull
protected ExitCode executeCompiler(
@NotNull CLICompiler compiler,
@NotNull CommonCompilerArguments arguments,
@NotNull CLICompiler<A> compiler,
@NotNull A arguments,
@NotNull MessageCollector messageCollector
) {
return compiler.exec(messageCollector, Services.EMPTY, arguments);
@@ -266,48 +226,22 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
/**
* Derived classes can register custom plugins or configurations
*/
protected abstract void configureCompilerArguments(CommonCompilerArguments arguments) throws MojoExecutionException;
protected abstract void configureSpecificCompilerArguments(@NotNull A arguments) throws MojoExecutionException;
protected void configureBaseCompilerArguments(Log log, K2JVMCompilerArguments arguments, String module,
List<String> sources, List<String> classpath, String output) throws MojoExecutionException {
// don't include runtime, it should be in maven dependencies
arguments.noStdlib = true;
ArrayList<String> classpathList = new ArrayList<String>();
if (module != null) {
log.info("Compiling Kotlin module " + module);
arguments.module = module;
}
else {
if (sources.isEmpty())
throw new MojoExecutionException("No source roots to compile");
arguments.freeArgs.addAll(sources);
log.info("Compiling Kotlin sources from " + sources);
// TODO: Move it compiler
classpathList.addAll(sources);
private void configureCompilerArguments(@NotNull A arguments) throws MojoExecutionException {
if (LOG.isDebugEnabled()) {
arguments.verbose = true;
}
classpathList.addAll(classpath);
if (classpathList.remove(output)) {
log.debug("Removed target directory from compiler classpath (" + output + ")");
List<String> sources = getSources();
if (sources == null || sources.isEmpty()) {
throw new MojoExecutionException("No source roots to compile");
}
if (classpathList.size() > 0) {
String classPathString = join(classpathList, File.pathSeparator);
log.info("Classpath: " + classPathString);
arguments.classpath = classPathString;
}
arguments.freeArgs.addAll(sources);
LOG.info("Compiling Kotlin sources from " + sources );
log.info("Classes directory is " + output);
arguments.destination = output;
arguments.noJdkAnnotations = true;
arguments.annotations = getFullAnnotationsPath(log, annotationPaths);
log.info("Using kotlin annotations from " + arguments.annotations);
configureSpecificCompilerArguments(arguments);
try {
Args.parse(arguments, ArrayUtil.toStringArray(args));
@@ -317,110 +251,7 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
}
if (arguments.noInline) {
log.info("Method inlining is turned off");
LOG.info("Method inlining is turned off");
}
if (arguments.noOptimize) {
log.info("Optimization is turned off");
}
}
protected String getFullAnnotationsPath(Log log, List<String> annotations) {
String jdkAnnotation = getJdkAnnotations().getPath();
List<String> list = new ArrayList<String>();
list.add(jdkAnnotation);
if (annotations != null) {
for (String annotationPath : annotations) {
if (new File(annotationPath).exists()) {
list.add(annotationPath);
} else {
log.info("annotation path " + annotationPath + " does not exist");
}
}
}
if (scanForAnnotations) {
for (String path : scanAnnotations(log)) {
if (!list.contains(path)) {
list.add(path);
}
}
}
return join(list, File.pathSeparator);
}
@NotNull
private static File getJdkAnnotations() {
ClassLoader classLoader = KotlinCompileMojoBase.class.getClassLoader();
if (!(classLoader instanceof URLClassLoader)) {
throw new RuntimeException("Kotlin plugin`s class loader is not URLClassLoader");
}
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
String path = url.getPath();
if (StringUtil.isEmpty(path)) {
continue;
}
File file = new File(path);
if (file.getName().startsWith("kotlin-jdk-annotations")) {
return file;
}
}
throw new RuntimeException("Could not get jdk annotations from Kotlin plugin`s classpath");
}
private List<String> scanAnnotations(Log log) {
List<String> annotations = new ArrayList<String>();
Set<Artifact> artifacts = project.getArtifacts();
for (Artifact artifact : artifacts) {
File file = artifact.getFile();
if (containsAnnotations(file, log)) {
log.info("Discovered kotlin annotations in: " + file);
try {
annotations.add(file.getCanonicalPath());
}
catch (IOException e) {
log.warn("Error extracting canonical path from: " + file, e);
}
}
}
return annotations;
}
private static boolean containsAnnotations(File file, Log log) {
log.debug("Scanning for kotlin annotations in " + file);
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
String name = entries.nextElement().getName();
if (name.endsWith("/annotations.xml")) {
return true;
}
}
}
catch (IOException e) {
log.warn("Error reading contents of jar: " + file, e);
}
finally {
if (zipFile != null) {
try {
zipFile.close();
}
catch (IOException e) {
log.warn("Error closing: " + zipFile, e);
}
}
}
return false;
}
}
@@ -18,8 +18,9 @@ package org.jetbrains.kotlin.maven;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.jetbrains.jet.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import java.util.List;
@@ -31,7 +32,7 @@ import java.util.List;
* @requiresDependencyResolution test
* @noinspection UnusedDeclaration
*/
public class KotlinTestCompileMojo extends KotlinCompileMojoBase {
public class KotlinTestCompileMojo extends K2JVMCompileMojo {
/**
* Flag to allow test compilation to be skipped.
*
@@ -86,11 +87,11 @@ public class KotlinTestCompileMojo extends KotlinCompileMojoBase {
}
@Override
protected void configureCompilerArguments(CommonCompilerArguments arguments) throws MojoExecutionException {
if (arguments instanceof K2JVMCompilerArguments) {
configureBaseCompilerArguments(
getLog(), (K2JVMCompilerArguments) arguments,
testModule, getSources(), testClasspath, testOutput);
}
protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArguments arguments) throws MojoExecutionException {
module = testModule;
classpath = testClasspath;
output = testOutput;
super.configureSpecificCompilerArguments(arguments);
}
}