Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you turn a FileSet into a list of Files in a Mojo

Tags:

java

maven-2

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.

like image 564
Tom Avatar asked Nov 24 '25 06:11

Tom


1 Answers

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();
        }
}
like image 96
Tom Avatar answered Nov 26 '25 20:11

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!