Gradle - การใช้งาน Build

Gradle จัดเตรียมบรรทัดคำสั่งเพื่อดำเนินการสร้างสคริปต์ สามารถดำเนินการได้มากกว่าหนึ่งงานในแต่ละครั้ง บทนี้จะอธิบายวิธีดำเนินการหลายงานโดยใช้ตัวเลือกต่างๆ

การดำเนินการหลายงาน

คุณสามารถดำเนินการได้หลายงานจากไฟล์บิลด์เดียว Gradle สามารถจัดการไฟล์บิวด์โดยใช้gradle command. คำสั่งนี้จะรวบรวมแต่ละงานตามลำดับที่ระบุไว้และดำเนินการแต่ละงานพร้อมกับการอ้างอิงโดยใช้ตัวเลือกที่แตกต่างกัน

Example- มีสี่งาน - task1, task2, task3 และ task4 Task3 และ task4 ขึ้นอยู่กับภารกิจ 1 และภารกิจ 2 ดูแผนภาพต่อไปนี้

ใน 4 งานข้างต้นขึ้นอยู่กับแต่ละงานที่แสดงด้วยสัญลักษณ์ลูกศร ดูรหัสต่อไปนี้ คัดลอกสามารถวางลงในbuild.gradle ไฟล์.

task task1 << {
   println 'compiling source'
}

task task2(dependsOn: task1) << {
   println 'compiling unit tests'
}

task task3(dependsOn: [task1, task2]) << {
   println 'running unit tests'
}

task task4(dependsOn: [task1, task3]) << {
   println 'building the distribution'
}

คุณสามารถใช้รหัสต่อไปนี้เพื่อรวบรวมและดำเนินการงานด้านบน

C:\> gradle task4 test

เอาท์พุต:

:task1
compiling source
:task2
compiling unit tests
:task3
running unit tests
:task4
building the distribution

BUILD SUCCESSFUL

Total time: 1 secs

ไม่รวมงาน

ในขณะที่ไม่รวมงานจากการดำเนินการคุณสามารถใช้ตัวเลือก –x ร่วมกับคำสั่ง gradle และระบุชื่อของงานที่คุณต้องการยกเว้น

ใช้คำสั่งต่อไปนี้เพื่อแยก task4 ออกจากสคริปต์ด้านบน

C:\> gradle task4 -x test

เอาท์พุต:

:task1
compiling source
:task4
building the distribution

BUILD SUCCESSFUL

Total time: 1 secs

ดำเนินการสร้างต่อเมื่อเกิดความล้มเหลว

Gradle จะยกเลิกการดำเนินการและล้มเหลวในการสร้างทันทีที่งานใด ๆ ล้มเหลว คุณสามารถดำเนินการต่อได้แม้ว่าจะเกิดความล้มเหลวก็ตาม สำหรับสิ่งนี้คุณต้องใช้ตัวเลือก –continue ด้วยคำสั่ง gradle จัดการแต่ละงานแยกกันพร้อมกับการพึ่งพาของพวกเขา และประเด็นสำคัญหลักคือมันจะตรวจจับความล้มเหลวที่พบแต่ละครั้งและรายงานเมื่อสิ้นสุดการทำงานของบิลด์ สมมติว่าถ้างานล้มเหลวงานที่ตามมาก็จะไม่ถูกดำเนินการ

การเลือกบิลด์ที่จะดำเนินการ

เมื่อคุณรันคำสั่ง gradle จะค้นหาไฟล์ build ในไดเร็กทอรีปัจจุบัน คุณสามารถใช้ตัวเลือก –b เพื่อเลือกไฟล์บิวด์โดยเฉพาะพร้อมกับพา ธ สัมบูรณ์ ตัวอย่างต่อไปนี้เลือกโครงการสวัสดีจากmyproject.gradle ซึ่งอยู่ในไฟล์ subdir/ ลองดูมัน

task hello << {
   println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
}

คุณสามารถใช้คำสั่งต่อไปนี้เพื่อเรียกใช้สคริปต์ข้างต้น

C:\> gradle -q -b subdir/myproject.gradle hello

เอาท์พุต:

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

การรับข้อมูลการสร้าง

Gradle มีงานในตัวหลายอย่างสำหรับการดึงรายละเอียดข้อมูลเกี่ยวกับงานและโครงการ สิ่งนี้มีประโยชน์ในการทำความเข้าใจโครงสร้างและการอ้างอิงของบิวด์ของคุณและสำหรับการดีบักปัญหา คุณสามารถใช้ปลั๊กอินรายงานโครงการเพื่อเพิ่มงานในโครงการของคุณซึ่งจะสร้างรายงานเหล่านี้

รายชื่อโครงการ

คุณสามารถแสดงลำดับชั้นโครงการของโครงการที่เลือกและโครงการย่อยโดยใช้ gradle –q projectsคำสั่ง นี่คือตัวอย่างใช้คำสั่งต่อไปนี้เพื่อแสดงรายการโครงการทั้งหมดในไฟล์บิลด์

C:\> 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

รายงานจะแสดงคำอธิบายของแต่ละโครงการหากระบุไว้ คุณสามารถใช้คำสั่งต่อไปนี้เพื่อระบุคำอธิบาย วางลงในไฟล์build.gradle ไฟล์.

description = 'The shared API for the application'

รายการงาน

คุณสามารถแสดงรายการงานทั้งหมดที่เป็นของหลายโครงการได้โดยใช้คำสั่งต่อไปนี้

C:\> 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
----------
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'.
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>

คุณสามารถใช้คำสั่งต่อไปนี้เพื่อแสดงข้อมูลของงานทั้งหมด

C:\> 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
----------
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'.
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'.

นี่คือรายการคำสั่งบางส่วนในคำอธิบายตารางตัวเลือกต่างๆ

เลขที่ คำสั่ง คำอธิบาย
1 gradle –q help –task <ชื่องาน> ให้ข้อมูลการใช้งาน (เช่นเส้นทางประเภทคำอธิบายกลุ่ม) เกี่ยวกับงานเฉพาะหรือหลายงาน
2 การอ้างอิง gradle –q แสดงรายการการอ้างอิงของโครงการที่เลือก
3 gradle -q api: dependencies --configuration <task name> แสดงรายการของการอ้างอิงที่ จำกัด ตามการกำหนดค่า
4 gradle –q buildEnvironment จัดเตรียมรายการการขึ้นต่อกันของสคริปต์การสร้าง
5 gradle –q dependencyInsight ให้ข้อมูลเชิงลึกเกี่ยวกับการพึ่งพาเฉพาะ
6 คุณสมบัติ Gradle –q แสดงรายการคุณสมบัติของโครงการที่เลือก