Gradle Goodness: Copy Files with Filtering

Gradle Goodness: Copy Files with Filtering

Gradle‘s copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method to define a filter. The good news is we can reuse the Ant filtering classes from the org.apache.tools.ant.filters package. We define the filtering class and can pass parameters for the filter. Or we can pass a closure which is passed each line as an argument. Within the closure we must return the filtered line.

view sourceprint?

00.import org.apache.tools.ant.filters.*

01.

02.task(‘filterCopy‘, type: Copy) {

03.from ‘src/templates‘

04.into buildDir

05.include ‘**/*.txt‘

06.filter { line -> line.contains(‘Gradle‘) ? line : ‘‘ }

07.filter(ReplaceTokens, tokens: [author: ‘mrhaki‘, gradleVersion: gradle.gradleVersion])

08.filter(ConcatFilter, prepend: file(‘src/include/header.txt‘))

09.}

Now let‘s create a sample text file that will get filtered in src/templates/HelloGradle.txt:

This is just a simple text file. This line will not make it.

We show filtering capabilities of Gradle copy.

This file is written by @[email protected] with Gradle version @[email protected]

And we create the file src/include/header.txt:

Include this header file.

-------------------------

After we run $ gradle filterCopy we get the following contents for the file build/HelloGradle.txt:

Include this header file.

-------------------------

We show filtering capabilities of Gradle copy in combination with the Ant filtering types.

This file is written by mrhaki with Gradle version 0.9-rc-1.

时间: 2024-08-08 05:37:57

Gradle Goodness: Copy Files with Filtering的相关文章

Gradle Goodness: Parse Files with SimpleTemplateEngine in Copy Task

With the copy task of Gradle we can copy files that are parsed by Groovy's SimpleTemplateEngine. This means we can expand properties in the source file and add Groovy code that is going to be executed. We must use the expand() method in the copy task

Gradle Goodness: Renaming Files while Copying

With the Gradle copy task we can define renaming rules for the files that are copied. We use the rename() method of the copy task to define the naming rules. We can use a closure where the filename is the argument of the closure. The name we return f

Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of Gradle is incremental build support. With incremental build support a task is only executed if it is really necessary. For example if a task generate

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. One of the ways to alter the build configuration is with initialization or init scripts. These are like other Gradle scripts but are executed before t

How to Copy files between ESXi hosts using SCP Command

How to Copy files between ESXi hosts using SCP command Enable SSH and allow SSH in ESXi firewall Only prerequisite to copy files between ESXi host using SCP command is that both source and destination ESXi host should have SSH enabled and SSH allowed

Copy Files Blurry 1.0

main.bat 1 @echo off 2 color 0a 3 title Copy Files Blurry 1.0 4 pushd "%~dp0" 5 6 SETLOCAL ENABLEEXTENSIONS 7 SETLOCAL ENABLEDELAYEDEXPANSION 8 9 set CPB_T1=%TEMP%\cpb1.tmp 10 set CPB_T2=%TEMP%\cpb2.tmp 11 set CPB_T3=%TEMP%\cpb3.tmp 12 13 :PROC_

Gradle Goodness: Adding Tasks to a Predefined Group

In Gradle we can group related tasks using the group property of a task. We provide the name of our group and if we look at the output of the tasks task we can see our tasks grouped in section with the given name. In the next sample we create a new t

Gradle Goodness: Add Incremental Build Support to Tasks

Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unless it is necessary. We can help Gradle and configure our task so it is ready for an incremental build. Suppose we have a task that generates a file. T

Gradle Goodness: Automatic Clean Tasks

Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This task is able to remove any output files or directories we have defined for our task. For example we can assign an output file or directory to our task with