3

I am not able to have buildStarted triggered in my gradle build, not sure what I am doing wrong.

I have a root project gradle file like this version '1.0'

buildscript {
    repositories {
        maven { url 'http://repo.jfrog.org/artifactory/gradle-plugins' }
    }
    dependencies {
        classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.1.0')
    }
}

gradle.buildStarted {
    println "buildStart"
}

gradle. buildFinished {
    println "buildFinished"
}

apply plugin: 'groovy'
apply plugin: 'maven'
.........

buildFinished works as I am able to see corresponding println but buildStarted never seem to get triggered.

EDIT Include init.gradle that uses build listener

class MyBuildAdapter extends BuildAdapter {
    void buildStarted(Gradle gradle) {
       println "buildStarted"
    }    
}

gradle.addBuildListener new MyBuildAdapter()
Prasanna
  • 3,703
  • 9
  • 46
  • 74

2 Answers2

3

The problem is that the buildStarted event occurs before you get a chance to register a callback in a build script. You'll have to use one of the other hooks. For details, see Gradle.addBuildListener in the Javadoc.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • I should have mentioned this in my post, I tried doing that as well but it did not work. I am updating the post with the code snippet. – Prasanna Oct 03 '13 at 19:41
  • 2
    Again, the problem is that the buildStarted event occurs before you get a chance to register a callback in a build script. **You'll have to use one of the other hooks**. – Peter Niederwieser Oct 03 '13 at 20:10
  • Okay. I am trying to download gradle.properties for a project before the build gets started. Without gradle.properties none of the property placeholders in build script will be substituted. So far the buildListener triggers `projectEvaluated` & `taskGraph.whenReady` but these stages are too late in the build lifecycle to not have gradle.properties. Is it possible to do anything like this? – Prasanna Oct 03 '13 at 20:53
  • I don't think it's possible, as `gradle.properties` is read very early on. It should be committed to source control. I'd only configure things like Gradle memory settings there, and would move user-defined properties into a build script or script plugin. – Peter Niederwieser Oct 03 '13 at 21:01
  • 1
    Hello! How can I get total build time? Total time: 1.721 secs Can i access it somehow from BuildResult in void buildFinished(BuildResult result)? @PeterNiederwieser – Vlad Iancu Jan 17 '17 at 22:40
0

I had the same issue in a custom plugin I wrote, and found that only projectsEvaluated callback is the one that gets executed before buildFinished in the BuildListener.

It's been 7 years, and finally Gradle got around to admitting that it didn't make sense to have this method in the first place. It'll be removed in Gradle 7.

https://docs.gradle.org/6.5.1/userguide/upgrading_version_5.html#apis_buildlistener_buildstarted_and_gradle_buildstarted_have_been_deprecated

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219