|
| 1 | +package eu.stamp_project; |
| 2 | + |
| 3 | +import eu.stamp_project.utils.DSpotUtils; |
| 4 | +import eu.stamp_project.utils.program.ConstantsProperties; |
| 5 | +import org.apache.maven.plugin.AbstractMojo; |
| 6 | +import org.apache.maven.plugin.MojoExecutionException; |
| 7 | +import org.apache.maven.plugin.MojoFailureException; |
| 8 | +import org.apache.maven.plugins.annotations.Mojo; |
| 9 | +import org.apache.maven.plugins.annotations.Parameter; |
| 10 | +import org.apache.maven.project.MavenProject; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.FileWriter; |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.nio.file.Paths; |
| 18 | +import java.util.Properties; |
| 19 | + |
| 20 | +/** |
| 21 | + * created by Benjamin DANGLOT |
| 22 | + * benjamin.danglot@inria.fr |
| 23 | + * on 21/01/19 |
| 24 | + * <p> |
| 25 | + * This maven plugin will generate a default properties with minimal values to use dspot |
| 26 | + */ |
| 27 | +@Mojo(name = "generate-properties") |
| 28 | +public class GeneratePropertiesMojo extends AbstractMojo { |
| 29 | + |
| 30 | + private final String COMMENT = "This properties file has been automatically generated using generate-properties of dspot-maven"; |
| 31 | + |
| 32 | + /** |
| 33 | + * Configure the output path of the generated properties file. |
| 34 | + */ |
| 35 | + @Parameter(defaultValue = "dspot.properties", property = "output") |
| 36 | + private String outputPath; |
| 37 | + |
| 38 | + /** |
| 39 | + * Maven project for which we want to produce a properties file. |
| 40 | + */ |
| 41 | + @Parameter(defaultValue = "${project}", required = true, readonly = true) |
| 42 | + private MavenProject project; |
| 43 | + |
| 44 | + private Properties properties; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void execute() throws MojoExecutionException, MojoFailureException { |
| 48 | + this.properties = new Properties(); |
| 49 | + final String rootPath = this.project.getBasedir().getAbsolutePath(); |
| 50 | + if (this.project.getParent() != null) { |
| 51 | + final MavenProject topParent = getTopParent(); |
| 52 | + final String pathOfTopParent = topParent.getBasedir().getAbsolutePath(); |
| 53 | + this.properties.put(ConstantsProperties.PROJECT_ROOT_PATH.getName(), pathOfTopParent); |
| 54 | + this.properties.put(ConstantsProperties.MODULE.getName(), rootPath); |
| 55 | + } else { |
| 56 | + this.properties.put(ConstantsProperties.PROJECT_ROOT_PATH.getName(), rootPath); |
| 57 | + } |
| 58 | + this.properties.put(ConstantsProperties.SRC_CODE.getName(), this.project.getCompileSourceRoots().get(0)); |
| 59 | + this.properties.put(ConstantsProperties.TEST_SRC_CODE.getName(), this.project.getTestCompileSourceRoots().get(0)); |
| 60 | + final File testSrcDirectory = new File(this.project.getCompileSourceRoots().get(0)); |
| 61 | + this.properties.put(ConstantsProperties.FILTER.getName(), buildFilter(testSrcDirectory)); |
| 62 | + this.output(); |
| 63 | + } |
| 64 | + |
| 65 | + private String buildFilter(File testSrcDirectory) { |
| 66 | + try { |
| 67 | + final Path pathToTopPackage = Files.walk(Paths.get(testSrcDirectory.getAbsolutePath())) |
| 68 | + .filter(path -> path.toFile().isDirectory()) |
| 69 | + .filter(path -> path.toFile().listFiles(pathname -> pathname.getAbsolutePath().endsWith(".java")).length > 0) |
| 70 | + .findFirst() |
| 71 | + .get(); |
| 72 | + return pathToTopPackage.toFile() |
| 73 | + .getAbsolutePath() |
| 74 | + .substring(DSpotUtils.shouldAddSeparator.apply(testSrcDirectory.getAbsolutePath()).length()) |
| 75 | + .replaceAll("/", ".") + "*"; |
| 76 | + } catch (Exception e) { |
| 77 | + throw new RuntimeException(e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private MavenProject getTopParent() { |
| 82 | + MavenProject parent = this.project.getParent(); |
| 83 | + while (parent.getParent() != null) { |
| 84 | + parent = parent.getParent(); |
| 85 | + } |
| 86 | + return parent; |
| 87 | + } |
| 88 | + |
| 89 | + private void output() { |
| 90 | + try { |
| 91 | + this.properties.store(new FileWriter(this.project.getBasedir().getAbsolutePath() + outputPath), COMMENT); |
| 92 | + } catch (IOException e) { |
| 93 | + throw new RuntimeException(e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // VISIBLE FOR TESTING |
| 98 | + String getOutputPath() { |
| 99 | + return this.outputPath; |
| 100 | + } |
| 101 | + |
| 102 | + MavenProject getProject() { |
| 103 | + return this.project; |
| 104 | + } |
| 105 | +} |
0 commit comments