Skip to content

palantir/auto-parallelizable

Repository files navigation

Autorelease

auto-parallelizable

An annotation to easily make Gradle Tasks use the Worker API, which is required for Gradle tasks to run in parallel within the same project.

Usage

Add the library as an annotation processor:

dependencies {
    compileOnly 'com.palantir.gradle.auto-parallelizable:auto-parallelizable-annotations'
    annotationProcessor 'com.palantir.gradle.auto-parallelizable:auto-parallelizable'
}

Reload Gradle in IntelliJ.

Now you can write a container class that will generate the required files:

@AutoParallelizable
final class MyCustom {
    // Create a package-private Params interface with you task
    // inputs/outputs as Gradle Managed Properties
    interface Params {
        @Input
        Property<String> getString();

        @OutputFile
        RegularFileProperty getOutput();
    }
    
    // Create a package-private static void action method here
    static void action(Params params) {
        // do your task action here
    }
    
    private MyCustom() {}
}

Then implement a task next to this:

public abstract class MyCustomTask extends MyCustomTaskImpl {
    public MyCustomTask() {
        // You can initialize values, description etc here
        getString().set("default value");
    }
}

Compile your code and the files MyCustomTaskImpl, MyCustomWorkParams, MyCustomWorkAction will be generated for you!

Gradle Service Injection

You can inject Gradle Services into your action method like so:

import com.palantir.gradle.autoparallelizable.AutoParallelizable.Inject;

@AutoParallelizable
final class MyCustom {
    // Params etc ...
    
    static void action(
            Params params, 
            @Inject ExecOperations execOperations,
            @Inject ProviderFactory providerFactory) {
        
        // Use your injected services here!
    }
}

Read-only managed nested properties

You can use Read-only managed nested properties in your Params interface. Properties will be recursively set.

@AutoParallelizable
final class MyCustom {
    interface Nested {
        @Input
        Property<String> getString();
    }
    
    interface DoubleNested {
        
        Property<String> getDescription();
        
        @Nested
        Nested getNested();
    }
    
    interface Params {
        @Nested
        Nested getNested();
        
        @Nested
        DoubleNested getDoubleNested();
        
        @Nested
        ListProperty<Nested> getNestedList();

        @OutputFile
        RegularFileProperty getOutput();
    }
    
    // action etc ...
    
    private MyCustom() {}
}

Note: If you are using a nested type you don't own, bear in mind that you will need to recompile against newer versions to have any new properties copied over.

About

Annotation processor to easily make Gradle tasks run in parallel in the same project using the Worker API

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published