I would like to copy some files in Gradle and the resulting files should not contain any blank lines, i.e., the blank lines are not copied. I assume that can be done with filter(...) and maybe with the TokenFilter from ant. However, I am not sure how to the syntax would look like.
Thanks.
Gradle uses Ant for filtering, because of its powerful implementation. For example, you can use the LineContainsRegExp Ant filter to filter out any line that is only empty or whitespaces.
The appropriate regexp can be [^ \n\t\r]+
You can use Ant directly from Gradle like this:
task copyTheAntWay {
ant.copy(file:'input.txt', tofile:'output.txt', overwrite:true) {
filterchain {
filterreader(classname:'org.apache.tools.ant.filters.LineContainsRegExp') {
param(type:'regexp', value:'[^ \n\t\r]+')
}
}
}
}
or by using the Gradle CopySpec's filter method:
task copyGradlefied(type:Copy) {
def regexp = new org.apache.tools.ant.types.RegularExpression()
regexp.pattern = '[^ \n\t\r]+'
from(projectDir) {
include 'input.txt'
filter(org.apache.tools.ant.filters.LineContainsRegExp, regexps:[regexp])
}
into "outputDir"
}
Related
Using Gradle typed task how can we exclude file copy for file names starting with as well as ending with some strings?
def contentSpec = copySpec {
exclude {
it.file.name.startsWith('img')
it.file.name.endsWith('gif')
}
from 'src'
}
task copyImages (type: Copy) {
with contentSpec
into 'Dest'
}
On running gradle copyImages, it excludes files ending with gif, but does not exclude files starting with img.
Is there a way to achieve both?
You forgot an or (||) between your two conditions:
exclude { it.file.name.startsWith('img') || it.file.name.endsWith('gif') }
The value of a closure is the value of its last expression. Since the last expression, in your code, is it.file.name.endsWith('gif'), that's the value of the closure, and the file is thus excluded when it.file.name.endsWith('gif') is true.
Of course, you could also use two exclusions:
exclude {
it.file.name.startsWith('img')
}
exclude {
it.file.name.endsWith('gif')
}
I'm trying to do some processing on some source before moving it to the build directory. Specifically, for files with a .template name, replace instances of #timestamp# with the timeStamp variable i've defined. Additionally, for .jsp files, I would like to remove whitespace between tags.
The first part of this, replacing the timestamp works. Replacing the whitespace in the jsps does not.
processResources {
def timeStamp = Long.toString(System.currentTimeMillis())
from ('../src/resources/webapp') {
include '**/*.template'
filter {
it.replace('#timestamp#', timeStamp)
}
rename '(.*)\\.template', '$1'
}
from ('../src/resources/webapp') {
include '**/*.jsp'
filter {
it.replace('>\\s+<', '><')
}
}
}
Previous to using processResources, I had done something like this for the minification:
task minifyJSPs(type: Copy) {
from ('../src/resources/webapp') {
include '**/*.jsp'
filter {
it.replace('>\\s+<', '><')
}
}
into 'gbuild'
}
Filtering the files like this using copy worked, however, I noticed I wasn't able to copy from a location to itself -- the file would end up empty. This meant that I had to copy files from one intermediate directory to another, applying a filter at each step.
I want to be able to apply various transformations to my source in one step. How can I fix my processResources task?
I have two resource files. For 'x.properties' I want evaluate placeholder and I want keep 'please-not-modify.properties' unchanged
build.gradle
processResources {
include "**/x.properties"
filter(ReplaceTokens, tokens: [abc: 'ABC', version: '2.3.1'])
}
processResources {
include "**/please-not-modify.properties"
}
Unfortunately, both file have evaluated placeholders...
PS. You guessed, I'm Maven veteran :)
If you have to use placeholders only in a single file from your resources, you should try to apply the filter to only subset of files from your resources, like:
processResources {
filesMatching('**/x.properties') {
filter ReplaceTokens, tokens: [
abc: 'ABC', version: '2.3.1'
]
}
}
Here is used a filesMatching method of the ProcessResources task, which:
Configure the FileCopyDetails for each file whose path matches the specified Ant-style pattern.
So, with it it is possible to provide an additional processing for only subset of the files, which are included in this task.
I have a tree of files that I'd like to copy with Gradle, and for some of the files (e.g. ending in .txt), I'd like to do some property substitions. For example, I have:
task "copyAndroidAssets$flavor" (type: Copy,
dependsOn: ["cleanAndroidAssets", "copyAndroidRes$flavor"] ) {
from "build/assets/${flavorLc}/release/"
into '../android/assets'
expand ( versionName: myVersionName, versionCode: myVersionCode )
}
The problem is that some of the files in the tree being copied have dollar signs ($) in them that have nothing to do with property expansion, and this creates the error SimpleTemplateScript6.groovy: 1: illegal string body character after dollar sign;.
In my specific scenario, all the files except one can simply be copied as is. Only a single file, about.txt, needs to have values substituted. Any suggestions on a simple way to do this?
Now (Gradle 2.3.1) there is a better solution: https://issues.gradle.org/browse/GRADLE-1566
processResources {
inputs.property('version', version)
filesMatching("**/version.properties") {
expand version: version
} }
In my case I want project properties expansion only in yaml files:
processResources {
filesMatching("**/*.yaml") {
expand project.properties
}
}
Thanks to the link from Opal in the comments, I found a solution. The link shows that it is possible to have multiple from sources and each of these can have a separate expand treatment. Thus, in my case, I only wanted to expand .txt files, so I split the from into two parts using include & exclude as follows:
task "copyAndroidAssets$flavor" (type: Copy,
dependsOn: ["cleanAndroidAssets", "copyAndroidRes$flavor"] ) {
from ("build/assets/${flavorLc}/release/") {
include '**/*.txt'
expand ( versionName: versionName, versionCode: versionCode )
}
from ("build/assets/${flavorLc}/release/") {
exclude '**/*.txt'
}
into '../android/assets'
expand ( versionName: myVersionName, versionCode: myVersionCode )
}
Very similar to this question: Gradle Zip task to do multiple sub-trees? Which I don't believe is fully answered, merely circumvented..
I have a project with child projects, built with gradle 1.6, and I need to assemble some results into multiple paths, but I too see the last path surviving.
task zip (type: Zip) {
from ('src/resources') {
into '/'
}
from ('web') {
into '/'
}
from project('A').reference { into ('A') }
from project('B').reference { into ('B') }
}
(Essentially the reference task creates a few directories which are named the same in A and B, so needs to prepend the project name)..
Obviously the references all end up into /B/** in the zip file. When I reverse the order of the two lines, they end up in /A/**.
The other two goes correctly into /. If I move the subproject up before the root resources, they would still go in either /A or /B depending on their order, but the normal resources end in / as assumed.
I would essentially like to include the subprojects dynamically, i.e.
project.subprojects.each {
def pname = it.name
from project(pname).reference {
into "$pname"
}
}
but all my attempts so far has been in vain.
Any pointers welcome
The syntax doesn't look right. It should be from(project('A').reference) { into ('A') }. (Same for B.) Does this make a difference?
PS: into "/" is redundant and can be omitted.