Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

daverbk/gradle-custom-task

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project Structure

This repository contains the basic project structure to use when in need of Java classes code usage in Gradle custom tasks.

Requirements for the thing to work

  • The following project structure must be used. The buildSrc folder must be present in the root of the project.
gradle-project
├── app
│   ├── build.gradle
│   └── src             // some java code
│       └── ...
├── buildSrc
│   ├── build.gradle
│   ├── settings.gradle
│   └── src             // common build logic
│       └── ...
├── settings.gradle
├── gradle
├── gradlew
└── gradlew.bat
  • The buildSrc should contain the task logic. In our case - CustomGradleTask.java. The logic may be split into multiple classes. The task in real life will probably be much more complex - check Gradle documentation for more information.
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

public abstract class CustomGradleTask extends DefaultTask {

    @TaskAction
    public void run() {
        System.out.println("Running some custom default task ...");
    }
}
  • The app module (or the module where your task has to be applied) should contain the build.gradle file with the following content. The dependency on build or any other task should be defined here.
plugins {
    id 'java'
}

group = 'org.studies'
version = '1.0-SNAPSHOT'

tasks.register('customTask', CustomGradleTask) {
}

tasks.build.dependsOn(customTask)

repositories {
    mavenCentral()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
    useJUnitPlatform()
}

If I now run build, I'll see the output of the custom task.

$ ./gradlew build

build-output

Sources