How can I read a property from the command line and use it in my Gradle build.gradle.kts? - gradle

Sorry if this is obvious, I'm new to Gradle and I'd like to include the latest git commit tag in my builds.
So far I have this task that simply outputs the string I want to save.
tasks.register<Exec>("get-git-latest") {
executable("git")
args("log", "--oneline", "-1", "--format=format:%h", ".")
}
Ideally I'd like to get this output into a variable that can be reused by other Gradle tasks, what is the best way to do this with Kotlin DSL? Any suggestions are welcome.

Upon revisitng the question that #aSemy linked, I was able to get the value into a variable in my Kotlin DSL like so:
val gitLatestCommit: String = ByteArrayOutputStream().use { outputStream ->
project.exec {
executable("git")
args("log", "--oneline", "-1", "--format=format:%h", ".")
standardOutput = outputStream
}
outputStream.toString()
}
I still need to figure out how to inject this into certain property files, but this is a great start. Thank You.

Related

Spring Rest Doc junit 5 parameterized Tests support

I read the documentation, but I didn't find anything about whether the parameterized tests functionality of junit 5 is supported. I tried it but unfortunately the result is always overwritten by the next one. Does one of you know if something like the following is possible?
#ParameterizedTest
#ValueSource(strings = { "Hello", "JUnit" })
public void testSnippet(String pseudo) {
this.mockMvc.perform(get("/pseudoCode/{pseudo}", pseudo))
.andExpect(status().isOk())
.andDo(document("locations", pathParameters(
parameterWithName("pseudo").description("It's just pseudo code")
)));
}
I expecting two folders, the first with a sample containing "Hello" as path parameter and the second with "JUnit" as path parameter.
Any suggestions?
You're using the same document identifier for both test executions:
document("locations"
so... the same folder is used for both.

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

custom war tasks and applying custom resources within Gradle

i want to have dynamic WAR tasks based on customer configuration. I created an array with the configuration names and tried to apply custom behavior as so:
ext.customerBuilds = ['customer1', 'customer2', 'customer3']
ext.customerBuilds.eachWithIndex() {
obj, i ->
task "dist_${obj}" (type:War) << {
from "etc/customers/${obj}/deploy"
println "I'm task number $i"
}
};
This creates my three tasks like dist_customer1, etc. Now i want that gradle uses the normal resources under src/main/webapp AND also my customer based ones under etc/customers/XXXX/deploy as stated in the from property.
But it doesnt pick up any file in this folder.
What i am doing wrong here? Thanks.
when setting up your War task ensure you don't accidently use the '<<' notation.
'<<' is just a shortcut for 'Task#doLast'. so instead do:
ext.customerBuilds = ['customer1', 'customer2', 'customer3']
ext.customerBuilds.eachWithIndex() { obj, i ->
task("dist_${obj}", type:War){
from "etc/customers/${obj}/deploy"
println "I'm task number $i"
}
};
You can just add more from statements to pickup stuff from 'src/main/webapp'.

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?

How to skip empty lines when copying files with Gradle?

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

Resources