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.
我正在编写我的第一个Maven Mojo,其中我想要一个文件集并处理它所引用的所有文件。
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.
所以我想要的是“FileSetTransformer.toFileList”的真正代码,在我看来,这是一个非常普遍的事情,但我似乎无法找到如何做到这一点。
2 个解决方案
#1
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.
请参阅Maven FileSet的javadoc并使用getDirectory()和getIncludes()方法。这是一个现有的maven mojo的例子,它做了类似的事情。
#2
Thanks emeraldjava, that gives me enough to work out the answer to my question.
谢谢emeraldjava,这足以让我找到我的问题的答案。
plexus-utils has a utility class called FileUtils, you can add a dependency on it thus:
plexus-utils有一个名为FileUtils的实用程序类,你可以在它上面添加一个依赖项:
<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:
一旦有了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();
}
}
#1
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.
请参阅Maven FileSet的javadoc并使用getDirectory()和getIncludes()方法。这是一个现有的maven mojo的例子,它做了类似的事情。
#2
Thanks emeraldjava, that gives me enough to work out the answer to my question.
谢谢emeraldjava,这足以让我找到我的问题的答案。
plexus-utils has a utility class called FileUtils, you can add a dependency on it thus:
plexus-utils有一个名为FileUtils的实用程序类,你可以在它上面添加一个依赖项:
<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:
一旦有了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();
}
}