Reputation: 44881
I'm writing my first Maven Mojo, in it I'm wanting to take a file set and process all the files it refers to.
In pseudo code what I'd like to do...
void myCode(org.apache.maven.model.FileSet fileSet) {
List<java.io.File> files = FileSetTransformer.toFileList(fileSet);
for (File f : files) {
doSomething(f);
}
}
So what I'm wanting is the real code for "FileSetTransformer.toFileList", it seems to me like a very common thing to want to do but I can't seem to find out how to do it.
Upvotes: 3
Views: 2472
Reputation: 11212
See the javadoc for Maven FileSet and use the getDirectory() and getIncludes() methods. This is an example of an existing maven mojo that does something simular.
Upvotes: 2
Reputation: 44881
Thanks emeraldjava, that gives me enough to work out the answer to my question.
plexus-utils has a utility class called FileUtils, you can add a dependency on it thus:
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.1</version>
</dependency>
Once you have FileUtils you can implement the FileSetTransformer thus:
public final class FileSetTransformer {
private FileSetTransformer () {
}
public static List<File> toFileList(FileSet fileSet) {
File directory = new File(fileSet.getDirectory());
String includes = toString(fileSet.getIncludes());
String excludes = toString(fileSet.getExcludes());
return FileUtils.getFiles(directory, includes, excludes);
}
private static String toString(List<String> strings) {
StringBuilder sb = new StringBuilder();
for (String string : strings) {
if (sb.length() > 0)
sb.append(", ");
sb.append(string);
}
return sb.toString();
}
}
Upvotes: 3