Rename the filename in build.gradle with version - gradle

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

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

What's the most elegant way to copy just one file with 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)
}
}

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?

How to get access to line number in file Copy filter task?

This is what I need:
I have log statements in my javascript files, they look like this:
log.d("some log message here")
I want to dynamically add fine name and line number to these during the copy task.
Ok, so adding the file name is probably easy enough, but how to I get access to line number? Strangely I could not find any information on how to do this.
The filter() method of Copy task just passing the actual line, it would be nice if it was passing 2 arguments - line and line number.
Here is the template of my task. I added comments of what I need to achieve.
I know I can get name of file from fileCopyDetails using fileCpyDetails.getSourceName() but I am stuck
on how to replace the lines that start with log.d() with a new log.d statement that has line number
I am really hoping someone can help me here.
task addLineNumbers(type: Copy) {
into 'build/deploy'
from 'source'
eachFile { fileCopyDetails ->
// Here I need to add line number to log.d("message")
// to become log.d("[$fileName::$line] + $message")
// for example, if original line in userdetails.js file was log.d("something logged here")
// replace with log.d("[userdetails.js::43] something logged here")
}
}
Not the most elegant solution, but works for me:
task addLineNumbers(type: Copy) {
into 'build/deploy'
from 'source'
def currentFile = ""
def lineNumber = 1
eachFile { fileCopyDetails ->
currentFile = fileCopyDetails.getSourceName()
lineNumber = 1
}
filter { line ->
if (line.contains('log.d("')) {
line = line.replace('log.d("', "log.d(\"[${currentFile}::${lineNumber}]")
}
lineNumber++
return line
}
}
The Copy task just copies files (it can filter/expand files using Ant filters or Groovy SimpleTemplateEngine). You're looking for a pre-processor of sorts. I think it's possible to do that with a custom Ant filter, but it seems like a lot of work.
I think what people typically do is use something like this at runtime to find the file/line number: How can I determine the current line number in JavaScript?

Gradle Copy files and expand only some of them and/or ignore dollar signs in others

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

Resources