Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wildcard support in file name / wso2-maven-json-merge-plugin #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions wso2-maven-json-merge-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@
*/
package org.wso2.maven;

import org.apache.commons.io.filefilter.WildcardFileFilter;
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 java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Mojo for Json Merge
Expand All @@ -51,8 +55,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
Map inputMap;
String targetPath = taskModel.getTarget();
for (String aFile : taskModel.getInclude()) {
inputMap = Utils.getReadMap(aFile);
baseJsonMap = Utils.mergeMaps(baseJsonMap, inputMap, taskModel.isMergeChildren());
for (String bFile : this.getMatchingFiles(aFile)) {
inputMap = Utils.getReadMap(bFile);
baseJsonMap = Utils.mergeMaps(baseJsonMap, inputMap, taskModel.isMergeChildren());
}
}

try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(targetPath))) {
Expand All @@ -64,4 +70,19 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

}

private String[] getMatchingFiles(String aFile) {
File file = new File(aFile).getParentFile();
if (file != null && file.exists()) {
FileFilter fileFilter = new WildcardFileFilter(new File(aFile).getName());
File[] files = Objects.requireNonNull(file.listFiles(fileFilter));
String[] result = new String[files.length];
for (int i = 0; i < files.length; i++) {
result[i] = files[i].getPath();
}
return result;
} else {
return new String[]{ aFile };
}
}
}