How do you turn a fileset into a list of files in Mojo

I am writing my first Maven Mojo, in this I want to take a file and process all the files that it refers to.

In pseudo code, what I would 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 want is the real code for "FileSetTransformer.toFileList", it seems to me that this is a very ordinary thing, but I cannot figure out how to do it.

+3
source share
2 answers

See javadoc for Maven FileSet and use getDirectory () and getIncludes () methods. This is an example of an existing maven mojo that does something simulative.

+2
source

Thanks emeraldjava, which gives me enough to work out the answer to my question.

plexus-utils FileUtils, :

<dependency>
  <groupId>org.codehaus.plexus</groupId>
  <artifactId>plexus-utils</artifactId>
  <version>1.1</version>
</dependency>

, FileUtils, FileSetTransformer, :

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();
        }
}
+2

Source: https://habr.com/ru/post/1702171/


All Articles