I want to run
./gradlew extractMyDeps
instead of
./gradlew :app:dependencies --configuration productionDebugRuntimeClasspath
but I can't pass args to this task via dependOn
How to achieve my goal?
task("extractMyDeps") {
dependsOn(":app:dependencies") // how pass --configuration
}
I think you can't pass the command line arguments to another task. But if you want to make an alias for a task you can use Exec Tasks like this:
task('extractMyDeps', type: Exec) {
workingDir '..'
def arguments = ['cmd', '/c', 'gradlew', ':app:dependencies', '--configuration', 'testCompileClasspath']
commandLine arguments
}
By using this task, you can run:
> gradlew extractMyDeps
Also you can make it more dynamic by passing the configuration name like this:
task('extractMyDeps', type: Exec) {
workingDir '..'
def arguments = ['cmd', '/c', 'gradlew', ':app:dependencies']
if (project.hasProperty("conf")) {
def conf = project.property("conf")
arguments.add('--configuration')
arguments.add(conf.toString())
}
commandLine arguments
}
This way these commands are valid:
> gradlew extractMyDeps
> gradlew extractMyDeps -Pconf=testCompileClasspath
I hope it will help you.
Related
Gradle 2.14
android {
dev {
}
}
task runAppDev(type: Exec, dependsOn: 'installDev') {
description "Install and run app (dev env)"
android.applicationVariants.all { variant ->
if ("installDev".toUpperCase().endsWith(variant.getName().toUpperCase())) {
commandLine getRunAppCommandLine(variant)
}
}
}
def getRunAppCommandLine(variant) {
List<String> commandLine = ["adb", "shell", "am", "start", "-n", variant.getApplicationId() + "/.activity.SplashActivity"]
return commandLine
}
Run my task by: gradlew runAppDev
Result:
Installing APK 'app-dev.apk' on 'Nexus 5 - 6.0.1' for app:dev
Installed on 1 device.
:app:runAppDev
BUILD SUCCESSFUL
So, my application success installed on device, but NOT run on device.
I found bug. I send incorrect adb arg. Here fix:
def getRunAppCommandLine(variant) {
List<String> commandLine = ["adb", "shell", "monkey", "-p", variant.getApplicationId() + " 1"]
return commandLine
}
I'm trying to upload to Crittercism on build, and I put this at the bottom of my gradle file. However, it doesn't seem to be working when I build debug. What am I doing wrong?
task uploadMappingToProd() << {
def critterAppId = "abcde"
def appVersionName = "1.0.1"
def appVersionCode = "DEBUG"
def critterKey = "12345"
commandLine 'curl',
"https://app.crittercism.com/api_beta/proguard/$critterAppId",
'-F', 'proguard=#build/outputs/mapping/production/release/mapping.txt',
'-F', "app_version=$appVersionName-$appVersionCode",
'-F', "key=$critterKey"
type Exec
dependsOn 'assembleDebug'
}
The way you've done it, the task that you have defined, uploadMappingToProd, will if invoked by some reason also invoke assembleDebug. Because you have asked uploadMappingToProd to depend on assembleDebug - not the reverse. Therefore assembleDebug will happily finish without getting anywhere close to uploadMappingToProd.
If you want the reverse dependency i.e. assembleDebug to depend on uploadMappingToProd then you need to add this line after defining your task.
afterEvaluate {
tasks.assembleDebug.dependsOn uploadMappingToProd
}
This will guarantee uploadMappingToProd is invoked everytime and before assembleDebug is invoked.
My solution is working fine.(local build and jenkins build)
for me, only working when build is from jenkins
make sure your APPID, APPKEY, and path(release_jenkins.... normally release)
build.gradle (app) (add at the end)
task uploadPro << {
logger.error("Uploading mapping.txt file to crittercism")
String temp = projectDir;
temp = temp.replace("\\", "/");
String[] cmd = ["curl", "-k", "https://app.crittercism.com/api_beta/proguard/AppIDSting",
"-F", "proguard=#" + temp + "/build/outputs/mapping/release_jenkins/mapping.txt",
"-F", "app_version=" + VERSION_NAME + '-' + VERSION_CODE,
"-F", "key=API_KEY"]
logger.error("CMD : " + cmd)
ProcessBuilder builder = new ProcessBuilder(cmd);
Process process = builder.start();
process.waitFor()
println process.err.text
println process.text
}
gradle.buildFinished {
//check your build type. I am not sure it's the best way to do it.
logger.error("JSC : 이름 ! - " + gradle.startParameter.taskNames);
if (gradle.startParameter.taskNames.contains("assembleRelease_jenkins")) {
logger.error("JSC : 올리기 시작 ! - " + gradle.startParameter.taskNames);
tasks.uploadPro.execute()
} else {
logger.error("JSC : PASS")
}
}
sample project -> https://github.com/SinsangMarket/CrittercismMappingTXT
I am writing a gradle task. The task it invokes returns 3 for successful run instead of 3. How do I go about doing this ?
task copyToBuildShare(){
def robocopySourceDir = "build\\outputs\\apk"
def cmd = "robocopy "+ robocopySourceDir + " C:\\TEST *.* /MIR /R:5 2>&1"
exec {
ignoreExitValue = true
workingDir '.'
commandLine "cmd", "/c", cmd
if (execResult.exitValue == 3) {
println("It probably succeeded")
}
}
}
It gives the error:
Could not find property 'execResult' on task
I don't want to create a separate task. I want it to be in the exec block. What am I doing wrong?
project.exec() has a return value typed ExecResult.
def result = exec {
ignoreExitValue true
executable "cmd"
args = ["/c", "exit", "1"]
}
println "exit value:"+result.getExitValue()
Reference here:
https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:exec(groovy.lang.Closure)
You're going to need to specify that this task is of type Exec. This is done by specifying the task type like so
task testExec(type: Exec) {
}
In your specific case, You'll also want to make sure you don't try to get the execResult until the exec has finished this can be done by wrapping the check in a doLast.
task testExec(type: Exec) {
doLast {
if (execResult.exitValue == 3) {
println("It probably succeeded")
}
}
}
Here's an example of executing ls and checking its return value
task printDirectoryContents(type: Exec) {
workingDir '.'
commandLine "sh", "-c", "ls"
doLast{
if (execResult.exitValue == 0) {
println("It probably succeeded")
}
}
}
In the code provided below I can print each of the file names in the directory, but when it reaches the Exec command it only performs an Exec on the last file.
task frmf2xml(type:Exec) {
new File('src/orca/').eachFile {file ->
if(file.name.endsWith(".fmb")){
println file
commandLine 'cmd', '/c', 'frmf2xml.bat', file, 'OVERWRITE=YES'
}
}
}
I would like it to run the tool on every file
Actually i found a Solution, using execute().
task frmf2xml() {
new File('src/orca/').eachFile {file ->
if(file.name.endsWith(".fmb")){
def cmd = "cmd /c frmf2xml.bat ${file} OVERWRITE=YES"
def result = cmd.execute();
result.waitFor();
}
}
}
I'm using a gradle task which executes the command-line inside a file collection loop:
...
collection.each { file ->
exec {
workingDir = file(props['WORKING_DIR']).getAbsolutePath()
commandLine "java", "-jar", file(props['SIGN_TOOL']).getAbsoluteFile(), file
}
}
...
Unfortunately, the gradle task ends up with this error:
Execution failed for task ':signFiles'.
No signature of method: java.io.File.call() is applicable for argument types: (java.lang.String) values: Possible
solutions: wait(), any(), wait(long), each(groovy.lang.Closure),
any(groovy.lang.Closure), list()
How can i fix this issue?
Thx MVM
You've called your loop var file and then it's trying to use that for the call to file()...
Try renaming your closure variable:
collection.each { aFile ->
exec {
workingDir = file(props['WORKING_DIR']).getAbsolutePath()
commandLine "java", "-jar", file(props['SIGN_TOOL']).getAbsoluteFile(), aFile
}
}