Skip to content

Commit

Permalink
Handle cases where programs have properties in their path
Browse files Browse the repository at this point in the history
  • Loading branch information
sianghwee committed Jan 22, 2022
1 parent 26d2353 commit d2c7eed
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/main/java/core/maven/MavenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MavenManager {
public final static String M2AFFIX = ".m2" + File.separator + "repository";
Expand Down Expand Up @@ -101,6 +104,25 @@ protected String getUserHomePath() {
public String getSrcDir(File pomFile) throws Exception{
Model pomModel = getPomModel(pomFile);
String srcDir = pomModel.getBuild().getSourceDirectory();
return srcDir == null ? String.format("src%cmain%cjava", File.separatorChar, File.separatorChar): srcDir;
return srcDir == null ? String.format("src%cmain%cjava", File.separatorChar, File.separatorChar):
replaceProperties(srcDir, pomModel);
}

private String replaceProperties(String s, Model pomModel) {
Properties props = pomModel.getProperties();
Pattern p = Pattern.compile("\\$\\{(.+)\\}");
Matcher m = p.matcher(s);
StringBuilder sb = new StringBuilder();
int start = 0;
while(m.find()) {
sb.append(s.substring(start, m.start()));
String prop = props.getProperty(m.group(1));
if (prop != null) {
sb.append(prop);
}
start = m.end();
}
sb.append(s.substring(start));
return sb.toString();
}
}

0 comments on commit d2c7eed

Please sign in to comment.