What use instead of TaskInternal execute() at gradle-5.1.1? - gradle

My custom task at gradle-4.10.1:
task fabricUploadApkDevelop(group: "fabric", dependsOn: ['assembleDevDebug']) {
doLast {
//fabric gradle task `assembleRelease crashlyticsUploadDistributionDevDevelop` and options:
def task = tasks.getByName("crashlyticsUploadDistributionDevDebug")
task.ext.betaDistributionGroupAliases = "develop"
task.ext.betaDistributionNotifications = true
task.ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
task.execute()
}
}
But at gradle-5.1.1:
TaskInternal.execute() is removed, so now I can't run my task.execute(), how to change my custom task? Or how to run task crashlyticsUploadDistributionDevDebug with params via terminal?
I tried to use:
//gradle 5.x
task fabricUploadApkDevelop(group: "fabric") {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
doLast {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
}
finalizedBy 'crashlyticsUploadDistributionDevDebug'
}
But crashlyticsUploadDistributionDevDebug task doesn't get params...

The sources for the Fabric/Crashlytics Gradle plugin don’t seem to be publicly available (and I have never used it myself), otherwise I would have checked there. But given your working example for Gradle 4 and looking at the issue through my vanilla Gradle glasses, I’d expect that the following should/could work with Gradle 5:
project.afterEvaluate {
crashlyticsUploadDistributionDevDebug.doFirst {
ext.betaDistributionGroupAliases = "develop"
ext.betaDistributionNotifications = true
ext.betaDistributionReleaseNotesFilePath = "${project.rootDir}/app/build/outputs/apk/dev/debug/releaseNotes.txt"
}
}
task fabricUploadApkDevelop(group: "fabric") {
dependsOn 'crashlyticsUploadDistributionDevDebug'
}
I’d even expect there to be a nicer way to do it but since I can’t test this myself, I wanted to play it safe. Let me know if it worked or what didn’t!

Related

Gradle disable javadoc except on deploy

I have enabled the javadoc for a project with
java {
withJavadocJar()
}
The problem with this is that the javadoc is built everytime the library is built. In my case, this takes several minutes. Is there a good way to make the javadoc only run on gradle deloy?
The following did the trick:
// Disables a task unless we're publishing something.
def disableUnlessPublishing(toDisable) {
toDisable.enabled = false
gradle.taskGraph.whenReady {
gradle.taskGraph.allTasks
.findAll { it.group == "publishing" }
.any { toDisable.enabled = true }
}
}
disableUnlessPublishing(tasks.javadocJar)
disableUnlessPublishing(tasks.javadoc)
disableUnlessPublishing(tasks.sourcesJar)

In gradle, what is the difference betweeen javaexec and a JavaExec task?

For example, I can have a JavaExec task:
task javaExecCaseA(type: JavaExec) {
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(11)
}
classpath = files("MySimpleProgram.jar")
}
or, inside a generic task:
task javaExecCaseB {
doLast {
javaexec {
classpath = files("MySimpleProgram.jar")
}
}
}
I haven't figured out how to specify the JavaLanguageVersion in the 2nd case (javaExecCaseB).
The bigger question though, is what is the difference?
I've tried various ways to set the version in javaExecCaseB, but I end up with an error like:
Could not set unknown property 'javaLauncher' for object of type org.gradle.process.internal.DefaultJavaExecAction_Decorated
I have found that the task is the gradle "JavaExec" task.
And the 2nd case, javaexec is a Project method.
I began this quest to find a way to run Java programs using a different JVM than gradle itself is using (set from an environment variable or command line when running gradle).
I was able to get it to work in both cases:
ext {
MyJvmVersion = 11
}
task SampleJavaExec1(type: JavaExec) {
// Example task for using a custom JVM version with a JavaExec task
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(MyJvmVersion as int)
}
environment['JAVA_HOME'] = javaLauncher.get().metadata.installationPath.asFile.absolutePath
classpath = files("MySimpleProgram.jar")
}
task SampleJavaExec2 {
// Example task for using a custom JVM version with the javaexec method
doLast {
javaexec {
environment['JAVA_HOME'] = "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.10.9-hotspot"
executable = "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.10.9-hotspot\\bin\\java.exe"
classpath = files("MySimpleProgram.jar")
}
}
}
In the 2nd case, javaexec() doesn't appear to have a "javaLauncher".
Instead of hardcoding a path, I also found that I can use javaLauncher to find it for me by adding this code inside the javaexec{} block:
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(MyJvmVersion as int)
}
environment['JAVA_HOME'] = javaLauncher.get().metadata.installationPath.asFile.absolutePath
This should invoke the auto download JVM resolution as well, but I've not tested that part.

Upload all productFlavors with crashlytics

My problem is simple but I don't find the solution. In my app, I have added crashlytics and I want to use beta to upload builds.
For one productFlavor I use the task called crashlyticsUploadDistribution<ProductFlavor>Release and before I have to assemble build with assemble<ProductFlavor>Release otherwise it can upload a wrong apk.
Now I want to merge this two tasks in only one so in my gradle.file
task prepareFabric(group: 'aat') {
println 'HereSTART'
dependsOn 'assemble<ProductFlavor>Release'
}
task publishRealeaseOnFabric(group: 'aat') {
mustRunAfter prepareFabric
println 'HereFINAL'
dependsOn 'crashlyticsUploadDistribution<ProductFlavor>Release'
}
Now when I execute my task (publishRealeaseOnFabric), print appears but the following line is not executed
dependsOn 'assemble<ProductFlavor>Release'
My question is : How can I make two tasks assemble<ProductFlavor>Release and then crashlyticsUploadDistribution<ProductFlavor>Release in one task ?
I use that it create all the flavors
//Creates the app for all the productFlavors
File crashlyticsProperties = new File("${project.projectDir.absolutePath}/fabric.properties")
applicationVariants.all { variant ->
variant.productFlavors.each { flavor ->
println("flavor : " + variant.name)
def variantSuffix = variant.name.capitalize()
def generatePropertiesTask = task("fabricGenerateProperties${variantSuffix}") << {
Properties properties = new Properties()
properties.put("apiKey", flavor.fabricApiKey)
properties.put("apiSecret", flavor.fabricApiSecret)
properties.store(new FileWriter(crashlyticsProperties), "")
}
def generateResourcesTask = project.tasks.getByName("fabricGenerateResources${variantSuffix}")
generateResourcesTask.dependsOn generatePropertiesTask
generateResourcesTask.doLast {
println "Removing fabric.properties"
crashlyticsProperties.delete()
}
}
}

How do I alias gradle task as if it were called with -x parameter?

I want instead of gradle cleanIdea idea -x compileJava -x compileTestJava
call something like gradle ideaNoRecompile
You can use TaskExecutionGraph to do it. First of all, you need to provide a custom task, named ideaNoRecompile, when during the configuration phase, you need to check, whether this graph contains ideaNoRecompile task (that means, that this task will be executed. And if this task should be executed, then you can use a closгre to skip all the tasks, you don't want to be executed. Something like this:
task ideaNoRecompile(dependsOn:idea) {
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(ideaNoRecompile)) {
compileJava.enabled = false
compileTestJava.enabled = false
}
}
}
I've found another similar answer:
task ideaNoRecompile {
finalizedBy allprojects*.tasks*.idea
doFirst {
def skipTasks = ['compileJava', 'compileMirah', 'processResources', 'classes', 'compileTestJava', 'compileTestMirah', 'processTestResources', 'testClasses', 'jar', 'mergeProperties', 'generateModuleManifest' ] as Set
allprojects*.tasks*.each {
if (skipTasks.contains(it.name))
it.enabled = false
}
}
}

Gradle show test output for all subprojects or CLI

showStandardStreams = true
Great but how do I set it on all sub-projects that have the Java plug-in? Better yet, how do I turn it on for only this project from the command line? That way my team can do it without fiddling with the build code.
To set showStandardStreams to true for all sub-projects that have java plugin applied you need the following piece of code:
subprojects.findAll {
it.plugins*.class*.name.any { it.toLowerCase().contains('java')
}.each { p ->
configure(p) {
test {
testLogging.showStandardStreams = true
}
}
}
It cannot be done from commandline. Also, have a look at a demo here.

Resources