Chapter 8. Introduction to multi-project builds 多工程构建介绍
Only the smallest of projects has a single build file and source tree, unless it happens to be a massive, monolithic application. It’s often much easier to digest and understand a project that has been split into smaller, inter-dependent modules. The word “inter-dependent” is important, though, and is why you typically want to link the modules together through a single build.
Gradle supports this scenario through multi-project builds.
8.1. Structure of a multi-project build
Such builds come in all shapes and sizes, but they do have some common characteristics:
//gradle具有的一些通用的特性
A
settings.gradlefile in the root ormasterdirectory of the project //在项目的根目录下有一个settings.gradle文件A
build.gradlefile in the root ormasterdirectory //在根目录下有一个build.gradle文件Child directories that have their own
*.gradlebuild files (some multi-project builds may omit child project build scripts) //每个子目录有他们自己的*.gradle文件
The settings.gradle file tells Gradle how the project and subprojects are structured. Fortunately, you don’t have to read this file simply to learn what the project structure is as you can run the command gradle projects. Here's the output from using that command on the Java multiproject build in the Gradle samples:
//settings.gradle文件告诉gradle工程和子工程是如何被组织的。幸运的是,你不需要读这个文件来了解项目结构,你可以使用gradle projects命令。例子如下:
Example 8.1. Listing the projects in a build
Output of gradle -q projects
> gradle -q projects ------------------------------------------------------------
Root project
------------------------------------------------------------ Root project 'multiproject'
+--- Project ':api'
+--- Project ':services'
| +--- Project ':services:shared'
| \--- Project ':services:webservice'
\--- Project ':shared' To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :api:tasks
This tells you that multiproject has three immediate child projects: api, services and shared. The services project then has its own children, shared and webservice. These map to the directory structure, so it’s easy to find them. For example, you can find webservice in <root>/services/webservice.
Each project will usually have its own build file, but that's not necessarily the case. In the above example, the services project is just a container or grouping of other subprojects. There is no build file in the corresponding directory. However, multiproject does have one for the root project.
//每一个项目通常会有一个自己的build文件,但是这不是必须要有的。在上面的例子中,services工程就是一个其他子工程的容器,因此在其对应的目录下没有build文件。然后多工程必须在根目录下有一个build文件
The root build.gradle is often used to share common configuration between the child projects, for example by applying the same sets of plugins and dependencies to all the child projects. It can also be used to configure individual subprojects when it is preferable to have all the configuration in one place. This means you should always check the root build file when discovering how a particular subproject is being configured.
//根目录下的build.gradle文件通常被用来共享子工程的通用配置,例如给所有的子工程应用相同的插件设置。它也可以用来配置子工程的独立配置,这意味着当研究某一个特定子工程是如何被配置的问题时,根目录的build.gradle文件应该是需要查看的。
Another thing to bear in mind is that the build files might not be called build.gradle. Many projects will name the build files after the subproject names, such asapi.gradle and services.gradle from the previous example. Such an approach helps a lot in IDEs because it’s tough to work out which build.gradle file out of twenty possibilities is the one you want to open. This little piece of magic is handled by the settings.gradle file, but as a build user you don’t need to know the details of how it’s done. Just have a look through the child project directories to find the files with the .gradle suffix.
//另一个事需要了解的是,build文件可能不叫build.gradle.很多工程会自己命名子工程下的build文件,例如上面例子中的api.gradle 和 services.gradle.这种方法帮了IDE很大的忙,因为让ide从20多重可能的文件中找到你想要打开的那个build文件是艰难的。这个处理逻辑在setting.gradle文件中。
Once you know what subprojects are available, the key question for a build user is how to execute the tasks within the project.
8.2. Executing a multi-project build
From a user's perspective, multi-project builds are still collections of tasks you can run. The difference is that you may want to control which project's tasks get executed. You have two options here:
//从用户的角度看,多工程构建仍然是一堆你可以运行的任务的集合。不同的是,你可能想去控制哪个工程的任务来执行,这里你有两个选项:
Change to the directory corresponding to the subproject you’re interested in and just execute
gradle <task>as normal. //切换目录到需要构建的子工程目录下,然后像正常情况一样执行gradle <task>Use a qualified task name from any directory, although this is usually done from the root. For example:
gradle :services:webservice:buildwill build the webservicesubproject and any subprojects it depends on. //在任何目录下使用全称的任务名
The first approach is similar to the single-project use case, but Gradle works slightly differently in the case of a multi-project build. The command gradle test will execute the test task in any subprojects, relative to the current working directory, that have that task. So if you run the command from the root project directory, you’ll run test inapi, shared, services:shared and services:webservice. If you run the command from the services project directory, you’ll only execute the task in services:shared andservices:webservice.
//第一种方法和在只有一个工程的项目中使用方法是相似的,但是在多工程的项目中gradle的操作会有一些不同。命令gradle test将会执行所有子工程中的test任务,相对于当前的工作目录并且有test任务的都会执行。所以,如果你在根目录运行这个命令,你将会运行在api,shared,services:shared,services:webservice中的test任务,如果你在setvices工程目录中运行这个命令,你将只会执行在servics:shared和service:webservice中的test任务
For more control over what gets executed, use qualified names (the second approach mentioned). These are paths just like directory paths, but use ‘:’ instead of ‘/’ or ‘\’. If the path begins with a ‘:’, then the path is resolved relative to the root project. In other words, the leading ‘:’ represents the root project itself. All other colons are path separators.
//想要更高级的控制执行的内容,使用全称名字(上面提到的第二种办法)。有一些像目录路径一样的路径信息,但是使用冒号:而不是/,如果路径以:开头,那么这个路径就是相对根工程而言的,换句话说,开头的:代表根工程它自己。所有其他的冒号表示的是路径下划线。
This approach works for any task, so if you want to know what tasks are in a particular subproject, just use the tasks task, e.g. gradle :services:webservice:tasks .
//这个方法适用于所有的任务。如果你想知道在某一个特定子工程中有哪些task,执行tasks任务。
Regardless of which technique you use to execute tasks, Gradle will take care of building any subprojects that the target depends on. You don’t have to worry about the inter-project dependencies yourself. If you’re interested in how this is configured, you can read about writing multi-project builds later in the user guide.
There’s one last thing to note. When you’re using the Gradle wrapper, the first approach doesn’t work well because you have to specify the path to the wrapper script if you’re not in the project root. For example, if you’re in the webservice subproject directory, you would have to run ../../gradlew build.
//使用第一种方法时候如果想要执行gradlew命令,需要切到根工程目录下:../../gradlew build
That’s all you really need to know about multi-project builds as a build user. You can now identify whether a build is a multi-project one and you can discover its structure. And finally, you can execute tasks within specific subprojects.
Chapter 8. Introduction to multi-project builds 多工程构建介绍的更多相关文章
- PRML Chapter 1. Introduction
PRML Chapter 1. Introduction 为了防止忘记,要把每章的重要内容都记下来,从第一章开始 2012@3@28 今天又回去稍微翻了一下第一章内容,发现第一次看的时候没有看透,每次 ...
- JVM Specification 9th Edition (2) Chapter 1. Introduction
Chapter 1. Introduction 翻译太累了,我就这样的看英文吧. 内容列表 1.1. A Bit of History 1.2. The Java Virtual Machine 1. ...
- ReSharper “Cannot resolve symbol” even when project builds
ReSharper “Cannot resolve symbol” even when project builds This worked for me (VS2012u4, R# 7.1.3) ...
- TIJ——Chapter One:Introduction to Objects
///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...
- Chapter 1. Introduction gradle介绍
We would like to introduce Gradle to you, a build system that we think is a quantum leap for build ...
- MongoDB:The Definitive Guide CHAPTER 1 Introduction
MongoDB is a powerful, flexible, and scalable data store. It combines the ability to scale out with ...
- Chapter 3 Introduction to Objects and Input/Output
与声明一个primitive variable不同,声明一个对象的时候,并不创建用来存储一个对象的内存空间,而是创建了一个存储该对象所在内存空间的地址. 在java里,new是一个操作符,它让系统分配 ...
- Logback手冊 Chapter 1: Introduction
翻译不周,多多包括 ---------------------------------------------------------------------------------------切割线 ...
- translation of 《deep learning》 Chapter 1 Introduction
原文: http://www.deeplearningbook.org/contents/intro.html Inventors have long dreamed of creating mach ...
随机推荐
- linux文件系统结构和权限
linux文件系统的目录结构 熟话说的好,好记性不如烂笔头,虽然没用笔,但动动手指还是可以的.下面的目录结构都是摘抄过来的,动动手指来加深下印象吧,还能练习下打字速度,哈哈,多好啊. ...突然又改变 ...
- C语言基础学习运算符-赋值运算符
简单赋值 在C语言里,运算符=并不表示相等,而是一个赋值运算符.这意味着,符号=的左边该是一个可修改的变量名,而右边是赋给该变量的值. 如下程序语句: i = i+; 在数学上,该等式无法成立.而作为 ...
- iOS文件系统的管理-b
NSFileManager 判断一个给定路劲是否为文件夹 [self.fileManagerfileExistsAtPath:isDirectory:]; 用于执行一般的文件系统操作 (reading ...
- 转:嵌入式linux启动时运行的inittab文件
嵌入式系统下的linux启动配置文件,不同与普通的PC linux启动配置,启动相关文件与文件的内容也要少得多.嵌入式系统下的linux启动过程一般是: 1 在bootloader中制定各种要 ...
- InstallShield FEQ
Q: 如何替换setup.exe的图标? A: 这不是一个推荐的操作,因为可能会引起不可预见的错误,而且IS没有开放这个接口.如果你坚持要这么做,可以使用第三方软件比如ExeScope来进行图标替换. ...
- 用extern关键字使程序更加清晰
一.基础研究 之前基于tcc.tlink实现一个新的编译连接工具cc.exe,用到的文件有cs.lib.c0s.obj.main.obj,其中main.obj是我们自己加入的文件,它可以实现开始显示彩 ...
- 透明与Z序示例
import QtQuick 2.4 import QtQuick.Controls 1.4 import QtQuick.Controls.Styles 1.4 import QtQuick.Win ...
- FLASK安装--兼收EZ_INSTALL及PIP
参考URL: http://www.cnblogs.com/haython/p/3970426.html http://www.pythondoc.com/flask/installation.htm ...
- 揭开.NET消息循环的神秘面纱(GetMessage()无法取得任何消息,就会进入Idle(空闲)状态,进入睡眠状态(而不是Busy Waiting)。当消息队列不再为空的时候,程序会自动醒过来)
揭开.NET消息循环的神秘面纱(-) http://hi.baidu.com/sakiwer/item/f17dc33274a04df2a9842866 曾经在Win32平台下奋战的程序员们想必记得, ...
- java代码调用rtx发送提醒消息
http://www.cnblogs.com/qstar/archive/2012/02/03/Astar.html 借用一下!好东西