Could not find method finalizedBy() - gradle

This is my code inside build.gradle
task eatBreakfast {
finalizedBy "brushYourTeeth"
doLast{
println "Om nom nom breakfast!"
}
}
task brushYourTeeth {
doLast {
println "Brushie Brushie Brushie."
}
}
Whenever I run, this is what I get
Where: Build file '/home/android-14/gradleUdacity/build.gradle' line: 3
What went wrong:
A problem occurred evaluating root project 'gradleUdacity'.
Could not find method finalizedBy() for arguments [brushYourTeeth] on root project 'gradleUdacity'.
Can anyone tell me what is the problem?

task.finalizedby(...) was added in gradle 1.7. Which version of gradle are you using?

Related

gradle : How to run externalNativeBuildDebug task after a custom task

I am working on Android project which uses build.gradle. I want to run externalNativeBuildDebug task after a custom task.
In build.gradle, I specify:
task mytask()
{
println 'hello'
}
tasks.getByName("externalNativeBuildDebug").mustRunAfter(mytask)
But gradle throws this error :
Task with name 'externalNativeBuildDebug' not found in project ':app'.
I have defined externalNativeBuild block in android{} block
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
version '3.18.1'
}
}
Is there a way to get externalNativeBuildDebug task and set its dependency?

findByName() method returns null for gradle plugin task

I am using maven-publish plugin. I have a publication called myMedia. When I try executing
tasks.findByName("publishMyMediaPublicationToSnapshotRepository"), it returns null. Why does it return null if there is a task by that name that maven-publish plugin created.
I'm not able to get around this issue. Any help is greatly appreciated.
EDIT 1 : START
I tried Francisco Mateo's suggestion to use the afterEvaluate method, but this too didn't seem to work for me.
However, while trying to figure out the actual cause I came across one interesting observation. I found that the when I call tasks.findByName("publishMyMediaPublicationToSnapshotRepository") from within a task, then it works perfectly fine and prints out the task, i.e. printTaskName runs as expected :
task printTaskName{
doLast{
println "task name found is :--- "+tasks.findByName("publishMyMediaPublicationToSnapshotRepository")
}
}
Now originally in my project's build.gradle I have a publishSnapshot task followed by ordering logic using mustRunAfter method:
task publishSnapshot (dependsOn: ['createMyMediaArchive','publishMyMediaPublicationToSnapshotRepository'])
tasks.findByName("publishMyMediaPublicationToSnapshotRepository").mustRunAfter tasks.findByName("createMyMediaArchive")
publishSnapshot.mustRunAfter tasks.findByName("publishMyMediaPublicationToSnapshotRepository")
executing the publishSnapshot task gives below error:
Cannot invoke method mustRunAfter() on null object
tasks.findByName method doesn't seem to work here as it returns null.
Am I missing out on some basics?
EDIT 1 : END
EDIT 2 : START
The below build.gradle is an edited version of what I actually have. I am not allowed to share the script. But the script is identical in structure to what I have. The rest of build script is as it is as far as the structure and syntax is concerned, just the names changed. Gradle version is 3.5.1
build.gradle
import java.time.LocalDate;
import java.time.format.DateTimeFormatter
import java.text.SimpleDateFormat
import Tasks.PublishArtifacts
import org.gradle.api.GradleException
buildscript {
repositories {
abcRelease()
}
dependencies {
classpath "org.codehaus.groovy.modules.http-builder:http-builder:0.7"
classpath "commons-collections:commons-collections:3.2.1"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.10"
}
}
repositories {
mavenCentral()
abcSnapshot()
xyzRelease()
}
apply plugin: 'maven-publish'
apply plugin: 'base'
apply plugin:'java'
apply from: "$rootDir/repositories.gradle"
def getMediaVersion(){
project.mediaVersion
}
configurations{
releaseMedia
}
publishing{
publications {
myMedia(MavenPublication) {
artifactId project.artifactId
artifact "$project.artifactId"+"-"+getMediaVersion()+".tgz"
version getMediaVersion()
}
}
repositories project.artifactoryRepositories
}
task publishSnapshot (dependsOn: ['createMyMediaArchive','publishMyMediaPublicationToSnapshotRepository'])
tasks.findByName("publishMyMediaPublicationToSnapshotRepository").mustRunAfter tasks.findByName("createMyMediaArchive")
publishSnapshot.mustRunAfter tasks.findByName("publishMyMediaPublicationToSnapshotRepository")
afterEvaluate { evaluated ->
evaluated.tasks.findByName("publishMyMediaPublicationToSnapshotRepository").configure {
println "My Media name --------- "+it.name
}
}
task printTaskName{
doLast{
println "task name found is :--- "+tasks.findByName("publishMyMediaPublicationToSnapshotRepository") // This returns task name as exected
}
}
task createMyMediaArchive{
doLast{
//code for creating media
}
}
EDIT 2 : END
EDIT 3: START
If I run the tasks individually, i.e. first running task createMyMediaArchive and then running task publishSnapshot ,where the taskpublishSnapshot depends on
task publishHelmMediaPublicationToSnapshotRepository, ie.
task publishSnapshot (dependsOn: ['publishHelmMediaPublicationToSnapshotRepository'])
then both the tasks run successfully. This proves that gradle does have the task publishHelmMediaPublicationToSnapshotRepository pre-configured.
Also gradlew tasks --all prints out this task.
This behavior is making things look more confusing to me.
EDIT 3 : END
A lot of the configuration performed by the maven-publish plugin is done lazily.
Wrapping in afterEvaluate should work.
afterEvaluate { evaluated ->
evaluated.tasks.findByName("publishMyMediaPublicationToSnapshotRepository").configure {
println it.name
}
}

gradle tasks -all throwing error

I have test build.gradle file as follows
task someTask(type: Sync) {
def folder = new File('fold1/fold2/');
if(!folder.exists()) {
throw new GradleException('Folder Absent');
}
else {
}
}
When I do gradle tasks --all it is running the task and throwing exception. I was thinking that only when this task is run that it will check for folder but it is actually running it for any task I run.
Can someone suggest workaround for this?
Thanks in advance.
Your code is executed during the configuration phase and not during the execution phase. You need to put it in a doFirst or doLast block:
task someTask(type: Sync) {
doLast {
def folder = new File('fold1/fold2/');
if (!folder.exists()) {
throw new GradleException('Folder Absent');
}
else {
}
}
}
See also: Why is my Gradle task always running?

is there an anchor task that contains bundleDebug and bundleRelease in Android Gradle plugin?

In Android Gradle plugin, task "assemble" is an anchor task that contains assembleDebug and assembleRelease. Is there a similar anchor task that contains bundleDebug and bundleRelease. I currently have following build script where a task depends on bundleDebug:
android.libraryVariants.all {
variant -> variant.javaCompile.classpath += configurations.provided
}
task removeCameraApiJar(dependsOn: 'bundleDebug') << {
FileCollection outputs = tasks['bundleDebug'].getOutputs().getFiles()
outputs.each {
File file ->
println file.name
}
println 'removeCameraApiJar'
}
task assemble.dependsOn(removeCameraApiJar)
However if I replace bundleDebug with just "bundle", the script would fail with following message:
What went wrong: Could not determine the dependencies of task ':camerasupport:removeCameraApiJar'.
Task with path 'bundle' not found in project ':camerasupport'.
It doesn't seem as though the Android plugin creates such a task. You could however do something like
task removeCameraApiJar(dependsOn: tasks.matching { it.name.startsWith('bundle') })
In the current version of the gradle android plugin (1.5.0), the bundle task can be found as a property ("packageLibrary") of the variant output :
android.libraryVariants.all {
variant ->
variant.outputs.each { output ->
FileCollection outputs =
output.packageLibrary.getOutputs().getFiles()
}
}
prinln output.packageLibrary.name will yield "bundle"+buildVariant.

Execute gradle task on sub projects

I have a MultiModule gradle project that I am trying to configure.
Root
projA
projB
other
projC
projD
projE
...
What I want to be able to do is have a task in the root build.gradle which will execute the buildJar task in each of the projects in the other directory.
I know I can do
configure(subprojects.findAll {it.name != 'tropicalFish'}) {
task hello << { task -> println "$task.project.name"}
}
But this will also get projA and projB, I want to only run the task on c,d,e...
Please let me know the best way to achieve this.
Not entirely sure which of these you're after, but they should cover your bases.
1. Calling the tasks directly
You should just be able to call
gradle :other/projC:hello :other/projD:hello
I tested this with:
# Root/build.gradle
allprojects {
task hello << { task -> println "$task.project.name" }
}
and
# Root/settings.gradle
include 'projA'
include 'projB'
include 'other/projC'
include 'other/projD'
2. Only creating tasks in the sub projects
Or is it that you only want the task created on the other/* projects?
If the latter, then the following works:
# Root/build.gradle
allprojects {
if (project.name.startsWith("other/")) {
task hello << { task -> println "$task.project.name" }
}
}
and it can then be called with:
$ gradle hello
:other/projC:hello
other/projC
:other/projD:hello
other/projD
3. Creating a task that runs tasks in the subprojects only
This version matches my reading of your question meaning there's already a task on the subprojects (buildJar), and creating a task in root that will only call the subprojects other/*:buildJar
allprojects {
task buildJar << { task -> println "$task.project.name" }
if (project.name.startsWith("other/")) {
task runBuildJar(dependsOn: buildJar) {}
}
}
This creates a task "buildJar" on every project, and "runBuildJar" on the other/* projects only, so you can call:
$ gradle runBuildJar
:other/projC:buildJar
other/projC
:other/projC:runBuildJar
:other/projD:buildJar
other/projD
:other/projD:runBuildJar
Your question can be read many ways, hope this covers them all :)
All of the ways mentioned by Mark can be used but all of them have some cons. So I am adding one more option:
4. Switching the current project
gradle -p other hello
This switches the "current project" and then runs all tasks named hello under the current project.
Example 5. Defining common behavior of all projects and subprojects,
allprojects {
task hello {
doLast { task ->
println "I'm $task.project.name"
}
}
}
subprojects {
hello {
doLast {
println "- I depend on water"
}
}
}
From the Gradle documentation,
https://docs.gradle.org/current/userguide/multi_project_builds.html

Resources