Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle execute task after build

I am building my project with gradle, with the following build.gradle file:

project('a'){
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'application'

    buildDir = 'build'

    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
    repositories {
        mavenCentral()
    }
    dependencies {
        compile 'org.slf4j:slf4j-api:1.7.7'
    } 
}

When I input the gradle build command, I want gradle to execute a task after the build.

I found a mustRunAfter on the Internet, and I have tried a variety of ways but failed.

Please tell me if you know how.

like image 968
jake Avatar asked Jun 16 '15 01:06

jake


People also ask

Does Gradle build run all tasks?

You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.

What happens when we do Gradle build?

Build phasesDuring the initialization phase, Gradle determines which projects are going to take part in the build, and creates a Project instance for each of these projects. During this phase the project objects are configured. The build scripts of all projects which are part of the build are executed.


2 Answers

What you need is finalizedBy, see the following script:

apply plugin: 'java'

task finalize {
    doLast {
       println('finally!')
    }
}

build.finalizedBy(finalize)

Here are the docs.

like image 136
Opal Avatar answered Oct 27 '22 05:10

Opal


Nowadays you can use a BuildListener, it just works. Below is an example written in kotlin DSL

build.gradle.kts

plugins {
    id("com.android.application")
    id("kotlin-android")
    id("kotlin-kapt")
}

android {
    //[..]

    project.gradle.addBuildListener(object : BuildListener {
        override fun buildStarted(gradle: Gradle) {}

        override fun settingsEvaluated(settings: Settings) {}

        override fun projectsLoaded(gradle: Gradle) {}

        override fun projectsEvaluated(gradle: Gradle) {}

        override fun buildFinished(result: BuildResult) {
            
            // add what you need to do here
            println("finally!")
        }

    })
}

dependencies {
    //[...]
}
like image 3
lasec0203 Avatar answered Oct 27 '22 05:10

lasec0203