gradle custom properties and println - gradle

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"

Related

Gradle - Configure tests includes from property file

I've got a Java project build with Gradle and a property file that contains custom configuration for my testing framework (amount of thread to use, test environment url, custom username & password for those environments, etc...).
I'm facing an issue related to using properties from that file that I can't figure out:
if my Test task include '**/*Test.class', all tests are running as expected.
if my Test task include '**/MyTest.class', only that test is running as expected.
if my Test task include readProperty(), the task is skipped as NO-SOURCE. <- this is the part I can't understand - as the readProperty return the correct value.
Let's get into details:
This is how the property is defined in a my.property file:
testng.class.includes='**/MyTest.class'
This is what the build.gradle file looks like:
Properties props = new Properties()
props.load(new FileInputStream(projectDir.toString() + '/common.properties'))
def testsToRunWorking(p) {
String t = 'MyTest.class'
println "Tests = $t"
return t ? t : '**/*Test.class'
}
def testsToRunNotWorking(p) {
String t = getProperty(p, "testng.class.includes")
println "Tests = $t"
return t ? t : '**/*Test.class'
}
task testCustom(type: Test) {
outputs.upToDateWhen { false }
testLogging.showStandardStreams = true
classpath = configurations.customTest + sourceSets.customTest.output
include testsToRunNotWorking(props) ///< Does not work!
// include testsToRunWorking(props) ///< Works!
useTestNG()
}
In terms of debugging:
The println properly return the value I expect, even when the testCustom task doesn't do what I would expect.
I tried adding a dependsOn task just to print the content of testCustom.configure { println $includes } which looks correct as well.
--info
Tests = '**/MyTest.class'
:clean
:compileCustomTestJava - is not incremental (e.g. outputs have changed, no previous execution, etc.).
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:processCustomTestResources
:customTestClasses
:testCustom NO-SOURCE
The core of the issue seems to be coming from the fact that I'm reading that value from property. I hard coded inside the build.gradle everything works as expected. If read from a property file - build stops with a NO-SOURCE statement.
Any idea?
Thanks!
You are using quotation marks in the values of your property files. Everything that comes after the assignment sign in a property file is used as value, so the quotation marks remain in the string. They are printed in your output Tests = '**/MyTest.class'. On the other hand, if you define a string in your (Groovy) code with quotation marks, they are not included in the string. Therefor, the passed strings are not the same.
Remove the quotation marks from your property file(s) and everything should work, since the class files will match your string without the quotation marks.

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
}

Pass CommandLine Arguments to buildScript

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

Gradle fails with error execCommand == null

I have searched for a while for this problem and am not able to solve it. I pulled a project down from a private git repo. Some people are able to build while others like myself are getting the following error:
Error:Gradle:Execution failed for task ':ProjectName:buildNative'.
> execCommand == null!
If anybody has encountered this and knows how to fix it, please let me know, it does not seem like a problem which is specific to the project I am on.
I think this is the part in the Gradle file where it is failing:
task buildNative(type: Exec) {
if (System.env.ANDROID_NDK_HOME != null) {
def ndkBuild = new File(System.env.ANDROID_NDK_HOME, 'ndk-build')
commandLine ndkBuild
} else {
doLast {
println '##################'
println 'Skipping NDK build'
println 'Reason: ANDROID_NDK_HOME not set.'
println '##################'
}
}
}
Seems like you don't have an ANDROID_NDK_HOME environment variable set. The code above doesn't treat that case correctly. As such, the problem is specific to your build. One way to fix it is to replace doLast with doFirst and to insert throw new StopExecutionException() after the printlns. Additionally you may have to set commandLine (or executable) to a dummy value.

Get user choice before execution of Gradle task

I am switching our application for Capistrano deployment to Gradle .
Here requirement to make the script user interactive .
I am stuck to provide the user input in between task .
task('hello') << {
println "hello" }
task('copy', type: Copy) {
some_user_input = prompt("Are you sure to copy this file. ") ... // Here wants something like that
if(some_user_input==true){
from(file('srcDir'))
into(buildDir)
} }
I am searching for the solution of such issue . If you know about such way than please let me know .
Thanks in advance .
Gradle lets you use existing Ant tasks within your build script. You could use the [Ant input task][1] to achieve this:
ant.input(message: 'Are you sure to copy this file?', validargs: 'y,n', addproperty: 'doDeleteFile')
if(ant.doDeleteFile == 'y') {
// Call copy task
}
Note that unlike System.console() this also works with the Gradle Daemon (tested on Linux).
Have you tried using the console? Something like this:
if (System.console().readLine().toLowerCase() == 'y') ...

Resources