-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: maven plugin to automatically generate a properties file (#674)
* fix: clean the state and enable comment * feat: new maven plugin to generate a properties file * pom: remove old github repository * install testrunner * feat: the generator set up the filter * ci: disable dspot-maven command line
- Loading branch information
Showing
9 changed files
with
214 additions
and
59 deletions.
There are no files selected for viewing
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
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
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
105 changes: 105 additions & 0 deletions
105
dspot-maven/src/main/java/eu/stamp_project/GeneratePropertiesMojo.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package eu.stamp_project; | ||
|
||
import eu.stamp_project.utils.DSpotUtils; | ||
import eu.stamp_project.utils.program.ConstantsProperties; | ||
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; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Properties; | ||
|
||
/** | ||
* created by Benjamin DANGLOT | ||
* benjamin.danglot@inria.fr | ||
* on 21/01/19 | ||
* <p> | ||
* This maven plugin will generate a default properties with minimal values to use dspot | ||
*/ | ||
@Mojo(name = "generate-properties") | ||
public class GeneratePropertiesMojo extends AbstractMojo { | ||
|
||
private final String COMMENT = "This properties file has been automatically generated using generate-properties of dspot-maven"; | ||
|
||
/** | ||
* Configure the output path of the generated properties file. | ||
*/ | ||
@Parameter(defaultValue = "dspot.properties", property = "output") | ||
private String outputPath; | ||
|
||
/** | ||
* Maven project for which we want to produce a properties file. | ||
*/ | ||
@Parameter(defaultValue = "${project}", required = true, readonly = true) | ||
private MavenProject project; | ||
|
||
private Properties properties; | ||
|
||
@Override | ||
public void execute() throws MojoExecutionException, MojoFailureException { | ||
this.properties = new Properties(); | ||
final String rootPath = this.project.getBasedir().getAbsolutePath(); | ||
if (this.project.getParent() != null) { | ||
final MavenProject topParent = getTopParent(); | ||
final String pathOfTopParent = topParent.getBasedir().getAbsolutePath(); | ||
this.properties.put(ConstantsProperties.PROJECT_ROOT_PATH.getName(), pathOfTopParent); | ||
this.properties.put(ConstantsProperties.MODULE.getName(), rootPath); | ||
} else { | ||
this.properties.put(ConstantsProperties.PROJECT_ROOT_PATH.getName(), rootPath); | ||
} | ||
this.properties.put(ConstantsProperties.SRC_CODE.getName(), this.project.getCompileSourceRoots().get(0)); | ||
this.properties.put(ConstantsProperties.TEST_SRC_CODE.getName(), this.project.getTestCompileSourceRoots().get(0)); | ||
final File testSrcDirectory = new File(this.project.getCompileSourceRoots().get(0)); | ||
this.properties.put(ConstantsProperties.FILTER.getName(), buildFilter(testSrcDirectory)); | ||
this.output(); | ||
} | ||
|
||
private String buildFilter(File testSrcDirectory) { | ||
try { | ||
final Path pathToTopPackage = Files.walk(Paths.get(testSrcDirectory.getAbsolutePath())) | ||
.filter(path -> path.toFile().isDirectory()) | ||
.filter(path -> path.toFile().listFiles(pathname -> pathname.getAbsolutePath().endsWith(".java")).length > 0) | ||
.findFirst() | ||
.get(); | ||
return pathToTopPackage.toFile() | ||
.getAbsolutePath() | ||
.substring(DSpotUtils.shouldAddSeparator.apply(testSrcDirectory.getAbsolutePath()).length()) | ||
.replaceAll("/", ".") + "*"; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private MavenProject getTopParent() { | ||
MavenProject parent = this.project.getParent(); | ||
while (parent.getParent() != null) { | ||
parent = parent.getParent(); | ||
} | ||
return parent; | ||
} | ||
|
||
private void output() { | ||
try { | ||
this.properties.store(new FileWriter(this.project.getBasedir().getAbsolutePath() + outputPath), COMMENT); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
// VISIBLE FOR TESTING | ||
String getOutputPath() { | ||
return this.outputPath; | ||
} | ||
|
||
MavenProject getProject() { | ||
return this.project; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
dspot-maven/src/test/java/eu/stamp_project/GeneratePropertiesMojoTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package eu.stamp_project; | ||
|
||
import eu.stamp_project.utils.program.ConstantsProperties; | ||
import org.apache.maven.plugin.testing.MojoRule; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.util.Properties; | ||
|
||
import static junit.framework.TestCase.assertTrue; | ||
import static org.codehaus.plexus.PlexusTestCase.getBasedir; | ||
|
||
/** | ||
* created by Benjamin DANGLOT | ||
* benjamin.danglot@inria.fr | ||
* on 21/01/19 | ||
*/ | ||
public class GeneratePropertiesMojoTest { | ||
|
||
@Rule | ||
public MojoRule mojoRule = new MojoRule(); | ||
|
||
/* | ||
The mojo under test | ||
*/ | ||
private GeneratePropertiesMojo mojoUnderTest; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
File testPom = new File(getBasedir(), "src/test/resources/test-projects/"); | ||
mojoUnderTest = (GeneratePropertiesMojo) mojoRule.lookupConfiguredMojo(testPom , "generate-properties"); | ||
} | ||
|
||
|
||
@Test | ||
public void testOnTestProjects() throws Exception { | ||
mojoUnderTest.execute(); | ||
assertTrue(mojoUnderTest.getProject().getBasedir().getAbsolutePath() + mojoUnderTest.getOutputPath() + " does not exist!", | ||
new File(mojoUnderTest.getProject().getBasedir().getAbsolutePath() + mojoUnderTest.getOutputPath()).exists() | ||
); | ||
} | ||
|
||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,53 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>example</groupId> | ||
<artifactId>example</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>test-projects</name> | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>example</groupId> | ||
<artifactId>example</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>test-projects</name> | ||
|
||
<properties> | ||
<default.encoding>UTF-8</default.encoding> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
</properties> | ||
<properties> | ||
<default.encoding>UTF-8</default.encoding> | ||
<maven.compiler.source>1.7</maven.compiler.source> | ||
<maven.compiler.target>1.7</maven.compiler.target> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
<!-- Minimal configuration: specify at least the path to the properties file. --> | ||
<plugin> | ||
<groupId>eu.stamp-project</groupId> | ||
<artifactId>dspot-maven</artifactId> | ||
<version>1.1.1-SNAPSHOT</version> | ||
<configuration> | ||
<pathToProperties>src/test/resources/test-projects/test-projects.properties</pathToProperties> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<plugin> | ||
<groupId>eu.stamp-project</groupId> | ||
<artifactId>dspot-maven</artifactId> | ||
<version>1.2.2-SNAPSHOT</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>amplify-unit-tests</goal> | ||
</goals> | ||
<configuration> | ||
<pathToProperties>src/test/resources/test-projects/test-projects.properties</pathToProperties> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
</dependency> | ||
</dependencies> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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
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
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