Coping files in Gradle is simple.
We start by simply declaring a new task,
and
specifying the type of the task as Copy.
Of course this task won't
actually do anything,
because we haven't told it what file
we'd like copied, and to where.
Simplest example is to copy
the contents of one folder to another.
We use the copy tasks from
method to add sources, and
the into method to
specify the destination.
This copy task will copy
the contents of the images directory
into the build directory.
We can specify files to include or
exclude, our filters can use wild cards,
allowing us to do things like, include
all files that match a certain pattern,
or have a particular file extension.
This combination of source,
destination, inclusions and
exclusions is called a copy spec.
Let's configure our task to
only include jpeg images,
by calling the include method,
and passing a file pattern.
We can also configure a single copy
task to copy from multiple sources.
Copy specs can be hierarchical, which
is particularly useful when you want to
exclude certain files from
a particular sub-folder, or
a specific Include them in another.
Let's configure a copy task which
copies all JPEGs into one folder, and
all GIFs into another.
We define two child copy specs.
One to include only JPEG files, and
the other to include only GIFs.
We also specify the folder we
want to copy these files into.
These folders will be created as
subfolders, under the root copy
destination, the result being that
JPEGs will be copied to build/jpeg, and
all GIFT files will be
copied to build/gif.
Gradle provides task types for
creating archives like ZIP or TAR files.
Archive tasks are nearly identical to
copy tasks, except instead of specifying
a destination directory,
we specify an archive name.
Let's zip up all files
in out images directory.
We use the Zip task base name,
and destinationDir properties
to tell Gradle what to name the archive,
and where to create it.
Note the the destinationDir
property takes a Java file object.
Luckily, the Gradle project interface
provides a convenience method for
creating files.
We can do similar things with
archive tasks and trial copy specs.
We can create an archive
which puts JPEGs and
GIFs into their own
directories inside the archive.
The only difference from
our copy task example,
is that we specify the archive name and
the destination.
Sometimes we want to delete
files during our builds,
usually to clean up
previous build outputs.
We can use the delete task to delete
files or folders in our project.
Here we have a task that
deletes the build folder.
.يتم نسخ الملفات في Gradle بسهولة
نبدأ بإعلان مهمة جديدة
و
.تعيين نوع المهمة باعتبارها نسخ
بالطبع لن تقوم هذه المهمة بالفعل
،بأي شي
لأننا لم نخبرها أي ملف
.نريد القيام بنسخه وإلى أي مكان
إن أبسط مثال هو نسخ
.محتويات أحد المجلدات إلى آخر
نستخدم مهام النسخ من
،أسلوب لإضافة مصادر
وأسلوب into
.لتعيين الوجهة
سوف تقوم مهمة النسخ هذه بنسخ
محتويات دليل الصور
.إلى دليل البنية
نستطيع تعيين الملفات المراد تضمينها أو
،استبعادها، وتستطيع عوامل التصفية استخدام أحرف البدل
مما يتيح لنا القيام بأشياء مثل تضيمن
،جميع الملفات التي تتوافق مع نمط معين
.أو تشتمل على ملحق ملف معين
إن هذه المجموعة التي تشمل المصدر
والوجهة والتضمينات و
.الاستبعادات يطلق عليها مواصفات النسخ
لنقم بتكوين مهمتنا
،لكي تتضمن فقط ملفات jpeg
،باستدعاء أسلوب التضمين
.وتمرير نمط ملف
نستطيع كذلك تكوين مهمة نسخة مفردة
.للنسخ من مصادر متعددة
يمكن أن تكون مواصفات النسخ هرمية، وهو ما يعتبر مفيد
بصفة خاصة عندما تريد
استبعاد ملفات معينة من
مجلد فرعي معين، أو
.أو تضمينهم بصفة خاصة في مجلد آخر
لنقم بتكوين مهمة نسخ تقوم
،بنسخ جميع ملفات JPEG إلى مجلد واحد
.جميع ملفات GIF إلى مجلد آخر
.نقوم بتعريف اثنين من مواصفات نسخ التابع
إحداهما لتضمين فقط ملفات JPEG والأخرى
.لتضمين فقط ملفات GIF
نقوم كذلك بتعيين المجلد
.الذي نريد نسخ هذه الملفات إليه
سوف يتم إنشاء هذه المجلدات باعتبارها
مجلدات فرعية، أسفل وجهة نسخ الجذر
وتكون النتيجة هي نسخ ملفات JPEG إلى build/jpeg ،
ويتم نسخ
جميع ملفات GIFT إلى
.build/gif
ويوفر Gradle أنواع المهام
.لإنشاء أرشيفات مثل ملفات ZIP أو TAR
تكون مهام الأرشيف متطابقة تقريبًا
باستثناء بدلاً من تعيين
،دليل وجهة
.نقوم بتعيين اسم أرشيف
لنقم بأرشفة جميع الملفات
.خارج دليل الصور
،نستخدم الاسم الأساسي لمهمة الضغط
وخصائص destinationDir
لإخبار Gradle بالاسم المحدد للأرشيف
.ومكان إنشائه
لاحظ أن خاصية destinationDir
.تأخذ كائن ملف Java
لحسن الحظ، توفر واجهة مشروع Gradle
أسلوب الملاءمة من أجل
.إنشاء الملفات
يمكن القيام بأشياء مشابهة مع
.مهام الأرشفة ومواصفات النسخة التجريبية
نستطيع إنشاء أرشيف
يضع ملفات JPEG
وGIF بداخل
.الأدلة الخاصة بها بداخل الأرشيف
والفارق الوحيد من
،مثال مهمة النسخ
هو أننا نقوم بتعيين اسم الأرشيف
.والوجهة
نريد في بعض الأحيان حذف
،ملفات خلال الإنشاءات
يكون هذا عادةً من أجل تنظيف
.مخرجات البنية السابقة
يمكننا استخدام مهمة الحذف من أجل حذف
.ملفات أو مجلدات في مشروعنا
لدينا هنا مهمة
.تقوم بحذف مجلد الإنشاء
Copiar arquivos no Gradle é simples.
Começamos declarando uma nova tarefa,
e
especificando o tipo de tarefa como Copy.
Claro que essa tarefa
não fará nada, pois não
especificamos qual arquivo
queremos copiar, nem onde.
O exemplo mais simples é
copiar o conteúdo de uma pasta para outra.
Usamos as tarefas do método
from para adicionar a origem
e do método into
para especificar o destino.
Essa tarefa de cópia copiará
o conteúdo do diretório images
no diretório de compilação.
Especificamos arquivos a incluir ou
excluir. Os filtros podem usar coringas,
possibilitando incluir todos
os arquivos que correspondam a
certo padrão ou tenham extensão específica.
Essa combinação de origem,
destino, inclusões e exclusões
é chamada de copy spec.
Vamos configurar uma tarefa
para incluir só imagens jpeg,
chamando o método include
e passando um padrão de arquivo.
Também podemos configurar tarefas
simples de cópia para copiar de várias origens.
Copy specs podem ser hierárquicos,
o que é útil quando queremos
excluir certos arquivos de
determinada subpasta ou
incluí-los em outra.
Vamos configurar uma tarefa de
cópia que copia todos os JPEGs em uma pasta
e todos os GIFs em outra.
Definimos suas copy specs filho.
Uma para incluir arquivos JPEG e
outra para incluir só GIFs.
Especificamos a pasta em que
queremos copiar esses arquivos.
Essas pastas serão criadas como
subpastas, no grupo de destino
da cópia e assim os JPEGs serão copiados
em build/jpeg, e todos os arquivos GIFT
serão copiados em
build/gif.
Gradle fornece tipos de tarefa
para criar arquivos como ZIP ou TAR.
Tarefas de arquivar são quase idênticas
às de copiar, mas em vez de
especificar o diretório de destino,
especificamos o nome do arquivo.
Vamos zipar todos
os arquivos no diretório images.
Usamos a tarefa Zip, as propriedades
baseName e destinationDir
para dizer ao Gradle como nomear
o arquivo e onde criá-lo.
Veja que a propriedade
destinationDir usa um objeto de arquivo Java.
Por sorte, a interface de projeto Gradle
oferece um método de conveniência
para criar arquivos.
Fazemos coisas similares com
tarefas de arquivar e copy specs de teste.
Criamos um arquivo que
coloca JPEGs e GIFs em seus
próprios diretórios
dentro do arquivo.
A única diferença do exemplo
da tarefa de cópia é que
especificamos o nome do arquivo e
o destino.
Às vezes queremos excluir
arquivos durante as compilações,
para limpar saídas de
compilação antigas.
Podemos usar a tarefa de excluir
para excluir arquivos ou pastas em nosso projeto.
Aqui temos uma tarefa
que exclui a pasta folder.
在 Gradle 中复制文件很容易
只需先声明一个新任务
然后将任务类型指定为“复制”
当然 该任务实际上并不会执行任何操作
因为我们没有告诉它要复制哪些文件 以及要复制到哪里
最简单的示例是 将一个文件夹的内容复制到另一个文件夹
我们使用 copy tasks from 方法来添加源
并使用 into 方法来指定目标
此复制任务会将图像目录的内容
复制到构建目录
我们可以指定要包括或排除的文件 筛选器可以使用通配符
这使我们可以执行多种操作
例如 将匹配特定模式或具有特定文件扩展名的所有文件包括在内
这种源、目标、包括项和排除项的组合
称为复制规格
让我们通过调用 include 方法
并传递文件模式 将任务配置为仅包括 jpeg 图像
我们也可以配置单个复制任务 以从多个来源进行复制
复制规格可以是分层的 这在以下情况特别有用
排除某个特定子文件夹中的特定文件
或者将特定文件包括到另一个文件夹中
我们配置一个复制任务 将所有 JPEG 复制到一个文件夹
将所有 GIF 复制到另一个文件夹
我们要定义两个子复制规格
一个用于仅包括 JPEG 文件 另一个用于仅包括 GIF
我们还要指定要将这些文件复制到的文件夹
这些文件夹将创建为根目录复制目标下方的子文件夹
结果是 所有 JPEG 文件将复制到 build/jpeg
所有 GIF 文件将复制到 build/gif
Gradle 提供了用于创建 ZIP 或 TAR 文件等存档的任务类型
存档任务与复制任务几乎相同
但存档任务不指定目标目录 而是指定存档名称
让我们将所有文件压缩到图像目录内外
我们使用压缩任务的基本名称和 destinationDir 属性
来告诉 Gradle 如何命名存档以及在何处创建存档
请注意 destinationDir 属性需要 Java 文件对象
幸运的是 Gradle 项目界面提供了一种方便的方法
来创建文件
我们可以使用存档任务和试用版复制规格来执行类似操作
我们可以创建一个存档
将 JPEG 和 GIF 放入各自在存档内的目录中
这与复制任务示例唯一的不同之处在于
我们要指定存档名称和目标
有时 我们想要在构建时删除文件
通常要清除以前的构建输出
我们可以使用删除任务来删除项目中的文件或文件夹
我们这里有一个删除构建文件夹的任务