forked from xaniox/set-property-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added concept of Extractions - to extract tokens from text into proje…
…ct properties using regular expressions
- Loading branch information
Showing
6 changed files
with
134 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**/target/ | ||
.idea/ | ||
*.iml | ||
*.log |
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
17 changes: 12 additions & 5 deletions
17
src/main/java/de/matzefratze123/setproperty/Condition.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
69 changes: 69 additions & 0 deletions
69
src/main/java/de/matzefratze123/setproperty/Extraction.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,69 @@ | ||
package de.matzefratze123.setproperty; | ||
|
||
import org.apache.maven.plugin.logging.Log; | ||
import org.apache.maven.project.MavenProject; | ||
|
||
import java.util.Properties; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class Extraction { | ||
private String property; | ||
private String text; | ||
private String regex; | ||
private String defaultValue; | ||
private boolean caseSensitive = true; | ||
private boolean toLowerCase; | ||
|
||
public void process(MavenProject project, Log log) { | ||
final Properties properties = project.getProperties(); | ||
|
||
if (property == null) { | ||
log.warn("'property' element of 'extraction' element is required. skipped"); | ||
} | ||
if (text == null) { | ||
log.warn("'value' element of 'extraction' element is required. skipped"); | ||
} | ||
if (regex == null) { | ||
log.warn("'regex' element of 'extraction' element is required. skipped"); | ||
} | ||
|
||
Pattern pattern = caseSensitive ? Pattern.compile(regex) : Pattern.compile(regex, Pattern.CASE_INSENSITIVE); | ||
final Matcher matcher = pattern.matcher(text); | ||
if (matcher.find()) { | ||
int groupCount = matcher.groupCount(); | ||
String v = null; | ||
for (int i = 1; i <= groupCount && (v == null || v.length() == 0); i++) { | ||
v = matcher.group(i); | ||
} | ||
if (v == null || v.length() == 0) { | ||
if (defaultValue != null) { | ||
properties.setProperty(property, defaultValue); | ||
log.info("Setting property '" + property + "' to default value '" + defaultValue + "'"); | ||
} else { | ||
log.info("No match was found for property '" + property); | ||
} | ||
} else { | ||
properties.setProperty(property, toLowerCase ? v.toLowerCase() : v); | ||
log.info("Setting property '" + property + "' to '" + v + "'"); | ||
} | ||
} else { | ||
log.info("No match was found for property '" + property); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
final Matcher matcher = Pattern.compile(".*\\[DEPLOY\\s+(VERSION|SNAPSHOT|SKIP)+].*|.*\\[(DEPLOY)+].*").matcher( | ||
"hello [DEPLOY VERSION] world"); | ||
final boolean found = matcher.find(); | ||
System.out.println(found); | ||
if (found) { | ||
final int groupCount = matcher.groupCount(); | ||
System.out.println(groupCount); | ||
System.out.println(matcher.group()); | ||
for (int i = 1; i <= groupCount; i++) { | ||
System.out.println(matcher.group(i)); | ||
} | ||
} | ||
} | ||
} |
53 changes: 32 additions & 21 deletions
53
src/main/java/de/matzefratze123/setproperty/SetPropertyMojo.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 |
---|---|---|
@@ -1,34 +1,45 @@ | ||
package de.matzefratze123.setproperty; | ||
|
||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
|
||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
@Mojo(name = "set-properties", defaultPhase = LifecyclePhase.VALIDATE) | ||
public class SetPropertyMojo extends AbstractMojo { | ||
|
||
@Parameter(defaultValue = "${project}", readonly = true, required = true) | ||
private org.apache.maven.project.MavenProject project; | ||
|
||
@Parameter | ||
private List<Operation> operations; | ||
|
||
public void execute() throws MojoExecutionException, MojoFailureException { | ||
Properties properties = project.getProperties(); | ||
|
||
for (Operation operation : operations) { | ||
List<SetProperty> propertiesSet = operation.execute(properties); | ||
|
||
for (SetProperty prop : propertiesSet) { | ||
getLog().info("Setting property '" + prop.getProperty() + "' to '" + prop.getValue() + "'..."); | ||
} | ||
} | ||
} | ||
|
||
@Parameter(defaultValue = "${project}", readonly = true, required = true) | ||
private org.apache.maven.project.MavenProject project; | ||
|
||
@Parameter | ||
private List<Operation> operations; | ||
|
||
@Parameter | ||
private List<Extraction> extractions; | ||
|
||
public void execute() throws MojoExecutionException, MojoFailureException { | ||
Properties properties = project.getProperties(); | ||
|
||
if (operations != null) { | ||
for (Operation operation : operations) { | ||
List<SetProperty> propertiesSet = operation.execute(properties); | ||
|
||
for (SetProperty prop : propertiesSet) { | ||
getLog().info("Setting property '" + prop.getProperty() + "' to '" + prop.getValue() + "'..."); | ||
} | ||
} | ||
} | ||
|
||
if (extractions != null) { | ||
for (Extraction extraction : extractions) { | ||
extraction.process(project, getLog()); | ||
} | ||
} | ||
} | ||
|
||
} |