Created
October 15, 2019 19:15
-
-
Save bdemers/53203fe313776932a7113a8e91ee5911 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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