0

For the purposes of build automation I need to be able to execute a number of gradle tasks matching a given pattern, process the assembled result, clean up a bit, and then do another task in a list. To that end i need to get all the task names and nothing but the names from gradle. However, when i say ./gradlew -tasks I also get task descriptions and some other text i don't need.

Is there a key I might pass to gradle to get only task names? Quick googling yielded no results (yet).

Ibolit
  • 9,218
  • 7
  • 52
  • 96

3 Answers3

1

You can get all task names in the current context from taskGraph

task printTasks << {
    def listTasks = gradle.taskGraph.getAllTasks();
    listTasks.each { logger.lifecycle(it.name) }
}

to skip execution of other tasks use below code

gradle.taskGraph.whenReady {taskGraph ->
    if(taskGraph.hasTask(printTasks)){
        def tasks = taskGraph.getAllTasks()
        tasks.each {
            if(it != printTasks){
                it.enabled = false
            }
        }
    }
}

usage

gradle printTasks task1 task2 task3 it would print all the tasks defined and dependant tasks as well;

mkobit
  • 43,979
  • 12
  • 156
  • 150
Taj Ahmed
  • 895
  • 11
  • 19
0

build.gradle

task outputTasksMatching << {
    String taskPattern = properties.taskPattern
    if (!taskPattern) throw new RuntimeException("taskPattern property not specified")
    tasks.all { Task task ->
       if (task.name.matches(taskPattern)) {
          println "$task.name - $task.description"
       }
    }
}

Usage:

> gradle -PtaskPattern=foo.* outputTasksMatching 
mkobit
  • 43,979
  • 12
  • 156
  • 150
lance-java
  • 25,497
  • 4
  • 59
  • 101
0

If you are asking about programmatically embedding Gradle, you could use the Tooling API. You can use this to access Gradle projects and programtically make decisions (like the one you are describing).

Here is a quick example I ran with Gradle 2.13 and the JUnit 5 project:

//build.gradle
plugins {
  id 'groovy'
  id 'java-gradle-plugin'
}
// some other things...
dependencies {
  localGroovy()
}

Connecting to a project:

import org.gradle.tooling.BuildLauncher;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.DomainObjectSet;
import org.gradle.tooling.model.GradleProject;
import org.gradle.tooling.model.GradleTask;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class UsingGradleToolingApi {
  public static void main(String[] args) {

    final Path junit5 = Paths.get("/Users/mkobit/Workspace/personal/junit/junit5");
    final ProjectConnection connection = GradleConnector.newConnector()
                                                        .forProjectDirectory(junit5.toFile())
                                                        .connect();


    final GradleProject gradleProject = connection.getModel(GradleProject.class);

    final DomainObjectSet<? extends GradleTask> tasks = gradleProject.getTasks();

    final List<? extends GradleTask> tasksToRun = tasks.stream()
           .filter(task -> task.getName().matches("spotless.*"))
           .peek(task -> System.out.println("Filtered task: " + task))
           .collect(Collectors.toList());


    final BuildLauncher buildLauncher = connection.newBuild();
    buildLauncher.setStandardOutput(System.out)
        .setStandardError(System.err)
        .forTasks(tasksToRun)
        .run();

    connection.close();
  }
}

Output:

Filtered task: LaunchableGradleProjectTask{path=':spotlessApply',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessCheck',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessDocumentationApply',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessDocumentationCheck',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessGroovyApply',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessGroovyCheck',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessJavaApply',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessJavaCheck',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessMiscApply',public=false}
Filtered task: LaunchableGradleProjectTask{path=':spotlessMiscCheck',public=false}
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
[versioning] WARNING - the working copy has unstaged or uncommitted changes.
:spotlessDocumentationApply
:spotlessGroovyApply
:spotlessJavaApply
:spotlessMiscApply
:spotlessApply
:spotlessDocumentationCheck
:spotlessGroovyCheck
:spotlessJavaCheck
:spotlessMiscCheck
:spotlessCheck

BUILD SUCCESSFUL

Total time: 0.87 secs

Process finished with exit code 0
mkobit
  • 43,979
  • 12
  • 156
  • 150