From 23fa0ba6ab4789feb180e46ba0757102636148e8 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Mon, 18 Aug 2014 19:53:36 +0400 Subject: [PATCH] Refactoring kotlin-maven-plugin: * cleanup KotlinCompileMojoBase moved JVM specific things to KotlinCompileMojo; * add support noInline in JS. --- .../jetbrains/kotlin/maven/doc/KDocMojo.java | 87 ++----- .../tools/kotlin-maven-plugin-test/pom.xml | 5 + .../src/it/test-js-extraArguments/pom.xml | 47 ++++ .../main/kotlin/org/jetbrains/HelloWorld.kt | 9 + .../src/it/test-js-extraArguments/verify.bsh | 6 + .../kotlin/maven/K2JSCompilerMojo.java | 48 ++-- .../kotlin/maven/K2JVMCompileMojo.java | 243 ++++++++++++++++++ .../kotlin/maven/KotlinCompileMojo.java | 39 --- .../kotlin/maven/KotlinCompileMojoBase.java | 233 +++-------------- .../kotlin/maven/KotlinTestCompileMojo.java | 17 +- 10 files changed, 395 insertions(+), 339 deletions(-) create mode 100644 libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/pom.xml create mode 100644 libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/src/main/kotlin/org/jetbrains/HelloWorld.kt create mode 100644 libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/verify.bsh create mode 100644 libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java delete mode 100644 libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java diff --git a/libraries/tools/kdoc-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/doc/KDocMojo.java b/libraries/tools/kdoc-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/doc/KDocMojo.java index 0db657c1a58..4ea48ab0e50 100644 --- a/libraries/tools/kdoc-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/doc/KDocMojo.java +++ b/libraries/tools/kdoc-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/doc/KDocMojo.java @@ -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 classpath; - - /** - * Project test classpath. - * - * @parameter default-value="${project.testClasspathElements}" - * @required - * @readonly - */ - protected List 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 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 packageSummaryText; + @Override + public List 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 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!"); } } diff --git a/libraries/tools/kotlin-maven-plugin-test/pom.xml b/libraries/tools/kotlin-maven-plugin-test/pom.xml index 45d894a6ed1..07a1c534634 100644 --- a/libraries/tools/kotlin-maven-plugin-test/pom.xml +++ b/libraries/tools/kotlin-maven-plugin-test/pom.xml @@ -23,6 +23,11 @@ kotlin-runtime ${project.version} + + org.jetbrains.kotlin + kotlin-js-library + ${project.version} + diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/pom.xml b/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/pom.xml new file mode 100644 index 00000000000..599d0addabe --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + + org.jetbrains.kotlin + kotlin-project + 0.1-SNAPSHOT + ../../pom.xml + + + test-js-extraArguments + + + ${project.basedir}/src/main/kotlin + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${project.version} + + + org.jetbrains.kotlin + kotlin-js-library + ${project.version} + + + + + compile + + js + + + + + + -Xno-inline + + + + + + + diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/src/main/kotlin/org/jetbrains/HelloWorld.kt b/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/src/main/kotlin/org/jetbrains/HelloWorld.kt new file mode 100644 index 00000000000..44d4071d9e3 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/src/main/kotlin/org/jetbrains/HelloWorld.kt @@ -0,0 +1,9 @@ +package org.jetbrains + +fun main(args : Array) { + println(getGreeting()) +} + +fun getGreeting() : String { + return "Hello, World!" +} diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/verify.bsh b/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/verify.bsh new file mode 100644 index 00000000000..b5da31475d7 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/test-js-extraArguments/verify.bsh @@ -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); +} diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java index 79508e91ae0..373fa9ddfec 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JSCompilerMojo.java @@ -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 { 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 inputSupplier = new InputSupplier() { @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 inputSupplier = new InputSupplier() { @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 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(); } } diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java new file mode 100644 index 00000000000..3b0bc543265 --- /dev/null +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java @@ -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 { + /** + * The directories used to scan for annotation.xml files for Kotlin annotations + * + * @parameter + */ + public List annotationPaths; + + /** + * @parameter default-value="true" + */ + public boolean scanForAnnotations; + + /** + * Project classpath. + * + * @parameter default-value="${project.compileClasspathElements}" + * @required + * @readonly + */ + public List classpath; + + /** + * Project test classpath. + * + * @parameter default-value="${project.testClasspathElements}" + * @required + * @readonly + */ + protected List 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 classpathList = new ArrayList(); + + 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 annotations) { + String jdkAnnotation = getJdkAnnotations().getPath(); + + List list = new ArrayList(); + 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 scanAnnotations(Log log) { + List annotations = new ArrayList(); + + Set 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 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; + } +} + diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java deleted file mode 100644 index 5c7abd3829d..00000000000 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojo.java +++ /dev/null @@ -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); - } - } -} - diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java index 6c0a47e64c9..67027918022 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java @@ -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 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 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 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 classpath; - - /** - * Project test classpath. - * - * @parameter default-value="${project.testClasspathElements}" - * @required - * @readonly - */ - protected List testClasspath; - /** * The directory for compiled classes. * @@ -153,9 +113,12 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo { */ public List 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 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 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 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 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 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 sources, List classpath, String output) throws MojoExecutionException { - // don't include runtime, it should be in maven dependencies - arguments.noStdlib = true; - - ArrayList classpathList = new ArrayList(); - - 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 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 annotations) { - String jdkAnnotation = getJdkAnnotations().getPath(); - - List list = new ArrayList(); - 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 scanAnnotations(Log log) { - List annotations = new ArrayList(); - - Set 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 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; } } diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java index b49d2f28aa0..f9a39c90013 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinTestCompileMojo.java @@ -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); } }