What's the most elegant way to copy just one file with gradle - gradle

What's the most concise and most elegant and the shortest way to copy just one file AND rename it with gradle?
So far I could think of just this:
copy {
from projectDir
into projectDir
include '.env.template'
rename '.env.template', '.env'
}

You can simplify your CopySpec:
copy {
from file('.env.template')
into projectDir
rename '.*', '.env'
}
The from method accepts single File objects and, since only this one file is copied, the rename pattern can match any copied file.
This way is simple and clean, but to follow the Gradle concept, you should consider using a Copy task, to maintain a clean cut between configuration and execution phase.
Edit:
I just learned, that one can provide a closure for the rename method, so you could also use:
copy {
// ...
rename { '.env' }
}

task copySingleFileInGradle {
doFirst {
def src = new File("sourcefile") // this must exist in top-level project dir
def dst = new File("destinationFile") // this will get created when task is run
dst.write(src.text)
}
}

Related

Using gradle typed tasks how can we exclude different types of files?

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')
}

How to run multiple filters on various file types in processResources

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?

Rename the filename in build.gradle with version

I am working on one gradle script where I need to rename the artifact name at the end but I am facing an issue my code is below.Version is coming from the
version.properties file and I am able to read it properly in build.gradle script but while I change the name of the artifact at the end for e.g. libmain.so to NativeJNI-4.0.0_15 then it doesn't change and change it from libmain.so to filechange.Cansome one let me know what is the issue here
def filechange = file("NativeJNI-${project.version}.so")
//println filechange
task fixartifactname (type: Copy) {
//def filechange = "NativeJNI-${project.version}.so"
//println filechange
from 'build/binaries/mainSharedLibrary'
into 'build/libs'
// def filechange = file("NativeJNI-${project.version}.so")
println filechange
include('libmain.so')
rename ('libmain.so', '${filechange}')
}
//println fixartifactname
build.dependsOn fixartifactname
I am able to fix my issue in below way
def filechange = file("build/libs/NativeJNI-${project.version}.so")
task copyfile(type: Copy) {
from 'build/binaries/mainSharedLibrary'
into 'build/libs'
include('libmain.so')
rename ('libmain.so', filechange.name)
}
Per your question I understand that you're building a native binary. Did you try to set the baseName property for your component? This way you could create your shared library with your desired name from the first place.
Now, regarding your code above then it contains 2 problems:
When calling rename you're wrapping ${filechange} with a single apostrophe (') rather than using inverted commas("), thus this variable is not being resolved to its value. Also, there is no need for a closure block here.
By default using filechange inside the rename would map to its string value which is its full path rather than just the new file name. To overcome this simply use the name property of filechange.
To conclude, each of the following options should do the work for you:
// use inverted commas
rename ('libmain.so', "$filechange.name")
// remove apostrophe or commas
rename ('libmain.so', filechange.name) //
// groovy style function call with no paranthesis
rename 'libmain.so', filechange.name //

Gradle Zip DuplicatesStrategy not working correctly

Suppose you have something like:
task zip(type: Zip) {
archiveName = "out.zip"
duplicatesStrategy = 'exclude'
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder1")
from("$rootDir/customizations/folder2")
}
}
According to http://www.gradle.org/docs/current/javadoc/org/gradle/api/file/DuplicatesStrategy.html Exclude means
Do not allow duplicates by ignoring subsequent items to be created at the same path.
So if you have the same filename in folder1 & folder2 only the file from folder1 should end up in the zip. If you change the two "from" lines in the buildfile, only the file from folder2 should end up there. This seems not to be whats happening (gradle 1.10). Instead always the same file is used. Seems like nested "from"s do not preserve their order.
The only solution I've found is to split up the conflicting parts:
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder1")
}
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder2")
}
now the order is preserved and the output is deterministic

multiple into sections in gradle zip seems to fail

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.

Resources