We've seen how to create new Gradle
tasks, and add task actions to them.
So far, task action
implementations have been simple,
such as just writing
text out to the console.
But what about more sophisticated tasks?
How might we go about implementing
a task action for, say, copying files?
We would first have to
define some way for
the users to configure which files
should be copied and to where.
We then have to write all the I/O code
necessary to perform the copy action,
handle errors, and
report status back to the user.
Thankfully, Gradle includes many
tasks which are preconfigured
to provide commonly
needed functionality.
We distinguish these tasks by their
type, such as copy, delete, or zip.
We refer to these kinds
of task as type task.
In contrast, tasks whose actions are
implemented inline in the build script
using gif first or gif last
are referred to as ad hoc tasks.
The syntax for creating a type task
is similar to that of an ad hoc task,
the difference being that we define the
type of the task as a name parameter.
تعرفنا على طريقة إنشاء مهام Gradle
.الجديدة، وإضافة إجراءات المهمة لها
ولقد رأينا أن عمليات تنفيذ
,إجراء المهمة أمر سهل للغاية
مثل مجرد كتابة
.نص في وحدة التحكم
ولكن ماذا عن المهام الأكثر تعقيدًا؟
كيف يمكننا التعامل مع تنفيذ إجراء
مهمة مثل نسخ الملفات؟
سيتعين علينا في البداية
تحديد طريقة ما
للمستخدم لكلي يكتشف نوع الملفات
.التي يجب نسخها والمكان المخصص لهذا النسخ
ثم نكتب بعد ذلك كل تعليمات الإدخال/الإخراج البرمجية
،الضرورية لتنفيذ إجراء النسخ
ومعالجة الأخطاء
.وتقرير الحالة مرة ثانية للمستخدم
ومن الرائع أن برنامج Gradle يحتوي على
الكثير من المهام المكونة مسبقًا
لكي يوفر
.الوظائف العامة المطلوبة
ونميز تلك الوظائف عن طريق
.نوعها، مثل النسخ أو الحذف أو الضغط
ونشير إلى هذه الأنواع من المهام
.بنوع المهمة
وفي المقابل، فإن المهام التي يتم تنفيذ
إجراءاتها داخليًا مع برامج نصية للبنية
باستخدام gif first أو gif last
.يشار إليها باعتبارها مهام مؤقتة
وتشبه صيغة بناء نوع المهمة
,تلك المهمة المؤقتة
والفارق بينهما أننا نحدد
.نوع المهمة كمعلمة اسم
Vimos como criar novas tarefas de Gradle
e adicionar ações de tarefa a elas.
Até agora, as implementações
de ação de tarefa foram simples,
como apenas escrever um texto
no console.
Mas e as tarefas mais sofisticadas?
Como podemos implementar uma ação
de tarefa para, digamos, copiar arquivos?
Primeiro teríamos de definir
um modo para os usuários
configurarem quais arquivos
devem ser copiados e onde.
Depois, temos de escrever todo o código I/O
necessário para executar uma ação de cópia,
lidar com erros e
relatar o status para o usuário.
Felizmente, o Gradle inclui muitas
tarefas que são pré-configuradas
para fornecer funcionalidades normalmente
necessárias.
Distinguimos essas tarefas pelo tipo,
como copiar, excluir ou zipar.
Essas tarefas são chamadas de
tarefa de tipo.
Por outro lado, as tarefas cujas ações
são implementas inline no script de compilação
usando gif primeiro ou gif por último
são chamadas de tarefas ad hoc.
A sintaxe para criar uma tarefa de tipo
é similar à de tarefa ad hoc,
com a diferença que definimos o tipo
de tarefa como um parâmetro de nome.
我们已学习了如何创建新的 Gradle
任务以及如何向其中添加任务操作。
目前为止,我们
实施的任务操作都非常简单,
例如只是
向控制台写入文本。
但如果执行更为复杂的任务呢?
我们如何执行
比如复制文件的任务操作呢?
首先,我们需要
为用户定义配置文件的
方式,即应
复制哪个文件及复制到何处。
然后我们应写出所有必需的
I/O 代码,以执行复制操作、
处理错误并
向用户返回状态报告。
幸运的是,Gradle
包含很多预配置的任务,
可提供常规
所需的功能。
我们通过这些任务的类型将其区分开来,
例如复制、删除或压缩。
我们把这些类型
的任务称作类型任务。
相反的,在构建文件中,
首先使用 gif 文件或最后使用 gif 文件
进行内联操作实施的
任务称作点对点任务。
创建类型任务的语法
与创建点对点任务的语法类似,
不同之处在于,我们将任务类型
定义成了名称参数。