Gradle指定一組輸入文件

2020-07-24 16:01 更新

在 Gradle 中有一些對(duì)象的某些屬性可以接收一組輸入文件.例如,JavaComplile 任務(wù)有一個(gè) source 屬性,它定義了編譯的源文件,你可以設(shè)置這個(gè)屬性的值,只要 files() 方法支持. 這意味著你可以使用 File ,String , collection , FileCollection 甚至是使用一個(gè)閉合去設(shè)置屬性的值.

例 15.8 指定文件

build.gradle

//使用一個(gè) File 對(duì)象設(shè)置源目錄
compile {
    source = file('src/main/java')
}

//使用一個(gè)字符路徑設(shè)置源目錄
compile {
    source = 'src/main/java'
}

// 使用一個(gè)集合設(shè)置多個(gè)源目錄
compile {
    source = ['src/main/java', '../shared/java']
}

// 使用 FileCollection 或者 FileTree 設(shè)置源目錄
compile {
    source = fileTree(dir: 'src/main/java').matching { include 'org/gradle/api/**' }
}

// 使用一個(gè)閉合設(shè)置源目錄
compile {
    source = {
        // Use the contents of each zip file in the src dir
        file('src').listFiles().findAll {it.name.endsWith('.zip')}.collect { zipTree(it) }
    }
}

Usually, there is a method with the same name as the property, which appends to the set of files. Again, this method accepts any of the types supported by the files() method.

通常情況下,會(huì)有一個(gè)方法名和屬性名相同的方法能夠附加一組文件,這個(gè)方法接收 files() 方法支持的任何類型的值.

例 15.9 指定文件

build.gradle

compile {
    // 使用字符路徑添加源目錄
    source 'src/main/java', 'src/main/groovy'

    // 使用 File 對(duì)象添加源目錄
    source file('../shared/java')

    // 使用閉合添加源目錄
    source { file('src/test/').listFiles() }
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)