第4章 使用Gradle命令行
目录
4.1、执行多个任务
task compile { doLast { println 'compiling source' } } task compileTest(dependsOn: compile) { doLast { println 'compiling unit tests' } } task test(dependsOn: [compile, compileTest]) { doLast { println 'running unit tests' } } task dist(dependsOn: [compile, test]) { doLast { println 'building the distribution' } }gradle dist test 命令输出
> gradle dist test :compile compiling source :compileTest compiling unit tests :test running unit tests :dist building the distribution BUILD SUCCESSFUL in 0s 4 actionable tasks: 4 executed每个任务只执行一次,所以 “gradle test test” 与 “gradle test ”是一样的。
4.2、排除任务
> gradle dist -x test :compile compiling source :dist building the distribution BUILD SUCCESSFUL in 0s 2 actionable tasks: 2 executed
4.3、发生故障时继续构建
4.4、任务名缩写
> gradle di :compile compiling source :compileTest compiling unit tests :test running unit tests :dist building the distribution BUILD SUCCESSFUL in 0s 4 actionable tasks: 4 executed
> gradle cT :compile compiling source :compileTest compiling unit tests BUILD SUCCESSFUL in 0s 2 actionable tasks: 2 executed您还可以使用这些缩写与-x命令行选项。
4.5、选择要执行的构建
task hello { doLast { println "using build file '$buildFile.name' in '$buildFile.parentFile.name'." } }gradle -q -b subdir/myproject.gradle hello 命令输出:
> gradle -q -b subdir/myproject.gradle hello using build file 'myproject.gradle' in 'subdir'.
> gradle -q -p subdir hello using build file 'build.gradle' in 'subdir'.
4.6、强制执行任务
> gradle doIt :doIt UP-TO-DATEgradle --rerun-tasks doIt 命令输出:
> gradle --rerun-tasks doIt :doIt
4.7、获取有关您的构建的信息
4.7.1。列出再有项目
> gradle -q projects ------------------------------------------------------------ Root project ------------------------------------------------------------ Root project 'projectReports' +--- Project ':api' - The shared API for the application \--- Project ':webapp' - The Web application implementation To see a list of the tasks of a project, run gradle <project-path>:tasks For example, try running gradle :api:tasks
description = 'The shared API for the application'
4.7.2。列出任务
> gradle -q tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Default tasks: dists Build tasks ----------- clean - Deletes the build directory (build) dists - Builds the distribution libs - Builds the JAR Build Setup tasks ----------------- init - Initializes a new Gradle build. wrapper - Generates Gradle wrapper files. Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'. components - Displays the components produced by root project 'projectReports'. [incubating] dependencies - Displays all dependencies declared in root project 'projectReports'. dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'. dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating] help - Displays a help message. model - Displays the configuration model of root project 'projectReports'. [incubating] projects - Displays the sub-projects of root project 'projectReports'. properties - Displays the properties of root project 'projectReports'. tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects). To see all tasks and more detail, run gradle tasks --all To see more detail about a task, run gradle help --task <task>
dists { description = 'Builds the distribution' group = 'build' }
gradle -q tasks --all
命令输出
> gradle -q tasks --all ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Default tasks: dists Build tasks ----------- clean - Deletes the build directory (build) api:clean - Deletes the build directory (build) webapp:clean - Deletes the build directory (build) dists - Builds the distribution api:libs - Builds the JAR webapp:libs - Builds the JAR Build Setup tasks ----------------- init - Initializes a new Gradle build. wrapper - Generates Gradle wrapper files. Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'. api:buildEnvironment - Displays all buildscript dependencies declared in project ':api'. webapp:buildEnvironment - Displays all buildscript dependencies declared in project ':webapp'. components - Displays the components produced by root project 'projectReports'. [incubating] api:components - Displays the components produced by project ':api'. [incubating] webapp:components - Displays the components produced by project ':webapp'. [incubating] dependencies - Displays all dependencies declared in root project 'projectReports'. api:dependencies - Displays all dependencies declared in project ':api'. webapp:dependencies - Displays all dependencies declared in project ':webapp'. dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'. api:dependencyInsight - Displays the insight into a specific dependency in project ':api'. webapp:dependencyInsight - Displays the insight into a specific dependency in project ':webapp'. dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating] api:dependentComponents - Displays the dependent components of components in project ':api'. [incubating] webapp:dependentComponents - Displays the dependent components of components in project ':webapp'. [incubating] help - Displays a help message. api:help - Displays a help message. webapp:help - Displays a help message. model - Displays the configuration model of root project 'projectReports'. [incubating] api:model - Displays the configuration model of project ':api'. [incubating] webapp:model - Displays the configuration model of project ':webapp'. [incubating] projects - Displays the sub-projects of root project 'projectReports'. api:projects - Displays the sub-projects of project ':api'. webapp:projects - Displays the sub-projects of project ':webapp'. properties - Displays the properties of root project 'projectReports'. api:properties - Displays the properties of project ':api'. webapp:properties - Displays the properties of project ':webapp'. tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects). api:tasks - Displays the tasks runnable from project ':api'. webapp:tasks - Displays the tasks runnable from project ':webapp'. Other tasks ----------- api:compile - Compiles the source files webapp:compile - Compiles the source files docs - Builds the documentation
4.7.3。显示任务使用细节
gradle help --task someTask
为您提供有关特定任务的详细信息或与多项目构建中的给定任务名称匹配的多个任务。
gradle -q help --task libs 命令输出
> gradle -q help --task libs Detailed task information for libs Paths :api:libs :webapp:libs Type Task (org.gradle.api.Task) Description Builds the JAR Group build该信息包括完整的任务路径,任务类型,可能的命令行选项以及给定任务的描述。
4.7.4。列出项目依赖关系
gradle dependencies 为
您列出所选项目的依赖关系,按配置细分。
gradle -q dependencies api:dependencies webapp:dependencies 命令输出:
> gradle -q dependencies api:dependencies webapp:dependencies ------------------------------------------------------------ Root project ------------------------------------------------------------ No configurations ------------------------------------------------------------ Project :api - The shared API for the application ------------------------------------------------------------ compile \--- org.codehaus.groovy:groovy-all:2.4.10 testCompile \--- junit:junit:4.12 \--- org.hamcrest:hamcrest-core:1.3 ------------------------------------------------------------ Project :webapp - The Web application implementation ------------------------------------------------------------ compile +--- project :api | \--- org.codehaus.groovy:groovy-all:2.4.10 \--- commons-io:commons-io:1.2 testCompile No dependencies
gradle -q api:dependencies --configuration testCompile 命令输出:
> gradle -q api:dependencies --configuration testCompile ------------------------------------------------------------ Project :api - The shared API for the application ------------------------------------------------------------ testCompile \--- junit:junit:4.12 \--- org.hamcrest:hamcrest-core:1.3