Initial implementation of maven plugin
This commit is contained in:
+21
-10
@@ -1,5 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2012 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;
|
package org.jetbrains.kotlin.maven;
|
||||||
|
|
||||||
|
import org.apache.maven.plugin.MojoExecutionException;
|
||||||
import org.jetbrains.jet.cli.CompilerArguments;
|
import org.jetbrains.jet.cli.CompilerArguments;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -8,8 +25,7 @@ import org.jetbrains.jet.cli.CompilerArguments;
|
|||||||
* @goal js
|
* @goal js
|
||||||
* @phase compile
|
* @phase compile
|
||||||
*/
|
*/
|
||||||
public class K2JSCompilerMojo extends KotlinBaseMojo {
|
public class K2JSCompilerMojo extends KotlinCompileMojo {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The output JS file name
|
* The output JS file name
|
||||||
*
|
*
|
||||||
@@ -19,18 +35,13 @@ public class K2JSCompilerMojo extends KotlinBaseMojo {
|
|||||||
private String outFile;
|
private String outFile;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureCompilerArguments(CompilerArguments arguments) {
|
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
|
||||||
super.configureCompilerArguments(arguments);
|
super.configureCompilerArguments(arguments);
|
||||||
|
|
||||||
K2JSCompilerPlugin plugin = new K2JSCompilerPlugin();
|
K2JSCompilerPlugin plugin = new K2JSCompilerPlugin();
|
||||||
plugin.setOutFile(outFile);
|
plugin.setOutFile(outFile);
|
||||||
arguments.getCompilerPlugins().add(plugin);
|
arguments.getCompilerPlugins().add(plugin);
|
||||||
|
|
||||||
getLog().info("Compiling Kotlin src from " + arguments.getSrc() + " to JavaScript at: " + outFile);
|
getLog().info("Compiling Kotlin src from " + arguments.getSrc() + " to JavaScript at: " + outFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected CompilerArguments createCompilerArguments() {
|
|
||||||
CompilerArguments answer = new CompilerArguments();
|
|
||||||
return answer;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-17
@@ -1,7 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2012 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;
|
package org.jetbrains.kotlin.maven;
|
||||||
|
|
||||||
|
import com.google.common.io.Files;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
import jet.Function1;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.compiler.CompilerPlugin;
|
import org.jetbrains.jet.compiler.CompilerPlugin;
|
||||||
import org.jetbrains.jet.compiler.CompilerPluginContext;
|
import org.jetbrains.jet.compiler.CompilerPluginContext;
|
||||||
@@ -11,13 +27,11 @@ import org.jetbrains.k2js.config.Config;
|
|||||||
import org.jetbrains.k2js.facade.K2JSTranslator;
|
import org.jetbrains.k2js.facade.K2JSTranslator;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static kotlin.io.namespace.use;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compiles Kotlin code to JavaScript
|
* Compiles Kotlin code to JavaScript
|
||||||
*/
|
*/
|
||||||
@@ -30,6 +44,7 @@ public class K2JSCompilerPlugin implements CompilerPlugin {
|
|||||||
Project project = context.getProject();
|
Project project = context.getProject();
|
||||||
BindingContext bindingContext = context.getContext();
|
BindingContext bindingContext = context.getContext();
|
||||||
List<JetFile> sources = context.getFiles();
|
List<JetFile> sources = context.getFiles();
|
||||||
|
|
||||||
if (bindingContext != null && sources != null && project != null) {
|
if (bindingContext != null && sources != null && project != null) {
|
||||||
Config config = new Config(project) {
|
Config config = new Config(project) {
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -38,6 +53,7 @@ public class K2JSCompilerPlugin implements CompilerPlugin {
|
|||||||
return new ArrayList<JetFile>();
|
return new ArrayList<JetFile>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
K2JSTranslator translator = new K2JSTranslator(config);
|
K2JSTranslator translator = new K2JSTranslator(config);
|
||||||
final String code = translator.generateProgramCode(sources);
|
final String code = translator.generateProgramCode(sources);
|
||||||
File file = new File(outFile);
|
File file = new File(outFile);
|
||||||
@@ -46,20 +62,9 @@ public class K2JSCompilerPlugin implements CompilerPlugin {
|
|||||||
outDir.mkdirs();
|
outDir.mkdirs();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
use(new FileWriter(file), new Function1<FileWriter, Object>() {
|
Files.write(code, file, Charset.forName("UTF-8"));
|
||||||
@Override
|
|
||||||
public Object invoke(FileWriter writer) {
|
|
||||||
try {
|
|
||||||
writer.write(code);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Error: " + e);
|
throw new RuntimeException(e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-62
@@ -1,62 +0,0 @@
|
|||||||
package org.jetbrains.kotlin.maven;
|
|
||||||
|
|
||||||
import org.apache.maven.plugin.AbstractMojo;
|
|
||||||
import org.apache.maven.plugin.MojoExecutionException;
|
|
||||||
import org.apache.maven.plugin.MojoFailureException;
|
|
||||||
import org.jetbrains.jet.cli.CompilerArguments;
|
|
||||||
import org.jetbrains.jet.cli.KotlinCompiler;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstract base class for Kotlin maven plugins
|
|
||||||
*/
|
|
||||||
public abstract class KotlinBaseMojo extends AbstractMojo {
|
|
||||||
private KotlinCompiler compiler = new KotlinCompiler();
|
|
||||||
private CompilerArguments arguments;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The source directory to compile
|
|
||||||
*
|
|
||||||
* @required
|
|
||||||
* @parameter default-value="${basedir}/src/main/kotlin"
|
|
||||||
*/
|
|
||||||
private String src;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The output directory for bytecode classes
|
|
||||||
*
|
|
||||||
* @required
|
|
||||||
* @parameter default-value="${project.build.directory}/classes"
|
|
||||||
*/
|
|
||||||
private String outputDir;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute() throws MojoExecutionException, MojoFailureException {
|
|
||||||
arguments = createCompilerArguments();
|
|
||||||
|
|
||||||
if (src != null) {
|
|
||||||
arguments.setSrc(src);
|
|
||||||
}
|
|
||||||
if (outputDir != null) {
|
|
||||||
arguments.setOutputDir(outputDir);
|
|
||||||
}
|
|
||||||
configureCompilerArguments(arguments);
|
|
||||||
|
|
||||||
compiler.exec(System.err, arguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Derived classes can create custom compiler argument implementations
|
|
||||||
* such as for KDoc
|
|
||||||
*/
|
|
||||||
protected CompilerArguments createCompilerArguments() {
|
|
||||||
return new CompilerArguments();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Derived classes can register custom plugins or configurations
|
|
||||||
*/
|
|
||||||
protected void configureCompilerArguments(CompilerArguments arguments) {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2012 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.CompilerArguments;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compiles kotlin sources
|
||||||
|
*
|
||||||
|
* @goal compile
|
||||||
|
* @phase compile
|
||||||
|
* @requiresDependencyResolution compile
|
||||||
|
*/
|
||||||
|
public class KotlinCompileMojo extends KotlinCompileMojoBase {
|
||||||
|
@Override
|
||||||
|
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
|
||||||
|
configureBaseCompilerArguments(getLog(), arguments, module, sources, classpath, output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+167
@@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2012 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.google.common.base.Joiner;
|
||||||
|
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.jetbrains.jet.cli.CompilerArguments;
|
||||||
|
import org.jetbrains.jet.cli.KotlinCompiler;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class KotlinCompileMojoBase extends AbstractMojo {
|
||||||
|
/**
|
||||||
|
* The source directories containing the sources to be compiled.
|
||||||
|
*
|
||||||
|
* @parameter default-value="${project.compileSourceRoots}"
|
||||||
|
* @required
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
public List<String> sources;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The source directories containing the sources to be compiled for tests.
|
||||||
|
*
|
||||||
|
* @parameter default-value="${project.testCompileSourceRoots}"
|
||||||
|
* @required
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
protected List<String> testSources;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @parameter default-value="${project.build.outputDirectory}"
|
||||||
|
* @required
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
public String output;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The directory for compiled tests classes.
|
||||||
|
*
|
||||||
|
* @parameter default-value="${project.build.testOutputDirectory}"
|
||||||
|
* @required
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
public String testOutput;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kotlin compilation module, as alternative to source files or folders.
|
||||||
|
*
|
||||||
|
* @parameter
|
||||||
|
*/
|
||||||
|
public String module;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kotlin compilation module, as alternative to source files or folders (for tests).
|
||||||
|
*
|
||||||
|
* @parameter
|
||||||
|
*/
|
||||||
|
public String testModule;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() throws MojoExecutionException, MojoFailureException {
|
||||||
|
final CompilerArguments arguments = createCompilerArguments();
|
||||||
|
|
||||||
|
configureCompilerArguments(arguments);
|
||||||
|
|
||||||
|
final KotlinCompiler compiler = createCompiler();
|
||||||
|
|
||||||
|
final KotlinCompiler.ExitCode exitCode = compiler.exec(System.err, arguments);
|
||||||
|
|
||||||
|
switch (exitCode) {
|
||||||
|
case COMPILATION_ERROR:
|
||||||
|
throw new MojoExecutionException("Compilation error. See log for more details");
|
||||||
|
|
||||||
|
case INTERNAL_ERROR:
|
||||||
|
throw new MojoExecutionException("Internal compiler error. See log for more details");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected KotlinCompiler createCompiler() {
|
||||||
|
return new KotlinCompiler();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derived classes can create custom compiler argument implementations
|
||||||
|
* such as for KDoc
|
||||||
|
*/
|
||||||
|
protected CompilerArguments createCompilerArguments() {
|
||||||
|
return new CompilerArguments();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derived classes can register custom plugins or configurations
|
||||||
|
*/
|
||||||
|
protected abstract void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException;
|
||||||
|
|
||||||
|
protected static void configureBaseCompilerArguments(Log log, CompilerArguments arguments, String module,
|
||||||
|
List<String> sources, List<String> classpath, String output) throws MojoExecutionException {
|
||||||
|
if (module != null) {
|
||||||
|
log.info("Compiling Kotlin module " + module);
|
||||||
|
arguments.setModule(module);
|
||||||
|
} else {
|
||||||
|
if (sources.size() <= 0)
|
||||||
|
throw new MojoExecutionException("No source roots to compile");
|
||||||
|
|
||||||
|
if (sources.size() > 1)
|
||||||
|
throw new MojoExecutionException("Multiple source roots are not supported yet");
|
||||||
|
|
||||||
|
final String src = sources.get(0);
|
||||||
|
|
||||||
|
log.info("Compiling Kotlin sources from " + src);
|
||||||
|
arguments.setSrc(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
final ArrayList<String> classpathList = new ArrayList<String>(classpath);
|
||||||
|
|
||||||
|
if (classpathList.remove(output)) {
|
||||||
|
log.debug("Removed target directory from classpath (" + output + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
final String classPathString = Joiner.on(File.pathSeparator).join(classpathList);
|
||||||
|
log.info("Classpath: " + classPathString);
|
||||||
|
arguments.setClasspath(classPathString);
|
||||||
|
|
||||||
|
log.info("Classes directory is " + output);
|
||||||
|
arguments.setOutputDir(output);
|
||||||
|
}
|
||||||
|
}
|
||||||
+53
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2012 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.apache.maven.plugin.MojoFailureException;
|
||||||
|
import org.jetbrains.jet.cli.CompilerArguments;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compiles Kotlin test sources
|
||||||
|
*
|
||||||
|
* @goal test-compile
|
||||||
|
* @phase test-compile
|
||||||
|
* @requiresDependencyResolution test
|
||||||
|
*/
|
||||||
|
public class KotlinTestCompileMojo extends KotlinCompileMojoBase {
|
||||||
|
/**
|
||||||
|
* Flag to allow test compilation to be skipped.
|
||||||
|
*
|
||||||
|
* @parameter expression="${maven.test.skip}" default-value="false"
|
||||||
|
* @noinspection UnusedDeclaration
|
||||||
|
*/
|
||||||
|
private boolean skip;
|
||||||
|
|
||||||
|
public void execute() throws MojoExecutionException, MojoFailureException {
|
||||||
|
if (skip) {
|
||||||
|
getLog().info("Test compilation is skipped");
|
||||||
|
} else {
|
||||||
|
super.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configureCompilerArguments(CompilerArguments arguments) throws MojoExecutionException {
|
||||||
|
configureBaseCompilerArguments(
|
||||||
|
getLog(), arguments,
|
||||||
|
testModule, testSources, testClasspath, testOutput);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2012 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;
|
package org.jetbrains.kotlin.maven;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|||||||
Reference in New Issue
Block a user