|
| 1 | +import org.apache.maven.model.Dependency; |
| 2 | +import org.apache.maven.plugin.AbstractMojo; |
| 3 | +import org.apache.maven.plugin.MojoExecutionException; |
| 4 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 5 | +import org.apache.maven.plugins.annotations.Mojo; |
| 6 | +import org.apache.maven.plugins.annotations.Parameter; |
| 7 | +import org.apache.maven.project.MavenProject; |
| 8 | + |
| 9 | +import java.io.*; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +@Mojo(name = "add-custom-sourceRoots", defaultPhase = LifecyclePhase.COMPILE) |
| 14 | +public class AddCustomSourceRootsMojo |
| 15 | + extends AbstractMojo { |
| 16 | + @Parameter(defaultValue = "${project}", required = true, readonly = true) |
| 17 | + public MavenProject project; |
| 18 | + |
| 19 | + public void execute() |
| 20 | + throws MojoExecutionException { |
| 21 | + File targetGeneratedFolder = new File(project.getBasedir().getAbsolutePath(), "target/gen"); |
| 22 | + if (!targetGeneratedFolder.exists()) { |
| 23 | + if (!targetGeneratedFolder.mkdirs()) { |
| 24 | + throw new MojoExecutionException("Failed to create target directory."); |
| 25 | + } |
| 26 | + } |
| 27 | + File generatedKtFile = new File(targetGeneratedFolder, "new.kt"); |
| 28 | + |
| 29 | + try { |
| 30 | + String str = "fun foo(){\n" + |
| 31 | + " println(\"2\")\n" + |
| 32 | + "}"; |
| 33 | + BufferedWriter writer = new BufferedWriter(new FileWriter(generatedKtFile)); |
| 34 | + writer.write(str); |
| 35 | + writer.close(); |
| 36 | + } catch (IOException e) { |
| 37 | + throw new MojoExecutionException(e.getMessage()); |
| 38 | + } |
| 39 | + |
| 40 | + getLog().info("path: " + generatedKtFile.getAbsolutePath()); |
| 41 | + project.addCompileSourceRoot(targetGeneratedFolder.getAbsolutePath()); |
| 42 | + getLog().info("CompileSourceRoots: " + project.getCompileSourceRoots().toString()); |
| 43 | + } |
| 44 | +} |
0 commit comments