Gradle使用相对路径
Chapter 18. Working With Files
第18章 使用文件
Most builds work with files. Gradle adds some concepts and APIs to help you achieve this.
大部分构建工作都会用到问文件。Gradle添加一些内容和API帮助你管理他们。
18.1. Locating files 定位文件
You can locate a file relative to the project directory using the Project.file() method.
你能够定位文件使用相对于工程目录的路径,使用Project.file()方法。
Example 例子 18.1. Locating files
build.gradle
// Using a relative path 相对路径
File configFile = file('src/config.xml')
// Using an absolute path 绝对路径
configFile = file(configFile.absolutePath)
// Using a File object with a relative path 用相对路径创建一个文件对象
configFile = file(new File('src/config.xml'))
You can pass any object to the file() method, and it will attempt to convert the value to an absolute File object. Usually, you would pass it a String or File instance. If this path is an absolute path, it is used to construct a File instance. Otherwise, a File instance is constructed by prepending the project directory path to the supplied path. The file() method also understands URLs, such as file:/some/path.xml.
你能够传递任何的对象给file()方法,它将尝试转换为一个绝对的对象。通常,你能传递一个路径字符串或是一个文件实例。如果这个路径是一个绝对路径,他被用来构建一个文件对象。否则,工程目录将会预处理为提供的路径,然后构建文件实例。file()方法也可以是URL,比如文件:/some/path.xml
Using this method is a useful way to convert some user provided value into an absolute File. It is preferable to using new File(somePath), as file() always evaluates the supplied path relative to the project directory, which is fixed, rather than the current working directory, which can change depending on how the user runs Gradle.
这个方法对转换用户提供的值到一个绝对的文件通常是非常有用的。使用new FIle(somePath)比使用file()更好,file()总是把提供的路径转成相对于工程的路径,这是固定的,而不是当前的工作目录,他依赖于用户运行Gradle的目录。
可以使用相对路径
// Use a collection to specify multiple source directories
compile {
source = ['src/main/java', '../shared/java']
}
网友评论