Skip to content

Instantly share code, notes, and snippets.

@bdemers
Created October 15, 2019 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdemers/53203fe313776932a7113a8e91ee5911 to your computer and use it in GitHub Desktop.
Save bdemers/53203fe313776932a7113a8e91ee5911 to your computer and use it in GitHub Desktop.
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.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.File;
/**
* Sets up common configuration needed for a kotlin project. Adds {@code src/main/kotlin} and {@code src/test/kotlin} as
* source (if the exist). Removes {@code src/main/java} and {@code src/test/java} if they do not.
*/
@Mojo(name = "configure")
public class KotlinConfigureMojo extends AbstractMojo {
@Parameter(property = "project")
private MavenProject project;
@Parameter(defaultValue = "${project.basedir}/src/main/kotlin", readonly = true)
private String kotlinSourceDirectory;
@Parameter(defaultValue = "${project.build.sourceDirectory}", readonly = true)
private String javaSourceDirectory;
@Parameter(defaultValue = "${project.basedir}/src/test/kotlin", readonly = true)
private String kotlinTestSourceDirectory;
@Parameter(defaultValue = "${project.build.testSourceDirectory}", readonly = true)
private String javaTestSourceDirectory;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// remove the default java source dirs if they do not exist
// Do NOT mess with any of the other source roots other plugins may add
if (!new File(javaSourceDirectory).exists()) {
project.getCompileSourceRoots().remove(javaSourceDirectory);
}
if (!new File(javaTestSourceDirectory).exists()) {
project.getTestCompileSourceRoots().remove(javaTestSourceDirectory);
}
// add the kotlin ones if needed
if (new File(kotlinSourceDirectory).exists()) {
project.addCompileSourceRoot(kotlinSourceDirectory);
}
if (new File(kotlinTestSourceDirectory).exists()) {
project.addTestCompileSourceRoot(kotlinTestSourceDirectory);
}
getLog().debug("Configured sources: " + project.getCompileSourceRoots());
getLog().debug("Configured test sources: " + project.getTestCompileSourceRoots());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment