Gradle:只获取任务名称

时间:2016-03-03 10:49:36

标签: gradle gradlew

出于构建自动化的目的,我需要能够执行许多匹配给定模式的gradle任务,处理组合结果,清理一点,然后在列表中执行另一个任务。为此,我需要获取所有任务名称,除了gradle中的名称。但是,当我说./gradlew -tasks时,我也会得到任务说明和其他一些我不需要的文字。

我是否可以通过gradle来获取任务名称?快速谷歌搜索没有产生任何结果(尚)。

3 个答案:

答案 0 :(得分: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"
       }
    }
}

用法:

> gradle -PtaskPattern=foo.* outputTasksMatching 

答案 1 :(得分:0)

您可以从taskGraph

获取当前上下文中的所有任务名称
task printTasks << {
    def listTasks = gradle.taskGraph.getAllTasks();
    listTasks.each { logger.lifecycle(it.name) }
}

跳过执行其他任务使用下面的代码

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

使用

gradle printTasks task1 task2 task3 它会打印所有定义的任务和相关的任务;

答案 2 :(得分:0)

如果您询问是否以编程方式嵌入Gradle,则可以使用Tooling API。您可以使用它来访问Gradle项目并以编程方式进行决策(如您所描述的那样)。

这是我使用Gradle 2.13和JUnit 5项目运行的一个简单示例:

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

连接到项目:

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();
  }
}

输出:

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