2

利用したいJavaライブラリがあるため、JavaでGradleプラグインを構築しています。プラグインの一部として、ファイルのフォルダーを一覧表示して処理する必要があります。Gradleビルドファイルでこれを行う方法の多くの例を見つけることができます:

  FileTree tree = fileTree(dir: stagingDirName)
  tree.include '**/*.md'
  tree.each {File file ->
    compileThis(file)
  }

しかし、Gradle の Java API を使用して Java でこれを行うにはどうすればよいでしょうか?

基礎となる FileTree Java クラスには、非常に柔軟な入力パラメーターがあり、非常に強力ですが、どの種類の入力が実際に機能するかを理解するのは非常に困難です。

4

2 に答える 2

1

Javaベースのgradleタスクでこれを行う方法は次のとおりです。

public class MyPluginTask extends DefaultTask {

    @TaskAction
    public void action() throws Exception {

        // sourceDir can be a string or a File
        File sourceDir = new File(getProject().getProjectDir(), "src/main/html");
        // or:
        //String sourceDir = "src/main/html";

        ConfigurableFileTree cft = getProject().fileTree(sourceDir);
        cft.include("**/*.html");

        // Make sure we have some input. If not, throw an exception.
        if (cft.isEmpty()) {
            // Nothing to process. Input settings are probably bad. Warn user.
            throw new Exception("Error: No processable files found in sourceDir: " +
                    sourceDir.getPath() );
        }

        Iterator<File> it = cft.iterator();
        while (it.hasNext()){
            File f = it.next();
            System.out.println("File: "+f.getPath()"
        }
    }

}
于 2013-08-26T19:02:13.723 に答える