Maven: Introduce test-js goal
#KT-7757 Fixed Maven K2JS should be able to build tests too
This commit is contained in:
@@ -39,6 +39,12 @@
|
||||
<goal>js</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>js tests</id>
|
||||
<goals>
|
||||
<goal>test-js</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package test.sample
|
||||
|
||||
import sample.Hello
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
class SampleTest {
|
||||
Test fun dummy(): Unit {
|
||||
|
||||
Hello().doSomething()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
<maven-plugin-anno.version>1.4.1</maven-plugin-anno.version>
|
||||
<maven.version>3.0.4</maven.version>
|
||||
<maven.version>3.0.5</maven.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
@@ -30,6 +30,12 @@
|
||||
<artifactId>maven-plugin-api</artifactId>
|
||||
<version>${maven.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.plugin-tools</groupId>
|
||||
<artifactId>maven-plugin-annotations</artifactId>
|
||||
<version>3.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-compiler</artifactId>
|
||||
|
||||
+61
-12
@@ -16,8 +16,13 @@
|
||||
|
||||
package org.jetbrains.kotlin.maven;
|
||||
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
||||
import org.apache.maven.plugins.annotations.Mojo;
|
||||
import org.apache.maven.plugins.annotations.Parameter;
|
||||
import org.apache.maven.plugins.annotations.ResolutionScope;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
|
||||
import org.jetbrains.kotlin.cli.js.K2JSCompiler;
|
||||
@@ -26,52 +31,72 @@ import org.jetbrains.kotlin.utils.LibraryUtils;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* Converts Kotlin to JavaScript code
|
||||
*
|
||||
* @goal js
|
||||
* @phase compile
|
||||
* @requiresDependencyResolution compile
|
||||
* @noinspection UnusedDeclaration
|
||||
*/
|
||||
@Mojo(name = "js", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
|
||||
public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArguments> {
|
||||
|
||||
private static final String OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME = "outputDirectoriesCollector";
|
||||
private static final Lock lock = new ReentrantLock();
|
||||
|
||||
/**
|
||||
* The output JS file name
|
||||
*
|
||||
* @required
|
||||
* @parameter default-value="${project.build.directory}/js/${project.artifactId}.js"
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.build.directory}/js/${project.artifactId}.js", required = true)
|
||||
private String outputFile;
|
||||
|
||||
/**
|
||||
* The output metafile name
|
||||
*
|
||||
* @parameter default-value="${project.build.directory}/js/${project.artifactId}.meta.js"
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.build.directory}/js/${project.artifactId}.meta.js")
|
||||
private String metaFile;
|
||||
|
||||
@Override
|
||||
/**
|
||||
* Flags enables or disable source map generation
|
||||
*/
|
||||
@Parameter(defaultValue = "false")
|
||||
private boolean sourceMap;
|
||||
|
||||
@Override
|
||||
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException {
|
||||
arguments.outputFile = outputFile;
|
||||
arguments.noStdlib = true;
|
||||
arguments.metaInfo = metaFile;
|
||||
|
||||
List<String> libraries = getKotlinJavascriptLibraryFiles();
|
||||
LOG.info("libraryFiles: " + libraries);
|
||||
arguments.libraryFiles = libraries.toArray(new String[0]);
|
||||
LOG.debug("libraryFiles: " + libraries);
|
||||
arguments.libraryFiles = ArrayUtil.toStringArray(libraries);
|
||||
|
||||
arguments.sourceMap = sourceMap;
|
||||
|
||||
Set<String> collector = getOutputDirectoriesCollector();
|
||||
|
||||
if (outputFile != null) {
|
||||
collector.add(new File(outputFile).getParent());
|
||||
}
|
||||
if (metaFile != null) {
|
||||
collector.add(new File(metaFile).getParent());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all Kotlin Javascript dependencies that this project has, including transitive ones.
|
||||
*
|
||||
* @return array of paths to kotlin javascript libraries
|
||||
*/
|
||||
@NotNull
|
||||
private List<String> getKotlinJavascriptLibraryFiles() {
|
||||
List<String> libraries = new ArrayList<String>();
|
||||
|
||||
for(Artifact artifact : project.getArtifacts()) {
|
||||
for (Artifact artifact : project.getArtifacts()) {
|
||||
if (artifact.getScope().equals(Artifact.SCOPE_COMPILE)) {
|
||||
File file = artifact.getFile();
|
||||
if (LibraryUtils.isKotlinJavascriptLibrary(file)) {
|
||||
@@ -83,6 +108,15 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
|
||||
}
|
||||
}
|
||||
|
||||
for (String file : getOutputDirectoriesCollector()) {
|
||||
if (new File(file).exists()) {
|
||||
libraries.add(file);
|
||||
}
|
||||
else {
|
||||
LOG.warn("JS output directory missing: " + file);
|
||||
}
|
||||
}
|
||||
|
||||
return libraries;
|
||||
}
|
||||
|
||||
@@ -97,4 +131,19 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
|
||||
protected K2JSCompiler createCompiler() {
|
||||
return new K2JSCompiler();
|
||||
}
|
||||
|
||||
private Set<String> getOutputDirectoriesCollector() {
|
||||
lock.lock();
|
||||
try {
|
||||
Set<String> collector = (Set<String>) getPluginContext().get(OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME);
|
||||
if (collector == null) {
|
||||
collector = new ConcurrentSkipListSet<String>();
|
||||
getPluginContext().put(OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME, collector);
|
||||
}
|
||||
|
||||
return collector;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.artifact.Artifact;
|
||||
import org.apache.maven.model.Plugin;
|
||||
import org.apache.maven.model.PluginExecution;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
||||
import org.apache.maven.plugins.annotations.Mojo;
|
||||
import org.apache.maven.plugins.annotations.Parameter;
|
||||
import org.apache.maven.plugins.annotations.ResolutionScope;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
|
||||
import org.jetbrains.kotlin.cli.js.K2JSCompiler;
|
||||
import org.jetbrains.kotlin.utils.LibraryUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Converts Kotlin to JavaScript code
|
||||
*
|
||||
* @noinspection UnusedDeclaration
|
||||
*/
|
||||
@Mojo(name = "test-js",
|
||||
defaultPhase = LifecyclePhase.TEST_COMPILE,
|
||||
requiresDependencyResolution = ResolutionScope.TEST
|
||||
)
|
||||
public class KotlinTestJSCompilerMojo extends K2JSCompilerMojo {
|
||||
|
||||
/**
|
||||
* Flag to allow test compilation to be skipped.
|
||||
*/
|
||||
@Parameter(property = "maven.test.skip", defaultValue = "false")
|
||||
private boolean skip;
|
||||
|
||||
/**
|
||||
* The default source directories containing the sources to be compiled.
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.testCompileSourceRoots}", required = true)
|
||||
private List<String> defaultSourceDirs;
|
||||
|
||||
/**
|
||||
* The source directories containing the sources to be compiled.
|
||||
*/
|
||||
@Parameter
|
||||
private List<String> sourceDirs;
|
||||
|
||||
@Override
|
||||
public List<String> getSources() {
|
||||
if (sourceDirs != null && !sourceDirs.isEmpty()) return sourceDirs;
|
||||
return defaultSourceDirs;
|
||||
}
|
||||
|
||||
/**
|
||||
* The output JS file name
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.build.directory}/test-js/${project.artifactId}-tests.js", required = true)
|
||||
private String outputFile;
|
||||
|
||||
/**
|
||||
* The output metafile name
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.build.directory}/test-js/${project.artifactId}-tests.meta.js")
|
||||
private String metaFile;
|
||||
|
||||
@Override
|
||||
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException {
|
||||
module = testModule;
|
||||
output = testOutput;
|
||||
|
||||
super.configureSpecificCompilerArguments(arguments);
|
||||
|
||||
arguments.outputFile = outputFile;
|
||||
arguments.metaInfo = metaFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() throws MojoExecutionException, MojoFailureException {
|
||||
if (skip) {
|
||||
getLog().info("Test compilation is skipped");
|
||||
}
|
||||
else {
|
||||
super.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user