Skip to content

Commit

Permalink
Added concept of Extractions - to extract tokens from text into proje…
Browse files Browse the repository at this point in the history
…ct properties using regular expressions
  • Loading branch information
roytmana committed Jul 8, 2017
1 parent 8a8d91e commit bee1ed9
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 27 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/target/
.idea/
*.iml
*.log
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ Example configuration:
</setpropertiesfalse>
</operation>
</operations>
<extractions>
<extraction>
<!--
Control maven deployment to deploy snapshot, version based on gitlab build number or skip deployment
Extracts XXX from [DEPLOY XXX] which should be should be one of VERSION, SNAPSHOT, SKIP
just [DEPLOY] without XX means [DEPLOY VERSION]
If no [DEPLOY XXX] tag is present, defaulting to SNAPSHOT maven deployment build
-->
<property>buildFlag.deployment</property>
<regex>.*\[DEPLOY\s+(VERSION|SNAPSHOT|SKIP)+].*|.*\[(DEPLOY)+].</regex>
<text>${git.commit.message.full}</text>
<caseSensitive>false</caseSensitive>
<toLowerCase>true</toLowerCase>
<defaultValue>SNAPSHOT</defaultValue>
</extraction>
</extractions>
</configuration>
</execution>
</executions>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.matzefratze123</groupId>
<artifactId>set-property-maven-plugin</artifactId>
<version>1.0.1</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<dependencies>
<dependency>
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/de/matzefratze123/setproperty/Condition.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package de.matzefratze123.setproperty;

import java.util.regex.Pattern;

public class Condition {

private String string;
private Operator operator;
private String operand;

private boolean caseSensitive = true;

public boolean process() {
if (operator == Operator.EQUALS) {
return string.equals(operand);
} else {
return string.matches(operand);
}
return string == null ? operand == null : caseSensitive ? string.equals(operand) : string.equalsIgnoreCase(operand);
} else {
if (string == null || operand == null) {
return string == operand;
}
Pattern pattern = caseSensitive ? Pattern.compile(operand) : Pattern.compile(operand, Pattern.CASE_INSENSITIVE);
return pattern.matcher(string).matches();
}
}

public enum Operator {
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/de/matzefratze123/setproperty/Extraction.java
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 src/main/java/de/matzefratze123/setproperty/SetPropertyMojo.java
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());
}
}
}

}

0 comments on commit bee1ed9

Please sign in to comment.