Gradle命令入门

jopen 9年前

  • 当使用gradle命令行执行task时,每个task只会被执行一次,所以 gradle test test和gradle test命令的执行结果是一模一样的        -x命令用来排除一些命令的执行,比如gradle build -x ext,会在编译的时候不执行ext任务,即使build task依赖ext也不会执行, 但ext所依赖的task如果被其他task依赖是会执行的。
  • --continue,当使用这个参数时,Gradle会执行每一个依赖没有错误的 task,即使出现错误,也会继续执行下去,可以在一次编译中发现尽可能多的bug,所有的fail项会在编译结束后显示出来。如果一个task编译失 败,所以依赖这个task的其他task都不会被执行。举个例子,如果要测试的代码编译错误,test任务将不会执行,因为test任务依赖编译任务。
  • Task名称缩写,当在命令行上面指定task时,不需要提供task的全部名称, 你只需要足够的缩写来供Gradle识别task就行了,比如gradle dist可以缩写为gradle d 或者gradle di。Gradle甚至还支持首字母缩写(camel case骆驼的峰),比如gradle comTest可以缩写为gradle cT
  • 当你执行gradle命令时,它会在当前目录寻找编译文件,你可以使用-b命令来选择另外一个编译文件,如果你使用了-b选项,settings.gradle将不会被使用。
               subdir/myproject.gradle
               task hello << {
                     println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
               }

           > gradle -q -b subdir/myproject.gradle hello

              using build file 'myproject.gradle' in 'subdir'.

           另外,你可以使用-p选项来指定要使用的工程目录,在多工程编译中应该使用-p选项而不是-b

            > gradle -q -p subdir hello

             using build file 'build.gradle' in 'subdir'.
  • Gradle提供了一些内置的task来显示你编译的详细信息,这有助于理解编译的架构,依赖与调试。除了内置的task,你还可以使用project report plugin 来为你的project添加task来生成这些报告。
  • 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属性为为项目添加说明
          description = 'The shared API for the application'
  • gradle -q tasks列出所有的task,默认的只显示那些被指定task group的任务,你可以通过group属性来为task指定群组,也可以为task指定详情进行显示。你可以获取更多的信息通过添加--all选项,这 会列出工程中所有的task与依赖。
          dists {
                 description = 'Builds the distribution'
                 group = 'build'
           }
  • gradle help --task someTask 显示匹配了指定名称的task的详细信息
           > 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

  • gradle 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选项来限制列出指定的任务报告
          > 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 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
  • --profile选项会记录编译时的信息,并在build/reports/profile目录下生成一个报告,可以通过这个报告对编译进行分析。
  • 有时候你会对task列表内任务的执行顺序感兴趣,但并不想任务真的被执行,这时候通过-m选项即可实现
           > gradle -m clean test
           :clean SKIPPED
           :compileJava SKIPPED
           :processResources SKIPPED
           :classes SKIPPED
           :compileTestJava SKIPPED
           :processTestResources SKIPPED
           :testClasses SKIPPED
           :test SKIPPED


BUILD SUCCESSFUL