[Groovy] Class FileTreeBuilder
- groovy.util.FileTreeBuilder
@CompileStatic class FileTreeBuilder extends Object
A builder dedicated at generating a file directory structure from a specification. For example, imagine that you want to create the following tree:
src/ |--- main | |--- groovy | |--- Foo.groovy |--- test |--- groovy |--- FooTest.groovy
Then you can create the structure using:
def tree = new FileTreeBuilder()
tree.dir('src') {
dir('main') {
dir('groovy') {
file('Foo.groovy', 'println "Hello"')
}
}
dir('test') {
dir('groovy') {
file('FooTest.groovy', 'class FooTest extends GroovyTestCase {}')
}
}
}
or with this shorthand syntax:
def tree = new FileTreeBuilder()
tree.src {
main {
groovy {
'Foo.groovy'('println "Hello"')
}
}
test {
groovy {
'FooTest.groovy'('class FooTest extends GroovyTestCase {}')
}
}
}
- Since:
- 2.4.2
Properties Summary
Type | Name and description |
---|---|
File |
baseDir |
Constructor Summary
Constructor and description |
---|
FileTreeBuilder
(File baseDir) |
Methods Summary
Type Params | Return Type | Name and description |
---|---|---|
File |
call(Closure spec) | |
File |
dir(String name) Creates a new empty directory | |
File |
dir(String name, Closure cl) Creates a new directory and allows to specify a subdirectory structure using the closure as a specification | |
File |
file(String name, CharSequence contents) Creates a file with the specified name and the text contents using the system default encoding. | |
File |
file(String name, byte[] contents) Creates a file with the specified name and the specified binary contents | |
File |
file(String name, File source) Creates a file with the specified name and the contents from the source file (copy). | |
File |
file(String name, Closure spec) Creates a new file in the current directory, whose contents is going to be generated in the closure. | |
Object |
methodMissing(String name, Object args) |
Inherited Methods Summary
Methods inherited from class | Name |
---|---|
class Object | wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
Property Detail
File baseDir
Constructor Detail
FileTreeBuilder(File baseDir)
Method Detail
File call(@DelegatesTo(value: FileTreeBuilder, strategy: Closure.DELEGATE_FIRST) Closure spec)
File dir(String name)
Creates a new empty directory
- Parameters:
-
name
- the name of the directory to create
- Returns:
- the created directory
File dir(String name, @DelegatesTo(value: FileTreeBuilder, strategy: Closure.DELEGATE_FIRST) Closure cl)
Creates a new directory and allows to specify a subdirectory structure using the closure as a specification
- Parameters:
-
name
- name of the directory to be created -
cl
- specification of the subdirectory structure
- Returns:
- the created directory
File file(String name, CharSequence contents)
Creates a file with the specified name and the text contents using the system default encoding.
- Parameters:
-
name
- name of the file to be created -
contents
- the contents of the file, written using the system default encoding
- Returns:
- the file being created
File file(String name, byte[] contents)
Creates a file with the specified name and the specified binary contents
- Parameters:
-
name
- name of the file to be created -
contents
- the contents of the file
- Returns:
- the file being created
File file(String name, File source)
Creates a file with the specified name and the contents from the source file (copy).
- Parameters:
-
name
- name of the file to be created -
contents
- the contents of the file
- Returns:
- the file being created
File file(String name, @DelegatesTo(value: File, strategy: Closure.DELEGATE_FIRST) Closure spec)
Creates a new file in the current directory, whose contents is going to be generated in the closure. The delegate of the closure is the file being created.
- Parameters:
-
name
- name of the file to create -
spec
- closure for generating the file contents
- Returns:
- the created file
@SuppressWarnings(value: Instanceof) Object methodMissing(String name, Object args)
© 2003-2020 The Apache Software Foundation
Licensed under the Apache license.
https://docs.groovy-lang.org/3.0.7/html/gapi/groovy/util/FileTreeBuilder.html