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 where we can pass properties to be used in the source file.

view sourceprint?

00.version = ‘DEMO‘

01.group = ‘com.mrhaki‘

02.

03.task copy(type: Copy) {

04.from ‘src/templates‘

05.into "$buildDir"

06.include ‘projectinfo.html.template‘

07.rename { file -> ‘projectinfo.html‘ }

08.expand(project: project, title: ‘ProjectInfo‘, generated: new Date())

09.}

We define the following source file in src/templates/projectinfo.html.template:

view sourceprint?

00.<html>

01.<head>

02.<title>${title}</title>

03.</head>

04.<body>

05.<h1>${project.name}</h1>

06.

07.<ul>

08.<% project.properties.findAll { k,v -> v instanceof String }.each { key, value -> %>

09.<li>$key = $value</li>

10.<% } %>

11.</ul>

12.

13.<hr />

14.<p>Generated on ${generated.format(‘dd-MM-yyyy‘)}</p>

15.</body>

16.</html>

When we run the copy task we get the following output:

时间: 2024-10-23 14:52:04

Gradle Goodness: Parse Files with SimpleTemplateEngine in 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: 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

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

Gradle Goodness: Unpacking an Archive

To create an archive with Gradle is easy. We have several tasks like Zip, Tar, Jar, War and Ear to create a new archive. But there is no UnZip or UnTar to unpack an archive in Gradle. To unpack an archive we must use the Copy task and copy the conten

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

Gradle Goodness: Continue Build Even with Failed Tasks

If we run a Gradle build and one of the tasks fails, the whole build stops immediately. So we have fast feedback of our build status. If we don't want to this and want Gradle to execute all tasks, even though some might have failed, we use the comman

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