±¾Õ½éÉÜÁË
Gradle ÃüÁîÐеĻù±¾ÖªÊ¶¡£ÕýÈçÔÚÇ°ÃæµÄÕ½ÚÀïÄãËù¼ûµ½µÄ£¬ µ÷Óà gradle ÃüÁîÀ´Ö´Ðй¹½¨¡£
Ö´Ðжà task
ͬ¸ö¹¹½¨¿ÉÒÔÖ´Ðжà¸ö task ,ͨ¹ýÔÙÃüÁîÐÐ Áгöÿ¸ö task¡£¾ÙÀý£¬ÃüÁî
gradle compile test ½«»áÖ´ÐÐ compile ºÍ test Á½¸ö task¡£ Gradle
½«»á°´Ë³ÐòÖ´ÐÐ ÃüÁîÐÐÿ¸öÁгöµÄ task£¬²¢ÇÒÖ´ÐÐÿ¸ö task µÄÒÀÀµ¡£Ã¿¸öÈÎÎñ½öÖ´ÐÐÒ»´Î£¬²»¹ÜËüÊÇÈçºÎ±»°üº¬ÔÚ¹¹½¨ÖУºÎÞÂÛÊÇÔÚÃüÁîÐÐÖÐÖ¸¶¨£¬»ò×÷ΪÁíÒ»¸ö
task µÄÒÀÀµ£¬»òÁ½Õß¶¼ÊÇ¡£ÈÃÎÒÃÇ¿´Ò»¸öÀý×Ó¡£
ÏÂÃæ¶¨ÒåÁË 4 ¸ö task¡£ dist ºÍ test ¶¼ÒÀÀµÓÚ compile ¡£Ö´ÐÐ gradle
dist test £¬compile ½«»á½ö½ö±»Ö´ÐÐÒ»´Î¡£
Figure 11.1. Task dependencies

build.gradle
task compile << {
println 'compiling source'
}
task compileTest(dependsOn: compile) <<
{
println 'compiling unit tests'
}
task test(dependsOn: [compile, compileTest])
<< {
println 'running unit tests'
}
task dist(dependsOn: [compile, test]) <<
{
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
Total time: 1 secs |
ÿ¸ö task Ö»Ö´ÐÐÒ»´Î£¬ËùÒÔ gradle test test ¸ú gradle test Ö´Ðнá¹ûÒ»Ñù¡£
Åųý task
¿ÉÒÔͨ¹ý -x ÃüÁîÐÐÀ´Åųý task ±»Ö´ÐС£
·¢Éú¹ÊÕÏʱ¼ÌÐø¹¹½¨
ĬÈÏÇé¿öÏ£¬Ö»ÒªÈκΠtask ʧ°Ü£¬Gradle ½«ÖÐÖ¹Ö´ÐС£ÕâʹµÃ¹¹½¨¸ü¿ìµØÍê³É£¬µ«Òþ²ØÁËÆäËû¿ÉÄÜ·¢ÉúµÄ¹ÊÕÏ¡£ÎªÁË·¢ÏÖÔÚÒ»¸öµ¥Ò»µÄ¹¹½¨Öжà¸ö¿ÉÄÜ·¢Éú¹ÊÕϵĵط½£¬Äã¿ÉÒÔʹÓÃ
--continue Ñ¡Ïî¡£
ͨ¹ýÖ´ÐÐ --continue £¬Gralde »áÖ´ÐÐÿһ¸ö task £¬µ±ÄǸö task ËùÓеÄÒÀÀµ¶¼ÎÞ¹ÊÕϵÄÖ´ÐÐÍê³É,
¶ø²»ÊÇÒ»µ©³öÏÖ´íÎó¾Í»áÖжÏÖ´ÐС£ËùÓйÊÕÏÐÅÏ¢¶¼»áÔÚ×îºó±¨¸æ³öÀ´¡£
Ò»µ©Ä³¸ö task Ö´ÐÐʧ°Ü,ÄÇôËùÓÐÒÀÀµÓڸà task µÄºóÃæµÄ task ¶¼²»»á±»Ö´ÐУ¬ÒòΪÕâÑù×ö²»°²È«¡£ÀýÈ磬ÔÚ²âÊÔʱ£¬µ±±àÒë´úÂëʧ°ÜÔò²âÊÔ²»»áÖ´ÐС£ÒòΪ
²âÊÔ task ½«È¡¾öÓÚ ±àÒë task£¨²»¹ÜÊÇÖ±½Ó»ò¼ä½Ó£©¡£
ÈÎÎñÃû³ÆËõд
µ±ÄãÊÔͼִÐÐij¸ö task µÄʱºò,ÎÞÐèÊäÈë task µÄÈ«Ãû.Ö»ÐèÌṩ×ã¹»µÄ¿ÉÒÔÎ¨Ò»Çø·Ö³ö¸Ã task
µÄ×Ö·û¼´¿É¡£ÀýÈç,ÉÏÃæµÄÀý×ÓÄãÒ²¿ÉÒÔÕâôд, Óà gradle di À´Ö±½Óµ÷Óà dist ¡£
Example 11.3. Abbreviated task name
Ö´ÐÐ gradle dist -x test Êä³ö
> gradle dist -x test :compile compiling source :dist building the distribution
BUILD SUCCESSFUL
Total time: 1 secs |
ͬʱҲ¿ÉÒÔÓ¦ÓÃÔÚÍÕ·åµÄ task Ãû³Æ¡£È磬ִÐÐ compileTest ʱ£¬ÔËÐÐ gradle compTest
»òÕß gradle cT ¶¼¿ÉÒÔ¡£
Example 11.4. Abbreviated camel case task name
Ö´ÐÐ gradle cT
> gradle cT :compile compiling source :compileTest compiling unit tests
BUILD SUCCESSFUL
Total time: 1 secs |
ͬʱ£¬Ò²¿ÉÒÔÓ¦ÓÃÔÚ -x ÃüÁîÑ¡Ïî¡£
Ñ¡ÔñÒªÖ´ÐеĹ¹½¨
µ÷Óà gradle ÃüÁîʱ,ĬÈÏÇé¿öÏÂ×ÜÊÇ»áÔÚµ±Ç°Ä¿Â¼ÏÂѰÕÒ¹¹½¨Îļþ£¨ÒëÕß×¢£ºÊ×ÏÈ»áѰÕÒµ±Ç°Ä¿Â¼ÏµÄ
build.gradle Îļþ,ÒÔ¼°¸ù¾Ýsettings.gradle ÖеÄÅäÖÃѰÕÒ×ÓÏîÄ¿µÄ build.gradle
£©¡£ ¿ÉÒÔʹÓà -b ²ÎÊýÑ¡ÔñÆäËûµÄ¹¹½¨Îļþ,²¢ÇÒµ±ÄãʹÓô˲ÎÊýʱ settings.gradle ½«²»»á±»Ê¹ÓÃ,¿´ÏÂÃæµÄÀý×Ó:
Example 11.5. Selecting the project using a build file
subdir/myproject.gradle
task hello << {
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'. |
»òÕߣ¬Äú¿ÉÒÔʹÓà -p Ñ¡ÏîÀ´Ö¸¶¨ÒªÊ¹ÓõÄÏîĿĿ¼¡£¶à project µÄ¹¹½¨Ê±Ó¦Ê¹Óà -p Ñ¡ÏîÀ´´úÌæ
-b Ñ¡Ïî¡£
Example 11.6. Selecting the project using project directory
Ö´ÐÐ gradle -q -p subdir hello Êä³ö
> gradle -q -p subdir hello using build file 'build.gradle' in 'subdir'. |
»ñÈ¡¹¹½¨ÐÅÏ¢
Gradle ÌṩÁËÐí¶àÄÚÖà task À´ÊÕ¼¯¹¹½¨ÐÅÏ¢¡£ÕâЩÄÚÖà task
¶ÔÓÚÁ˽âÒÀÀµ½á¹¹ÒÔ¼°½â¾öÎÊÌâ¶¼ÊǺÜÓаïÖúµÄ¡£
ÏîÄ¿Áбí
Ö´ÐÐ gradle projects »áΪÄãÁгöÑ¡ÖÐÏîÄ¿µÄ×ÓÏîÄ¿ÁÐ±í¡£ÈçÏÂÀý¡£
Example 11.7. Obtaining information about projects
Ö´ÐÐ gradle -q projects Êä³ö
> 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 ÊôÐÔÀ´Ö¸¶¨ÕâЩÃèÊöÐÅÏ¢¡£¡£
Example 11.8. Providing a description for a project
build.gradle
description = 'The shared API for the application'
|
ÈÎÎñÁбí
Ö´ÐÐ gradle tasks »áÁгöÏîÄ¿ÖÐËùÓÐ task¡£Õâ·Ý±¨¸æÏÔʾÏîÄ¿ÖÐËùÓеÄĬÈÏ task ÒÔ¼°Ã¿¸ö
task µÄÃèÊö¡£ÈçÏÂ
Example 11.9. Obtaining information about tasks
Ö´ÐÐ gradle -q tasks Êä³ö
> 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. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Help tasks
----------
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'.
help - Displays a help message.
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 with --all. |
ĬÈÏÇé¿öÏÂ,ÕâÖ»»áÏÔʾÄÇЩ±»·Ö×éµÄ task.Äã¿ÉÒÔͨ¹ýΪ task ÉèÖÃgroup ÊôÐÔºÍ description
À´°ÑÕâЩÐÅϢչʾµ½±¨¸æÖÐ
Example 11.10. Changing the content of the task report
build.gradle
dists {
description = 'Builds the distribution'
group = 'build'
} |
µ±È»ÄãÒ²¿ÉÒÔÓà --all ²ÎÊýÀ´ÊÕ¼¯¸ü¶à task ÐÅÏ¢¡£Õⱨ¸æÁгöÏîÄ¿ÖÐËùÓб»Ö÷ task µÄ·Ö×éµÄ
task ÒÔ¼° task Ö®¼äµÄÒÀÀµ¹ØÏµ¡£ÏÂÃæÊÇʾÀý
Example 11.11. Obtaining more information about tasks
Ö´ÐÐ 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, webapp:libs]
docs - Builds the documentation
api:libs - Builds the JAR
api:compile - Compiles the source files
webapp:libs - Builds the JAR [api:libs]
webapp:compile - Compiles the source files
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Help tasks
----------
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'.
help - Displays a help message.
api:help - Displays a help message.
webapp:help - Displays a help message.
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'. |
ÏÔʾ task ʹÓÃϸ½Ú
Ö´ÐÐ gradle help --task someTask ¿ÉÒÔ»ñÈ¡µ½ task µÄÏêϸÐÅÏ¢£¬ »òÕß¶àÏîÄ¿¹¹½¨ÖÐÏàͬ
task Ãû³ÆµÄËùÓÐ task µÄÐÅÏ¢,ÈçÏÂ
Example 11.12. Obtaining detailed help for tasks
Ö´ÐÐ 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 |
ÕâЩ½á¹û°üº¬ÁËÍêÕûµÄ task µÄ·¾¶¡¢ÀàÐÍ¡¢¿ÉÄܵÄÃüÁîÐÐÑ¡ÏîÒÔ¼°ÃèÊöÐÅÏ¢µÈ.
ÀµÁбí
Ö´ÐÐ gradle dependencies »áÁгöÏîÄ¿µÄÒÀÀµÁбí,ËùÓÐÒÀÀµ»á¸ù¾ÝÈÎÎñÇø·Ö,ÒÔÊ÷Ðͽṹչʾ³öÀ´¡£ÈçÏÂ
Example 11.13. Obtaining information about 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.3.6
testCompile
\--- junit:junit:4.11
\--- org.hamcrest:hamcrest-core:1.3
------------------------------------------------------------
Project :webapp - The Web application implementation
------------------------------------------------------------
compile
+--- project :api
| \--- org.codehaus.groovy:groovy-all:2.3.6
\--- commons-io:commons-io:1.2
testCompile
No dependencies |
ÓÉÓÚÒÀÀµµÄ±¨¸æ¿ÉÒÔ±äµÃ½Ï´ó£¬¿ÉÒÔʹÓÃÌØ¶¨µÄÅäÖÃÀ´ÏÞÖÆµ½Ò»¸öÓÐÓõı¨¸æ¡£¿ÉÒÔͨ¹ý --configuration
¿ÉÑ¡²ÎÊýÀ´ÊµÏÖ¡£
Example 11.14. Filtering dependency report by configuration
Ö´ÐÐ gradle -q api:dependencies --configuration testCompile
Êä³öΪ
> gradle -q api:dependencies --configuration testCompile ------------------------------------------------------------ Project :api - The shared API for the application ------------------------------------------------------------
testCompile
\--- junit:junit:4.11
\--- org.hamcrest:hamcrest-core:1.3 |
²é¿´Ìض¨ÒÀÀµ
Ö´ÐÐ gradle dependencyInsight ¿ÉÒԲ鿴ָ¶¨µÄÒÀÀµÇé¿ö£¬ÈçÏÂ
Example 11.15. Getting the insight into a particular
dependency
Ö´ÐÐ gradle -q webapp:dependencyInsight --dependency
groovy --configuration compile Êä³ö
> gradle -q webapp:dependencyInsight --dependency groovy --configuration compile org.codehaus.groovy:groovy-all:2.3.6 \--- project :api \--- compile |
Õâ¶ÔÓÚ·Ö±æÒÀÀµ¡¢Á˽âÒÀÀµ¹ØÏµ¡¢Á˽âΪºÎÑ¡Ôñ´Ë°æ±¾×÷ΪÒÀÀµÊ®·ÖÓÐÓá£Á˽â¸ü¶àÇë²ÎÔÄ DependencyInsightReportTask
ÀàµÄ API
ÄÚ½¨µÄ dependencyInsight ÊÇ'Help' task ·Ö×éÖеÄÒ»¸ö¡£ÕâÏî task ÐèÒª½øÐÐÒÀÀµºÍÅäÖÃÎļþµÄÅäÖòſÉÒÔ¡£¸Ã±¨¸æÑ°ÕÒÄÇЩÓ붨ÒÀÀµ¹æ·¶Ö¸¶¨µÄÅäÖÃÆ¥ÅäµÄµÄÒÀÀµ¡£Èç¹ûÓ¦ÓÃÁË
Java Ïà¹ØµÄ²å¼þ£¬¸Ãdependencyinsight task ÊÇÔ¤ÏȾ¹ý 'compile' ÅäÖã¬ÒòΪËüͨ³£ÒÀÀµÎÒÃǸÐÐËȤµÄ±àÒë¡£ÄãÓ¦¸ÃÖ¸¶¨Äú¸ÐÐËȤµÄÒÀÀµ,ͨ¹ýÃüÁîÐÐ
'--dependency'Ñ¡Ïî¡£Èç¹ûÄ㲻ϲ»¶Ä¬Èϵģ¬Äã¿ÉÒÔÑ¡Ôñͨ¹ý '--configuration'
Ñ¡ÏîÀ´ÅäÖ᣸ü¶àÐÅÏ¢¼ûDependencyInsightReportTask ÀàµÄAPIÎĵµ¡£
ÏîÄ¿ÊôÐÔÁбí
Ö´ÐÐ gradle properties ¿ÉÒÔ»ñÈ¡ÏîÄ¿ËùÓÐÊôÐÔÁÐ±í£¬ÈçÏÂ
Example 11.16. Information about properties
Ö´ÐÐ gradle -q api:properties Êä³ö
> gradle -q api:properties ------------------------------------------------------------ Project :api - The shared API for the application ------------------------------------------------------------
allprojects: [project ':api']
ant: org.gradle.api.internal.project.DefaultAntBuilder@12345
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@12345
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@12345
asDynamicObject: org.gradle.api.internal.ExtensibleDynamicObject@12345
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@12345
buildDir: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build
buildFile: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build.gradle |
Profiling a build
--profile ÃüÁîÑ¡Ïî¿ÉÒԼǼһЩ¹¹½¨ÆÚ¼äµÄÐÅÏ¢²¢±£´æµ½ build/reports/profile
Ŀ¼Ï²¢ÇÒÒÔ¹¹½¨Ê±¼äÃüÃûÕâЩÎļþ
¸Ã±¨¸æÁгö×Üʱ¼äºÍÔÚÅäÖÃºÍ task µÄÖ´ÐÐ ½×¶ÎµÄϸ½Ú¡£²¢ÒÔʱ¼ä´óСµ¹ÐòÅÅÁУ¬²¢ÇҼǼÁËÈÎÎñµÄÖ´ÐÐÇé¿ö
Èç¹û²ÉÓÃÁË buildSrc ¹¹½¨,ÄÇôÔÚ buildSrc/build
ÏÂͬʱҲ»á¸ø buildSrc Éú³ÉÒ»·ÝÈÕÖ¾¼Ç¼

Ö´ÐÐ
ÓÐʱ¿ÉÄÜÄãÖ»ÏëÖªµÀij¸ö task ÔÚÒ»¸ö task ¼¯Öа´Ë³ÐòÖ´ÐеĽá¹û,µ«²¢²»Ïëʵ¼ÊÖ´ÐÐÕâЩ
task ¡£ÄÇôÄã¿ÉÒÔÓà -m Ñ¡Ïî¡£ÀýÈç Ö´ÐÐ gradle -m clean compile ½«»á¿´µ½ËùÓеÄ×÷Ϊ
clean ºÍ compile Ò»²¿·ÖµÄ task »á±»Ö´ÐС£ÕâÓë task ¿ÉÒÔÐγɻ¥²¹,ÈÃÄãÖªµÀÄÄЩ
task ¿ÉÒÔÓÃÓÚÖ´ÐС£
×ܽá
±¾Õ¿´µ½ÁËÃüÁîÐеÄÒ»²¿·Ö¡£
|