Pass CommandLine Arguments to buildScript - gradle

I have a below task
task showLog1 <<{
def grgit = org.ajoberstar.grgit.Grgit.open(dir: '')
def log = grgit.log {
range '$tag1','$tag2'
}
}
Now I was using my tag names after the range but I want to pass this through Command Line. I have gone through few links like http://mrhaki.blogspot.in/2010/10/gradle-goodness-pass-command-line.html and I am passing from cli using -p like below:
gradlew showLog1 -ptag1=tag_one -ptag2=tag_two
But this doesn't give me the log. Anything that I am missing

Try a capital "P".
Like so: -Ptag1=tag_one -Ptag2=tag_two

Its working, I need to write my task like below:
build.gradle
def grgit = org.ajoberstar.grgit.Grgit.open(dir: "")
def log = grgit.log {
range "$tag1","$tag2"
}
Then I need to execute it like this :
Command Line
gradlew showLog1 -Ptag1=tag_one -project-prop tag2=tag_two

If you pass your vars via -P option as format -PvarName=xxx. varName became a property of you project object in buildScript
if (project.hasProperty('varName')) { //check varName is set or not
println "varName set to:" + varName; //use the varName directly.
}

Related

Is there any way to pass variables from bash script to Jenkinsfile without using extra plugins

I'm trying to use variables declared in bash script in my Jenkinsfile (jenkins pipeline) without using extra plugins like EnvInject plugin
please help, any idea will be appreciated
you need to output those variables to a file like Property/Yaml file. Then use pipeline step readProperties / readYaml to read into Map in Jenkinsfile.
steps {
sh'''
...
AA=XXX
BB=YYY
set > vars.prop
'''
script {
vars = readProperties file: 'vars.prop'
env << vars // merge vars into env
echo 'AA='+ env['AA']
}
}
I have done it with something like this, you can store the variables inside Shell into a file inside workspace and then you are out of shell block, read the file in groovy to load the key value pair into your environment
Something like:
def env_file = "${WORKSPACE}/shell_env.txt"
echo ("INFO: envFileName = ${env_file}")
def read_env_file = readFile env_file
def lines = read_env_file.readLines()
lines.each { String line ->
def object = line.split("=")
env.object[0] = object[1]
}

How to call another projects build.gradle file with command line arguments

I went through the following link and successfully implemented a task which calls build.gradle file from another project. i.e. solution provided by #karl worked for me.
But I need something up on that.
Can somebody help me to know how I can pass command line arguments while calling another build.gradle? Command line argument should be the variable which I have generated from my current build.gradle file.
In my case, I am defining a buildNumber and doing something like this:
def buildNumber = '10.0.0.1'
def projectToBuild = 'projectName'
def projectPath = "Path_till_my_Project_Dir"
task executeSubProj << {
def tempTask = tasks.create(name: "execute_$projectToBuild", type: GradleBuild)
// ****** I need to pass buildNumber as command line argument in "$projectPath/$projectToBuild/build.gradle" ******
tempTask.tasks = ['build']
tempTask.buildFile = "$projectPath/$projectToBuild/build.gradle"
tempTask.execute()
}
You should never call execute directly on any gradle object. The fact it's feasible doesn't mean it should be done and it's highly discouraged since you intrude internal gradle's execution graph structure.
What you need is a task of type GradleBuild which has StartParameter field that can be used to carry build options.
So:
task buildP2(type: GradleBuild) {
buildFile = '../p2/build.gradle'
startParameter.projectProperties = [lol: 'lol']
}
Full demo can be found here, navigate to p1 directory and run gradle buildP2.
You should modify your script in the following way:
def buildNumber = '10.0.0.1'
def projectToBuild = 'projectName'
def projectPath = "Path_till_my_Project_Dir"
task executeSubProj(type: GradleBuild) {
buildFile = "$projectPath/$projectToBuild/build.gradle"
tasks = ['build']
startParameter.projectProperties = [buildNumber: '10.0.0.1']
}
In the project that is executed use project.findProperty('buildNumber') to get the required value.

Gradle variable initialization

I have got a little problem with variables / ext properties in gradle.
In my root project i have this:
task foo {
println project.fooContent
}
in my child project fooContent is defined like this:
ext { fooContent='somethingProjectSpecific' }
When executing :childproject:foo it says variable is not set.
Do you know how to circumvent that problem?
This variable is not set since you try to print it at configuration phase. Try with an action (<<) it will be printed on execution phase:
task foo << {
println project.fooContent
}

gradle custom properties and println

I'm very new to gradle so I'm trying to make sense of it. I'm trying to print the custom properties using println and it works when I just use the property. But as soon as I put it in a string it fails and I'm not sure what is going on.
gradle.properties looks like this:
version = '2.0'
description = 'Project Description'
project.ext {
winADTSDKManager = 'SDK Manager.exe'
winADTSDKManagerPath = 'C:/Projects/WinSDKEnv/sdk/adt'
}
in build.gradle I try to run the code:
task androidSDKManager << {
description = 'Run Android SDK Manager'
println project.ext.winADTSDKManagerPath
println 'Starting: $project.ext.winADTSDKManagerPath'
}
The output looks like this:
c:\Projects\Prototypes\STouchGradle>gradle androidSDKManager
:androidSDKManager
'C:/Projects/WinSDKEnv/sdk/adt'
Starting: $project.ext.winADTSDKManagerPath
The second println statement is not printing the path like the one above. Any ideas on what is wrong with what im doing?
Update: 7/27/2014
OK I figured it out. I need to use double quotes like this
println "Starting: $project.ext.winADTSDKManagerPath"

Why won't gradle Exec task run my command?

I have read around stackoverflow and the gradle forms, but I am still stumped. The ultimate goal here is that after I copy some files, I want to set the writable flag -- because 'copy' doesn't like overwriting read-only files on 'nix (huh...), nor can it be forced to do so (harumph!).
Here is the outline of what I have:
task setPermissions (type : Exec) {
executable = 'chmod -R +w'
}
// ... a little while later ...
task('somethingElse') << {
// ... unrelated stuff ...
def String targetDir = "$aVar/theTarget"
// >> TASK CALL <<
setPermissions {
commandLine = [executable + " $targetDir"]
}
// but that doesn't work... this does...
proc = Runtime.getRuntime().exec("chmod -R +w $deployDir")
proc.waitFor()
}
I have tried variations in "setPermissions".
Trial 1:
commandLine = 'chmod'
args = '-R', '+w'
In which case I appended the target directory to "args" when I called setPermissions.
Trial 2:
commandLine = 'chmod -R +w'
In which case I appended the target directory to "commandLine" when I called setPermissions. I also tried making it the only "args" value.
Trial 3:
commandLine = 'chmod', '-R', '+w'
In which case I appended the target directory to "commandLine" when I called setPermissions. I also tried making it the only "args" value.
So what am I doing wrong here that an Exec task won't run this properly, but the Rt.gR.exec() will?
You can't call a task from another task. You'll have to make one depend on the other, or call the Project.exec method from a task action. The syntax for configuring the exec method is exactly the same as for the Exec task.
PS: Have you tried to use Copy.fileMode instead of chmod?

Resources