Create Gradle Tasks that executes other gradle task with parameters - gradle

I use this gradle command to execute my UI-tests:
./gradlew connectedDebugAndroidTest -PUiTest
I want to define a custom gradle task that executes my UI-tests, so I could this:
task runUiTests() {
dependsOn("connectedDebugAndroidTest")
}
How can I pass the parameter -PUiTest in my custom gradle task to the connectedDebugAndroidTest task?

Related

How do I run a custom task before the "build" gradle?

How do I automatically run a custom task before gradle build in build.gradle

how to execute 'gradle build' command from gradle.build script

I want to execute a gradle build command from build.gradle file.
build.gradle file
apply plugin: 'java'
apply plugin: 'application'
task me{
//Can we perform gradle build command here.
}
Question:
Can we perform gradle build command inside task me, similarly like we do from command prompt(screenshot attached)..?
Note : I am using windows env, please assume I am providing all other statements in build.gradle file.
I am able to build from command prompt by executing
.
I want to execute this task me from command prompt and it should be responsible to call gradle build command.(attached screenshot)
Yes, if you make the task me to a task of type GradleBuild
https://docs.gradle.org/current/dsl/org.gradle.api.tasks.GradleBuild.html
E.g.
task me(type: GradleBuild){
buildFile 'mybuild.gradle' //defaults to build.gradle so not needed
dir 'someDir'
tasks 'clean', 'build'
}
You cannot "call" a Gradle task in a Gradle build.
You might stumble upon the method execute() that each Gradle task has, but this is a purely internal method that must not be used by any build script. Using it will not work properly. It will not ensure the task is run only once, it will not ensure dependency tasks are run before and so on and so on, just don't use it.
Your code snippet is too big for me wanting to interpret it right now to understand what you want to do exactly, but if you want to "call" a Gradle task from another Gradle task, your logic is bogus and should be changed. If you want to run a Gradle task before or after another Gradle task is run, the only supported way is to use dependsOn (before) or finalizedBy (after) and you can not set these properties during execution phase of a task but only during configuration phase.
Well, there is one other way to call a Gradle task from a build script, but this is by using a task of type GradleBuild which would start a complete new and separate build of the project you specify which is most often not the action you really want to do.

How to add task dependency on all test tasks in gradle in multi module project

I have java gradle project with multiple modules and each module has its tests. How can I configure gradle to run some task (run docker compose up to give you some context, but that's not a part of the question) before first test task is run and some another task after last test has finished?
You can use both dependsOn and finalizedBy on all test tasks from the root project. If all tasks share a common base class (Test in this example), you can use withType:
subprojects*.tasks.withType(Test)*.configure {
dependsOn myPreparationTask
finalizedBy myCleanUpTask
}
If you need to configure tasks of different types, use the matching method instead with a custom condition.

Gradle - run testReport task after test phase of all submodules

I have a simple project with subprojects and I want to generate aggregate report for all tests when I execute gradle test command.
I have followed the gradle documentation and added following:
task testReport(type: TestReport) {
// make sure this task is run after all subproject test tasks
mustRunAfter subprojects*.test
destinationDir = file("$buildDir/reports/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test
}
This works when I execute gradle test testReport, but when I execute gradle test or gradle build in the root project - the task testReport is not run.
How do make gradle to run the task without specifying it every time?
Add: test.finalizedBy 'testReport' to your build.gradle; just at the root level, doesn't have to be inside any closure.
taskX.finalizedBy taskY
Will run taskY everytime taskX completes execution successfully.

Gradle call programatically ./gradlew assembleDebug crashlyticsUploadDistributionStageDebug

How can I execute ./gradlew assembleDebug crashlyticsUploadDistributionStageDebug from my custom task? I tried this code but it does not work:
task myTask {
doFirst {
tasks.assembleStageDebug().execute()
tasks.crashlyticsUploadDistributionStageDebug().execute()
}}
You should never execute tasks directly. Instead define dependencies between them:
task myTask
myTask.dependsOn assembleStageDebug, crashlyticsUploadDistributionStageDebug

Resources