Gradle命令入門

jopen 9年前發布 | 45K 次閱讀 Gradle 項目構建

  • 當使用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

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!