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