下面列出了怎么用org.gradle.api.tasks.TaskCollection的API类实例代码及写法,或者点击链接到github查看源代码。
public static List<TransformTask> findTransformTaskByTransformType(VariantContext appVariantContext, Class<?>
transformClass) {
List<TransformTask> transformTasksList = Lists.newArrayList();
VariantConfiguration config = appVariantContext.getVariantConfiguration();
TaskCollection<TransformTask> transformTasks = appVariantContext.getProject().getTasks().withType(
TransformTask.class);
SortedMap<String, TransformTask> transformTaskSortedMap = transformTasks.getAsMap();
String variantName = config.getFullName();
for (String taskName : transformTaskSortedMap.keySet()) {
TransformTask transformTask = transformTaskSortedMap.get(taskName);
if (variantName == transformTask.getVariantName()) {
if (transformTask.getTransform().getClass() == transformClass) {
transformTasksList.add(transformTask);
}
}
}
return transformTasksList;
}
@Test
public void testMinikubeExtensionSetProperties() {
Project project = ProjectBuilder.builder().withProjectDir(tmp.getRoot()).build();
project.getPluginManager().apply(MinikubePlugin.class);
MinikubeExtension ex = (MinikubeExtension) project.getExtensions().getByName("minikube");
ex.setMinikube("/custom/minikube/path");
TaskContainer t = project.getTasks();
TaskCollection<MinikubeTask> tc = t.withType(MinikubeTask.class);
Assert.assertEquals(3, tc.size());
tc.forEach(
minikubeTask -> {
Assert.assertEquals(minikubeTask.getMinikube(), "/custom/minikube/path");
});
}
/**
* Returns the most suitable Archive-Task for wrapping in the swarm jar - in the following order:
* <p>
* 1. Custom-JAR-Task defined in ThorntailExtension 'archiveTask'
* 2. WAR-Task
* 3. JAR-Task
*/
public static Jar getArchiveTask(Project project) {
TaskCollection<Jar> existingArchiveTasks = project.getTasks().withType(Jar.class);
Jar customArchiveTask = project.getExtensions().getByType(ThorntailExtension.class).getArchiveTask();
if (customArchiveTask != null) {
return existingArchiveTasks.getByName(customArchiveTask.getName());
} else if (existingArchiveTasks.findByName(WarPlugin.WAR_TASK_NAME) != null) {
return existingArchiveTasks.getByName(WarPlugin.WAR_TASK_NAME);
} else if (existingArchiveTasks.findByName(JavaPlugin.JAR_TASK_NAME) != null) {
return existingArchiveTasks.getByName(JavaPlugin.JAR_TASK_NAME);
}
throw new GradleException("Unable to detect Archive-Task: project contains neither 'war' nor 'jar', " +
"nor is custom Archive-Task specified in the \"thorntail\" extension.");
}
@TaskAction
public void run() {
TaskCollection<JavaCompile> javaCompileTasks = this.getProject().getTasks().withType(JavaCompile.class);
CompilerInputOutputConfiguration inputOutputDirectories
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
JjtreeProgramInvoker jjtreeInvoker = new JjtreeProgramInvoker(getProject(), getClasspath(), inputOutputDirectories.getTempOutputDirectory());
ProgramArguments arguments = new ProgramArguments();
arguments.addAll(getArguments());
SourceFileCompiler compiler = new JavaccSourceFileCompiler(jjtreeInvoker, arguments, inputOutputDirectories, getLogger());
compiler.createTempOutputDirectory();
compiler.compileSourceFilesToTempOutputDirectory();
compiler.copyCompiledFilesFromTempOutputDirectoryToOutputDirectory();
compiler.copyNonJavaccFilesToOutputDirectory();
compiler.cleanTempOutputDirectory();
}
@TaskAction
public void run() {
TaskCollection<JavaCompile> javaCompileTasks = this.getProject().getTasks().withType(JavaCompile.class);
CompilerInputOutputConfiguration inputOutputDirectories
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
JavaccProgramInvoker javaccInvoker = new JavaccProgramInvoker(getProject(), getClasspath(), inputOutputDirectories.getTempOutputDirectory());
ProgramArguments arguments = new ProgramArguments();
arguments.addAll(getArguments());
SourceFileCompiler compiler = new JavaccSourceFileCompiler(javaccInvoker, arguments, inputOutputDirectories, getLogger());
compiler.createTempOutputDirectory();
compiler.compileSourceFilesToTempOutputDirectory();
compiler.copyCompiledFilesFromTempOutputDirectoryToOutputDirectory();
compiler.copyNonJavaccFilesToOutputDirectory();
compiler.cleanTempOutputDirectory();
}
@Test
public void generatedJavaFilesFromCompileJavaccAreAddedToMainJavaSourceSet() {
applyJavaccPluginToProject();
applyJavaPluginToProject();
JavaToJavaccDependencyAction action = new JavaToJavaccDependencyAction();
final File outputDirectory = new File(getClass().getResource("/javacc/testgenerated").getFile());
CompileJavaccTask compileJavaccTask = (CompileJavaccTask) project.getTasks().findByName(CompileJavaccTask.TASK_NAME_VALUE);
compileJavaccTask.setOutputDirectory(outputDirectory);
action.execute(project);
TaskCollection<JavaCompile> javaCompilationTasks = project.getTasks().withType(JavaCompile.class);
for (JavaCompile task : javaCompilationTasks) {
assertTrue(task.getSource().contains(new File(outputDirectory, "someSourceFile.txt")));
}
}
@Test
public void compileJavaccDependOnCompileJJTreeWhenInputDirectoryNotEmpty() {
applyJavaPluginToProject();
applyJavaccPluginToProject();
JavaToJavaccDependencyAction action = new JavaToJavaccDependencyAction();
final File inputDirectory = new File(getClass().getResource("/jjtree/input").getFile());
CompileJjtreeTask compileJjtreeTask = (CompileJjtreeTask) project.getTasks().findByName(CompileJjtreeTask.TASK_NAME_VALUE);
compileJjtreeTask.setInputDirectory(inputDirectory);
action.execute(project);
TaskCollection<CompileJavaccTask> compileJavaccTasks = project.getTasks().withType(CompileJavaccTask.class);
for (CompileJavaccTask task : compileJavaccTasks) {
Set<Object> dependencies = task.getDependsOn();
assertTrue(dependencies.contains(project.getTasks().findByName(CompileJjtreeTask.TASK_NAME_VALUE)));
}
}
@Test
public void compileJavaccDoesNotDependOnCompileJJTreeWhenInputDirectoryEmpty() {
applyJavaPluginToProject();
applyJavaccPluginToProject();
JavaToJavaccDependencyAction action = new JavaToJavaccDependencyAction();
final File inputDirectory = new File(getClass().getResource("/empty").getFile());
CompileJjtreeTask compileJjtreeTask = (CompileJjtreeTask) project.getTasks().findByName(CompileJjtreeTask.TASK_NAME_VALUE);
compileJjtreeTask.setInputDirectory(inputDirectory);
action.execute(project);
TaskCollection<CompileJavaccTask> compileJavaccTasks = project.getTasks().withType(CompileJavaccTask.class);
for (CompileJavaccTask task : compileJavaccTasks) {
Set<Object> dependencies = task.getDependsOn();
assertFalse(dependencies.contains(project.getTasks().findByName(CompileJjtreeTask.TASK_NAME_VALUE)));
}
}
private TransformTask findTransformTask(Class transformClazz) {
TaskCollection<TransformTask> androidTasks = project.getTasks().withType(TransformTask.class);
for (TransformTask transformTask : androidTasks) {
if (transformTask.getTransform().getClass().equals(transformClazz)) {
if (appVariantContext.getVariantData().getName().equals(transformTask.getVariantName())) {
return transformTask;
}
}
}
return null;
}
@Test
public void testDefaultMinikubeTasks() {
Project project = ProjectBuilder.builder().withProjectDir(tmp.getRoot()).build();
project.getPluginManager().apply(MinikubePlugin.class);
((ProjectInternal) project).evaluate();
TaskContainer t = project.getTasks();
TaskCollection<MinikubeTask> tc = t.withType(MinikubeTask.class);
Assert.assertEquals(3, tc.size());
AssertMinikubeTaskConfig(tc, "minikubeStart", "start");
AssertMinikubeTaskConfig(tc, "minikubeStop", "stop");
AssertMinikubeTaskConfig(tc, "minikubeDelete", "delete");
}
private void AssertMinikubeTaskConfig(
TaskCollection<MinikubeTask> tc, String taskName, String taskCommand) {
MinikubeTask minikubeTask = tc.getByName(taskName);
Assert.assertEquals(minikubeTask.getMinikube(), "minikube");
Assert.assertEquals(minikubeTask.getCommand(), taskCommand);
Assert.assertArrayEquals(minikubeTask.getFlags(), new String[] {});
}
@TaskAction
public void run() {
TaskCollection<JavaCompile> javaCompileTasks = this.getProject().getTasks().withType(JavaCompile.class);
CompilerInputOutputConfiguration inputOutputDirectories
= new JavaccCompilerInputOutputConfiguration(getInputDirectory(), getOutputDirectory(), getSource(), javaCompileTasks);
JjdocProgramInvoker jjdocInvoker = new JjdocProgramInvoker(getProject(), getClasspath(), inputOutputDirectories.getTempOutputDirectory());
ProgramArguments arguments = new ProgramArguments();
arguments.addAll(getArguments());
SourceFileCompiler compiler = new JavaccSourceFileCompiler(jjdocInvoker, arguments, inputOutputDirectories, getLogger());
compiler.createTempOutputDirectory();
compiler.compileSourceFilesToTempOutputDirectory();
compiler.copyCompiledFilesFromTempOutputDirectoryToOutputDirectory();
compiler.cleanTempOutputDirectory();
}
public JavaccCompilerInputOutputConfiguration(File inputDirectory, File outputDirectory, FileTree source, TaskCollection<JavaCompile> javaCompileTasks) {
this.inputDirectory = inputDirectory;
this.outputDirectory = outputDirectory;
this.source = source;
this.javaCompileTasks = new HashSet<JavaCompile>();
if (!CollectionUtils.isEmpty(javaCompileTasks)) {
this.javaCompileTasks.addAll(javaCompileTasks);
}
}
@Test
public void compileJavaDependsOnCompileJavaccAfterExecutionWhenJavaPluginApplied() {
applyJavaccPluginToProject();
applyJavaPluginToProject();
JavaToJavaccDependencyAction action = new JavaToJavaccDependencyAction();
action.execute(project);
TaskCollection<JavaCompile> javaCompilationTasks = project.getTasks().withType(JavaCompile.class);
for (JavaCompile task : javaCompilationTasks) {
Set<Object> dependencies = task.getDependsOn();
assertTrue(dependencies.contains(project.getTasks().findByName(CompileJavaccTask.TASK_NAME_VALUE)));
}
}
@Test
public void compileJavaDoesNotDependOnCompileJavaccWhenJavaccPluginNotApplied() {
applyJavaPluginToProject();
JavaToJavaccDependencyAction action = new JavaToJavaccDependencyAction();
action.execute(project);
TaskCollection<JavaCompile> javaCompilationTasks = project.getTasks().withType(JavaCompile.class);
for (JavaCompile task : javaCompilationTasks) {
Set<Object> dependencies = task.getDependsOn();
assertFalse(dependencies.contains(project.getTasks().findByName(CompileJavaccTask.TASK_NAME_VALUE)));
}
}
@Test
public void compileJavaDoesNotDependOnCompileJJTreeWhenJavaccPluginNotApplied() {
applyJavaPluginToProject();
JavaToJavaccDependencyAction action = new JavaToJavaccDependencyAction();
action.execute(project);
TaskCollection<JavaCompile> javaCompilationTasks = project.getTasks().withType(JavaCompile.class);
for (JavaCompile task : javaCompilationTasks) {
Set<Object> dependencies = task.getDependsOn();
assertFalse(dependencies.contains(project.getTasks().findByName(CompileJjtreeTask.TASK_NAME_VALUE)));
}
}
@Override
public <S extends T> TaskCollection<S> withType(Class<S> type) {
return filtered(createFilter(type));
}
@Override
public TaskCollection<T> matching(Spec<? super T> spec) {
return filtered(createFilter(spec));
}
@Override
public TaskCollection<T> matching(Closure spec) {
return matching(Specs.<T>convertClosureToSpec(spec));
}
@Override
public <S extends T> TaskCollection<S> withType(Class<S> type) {
return filtered(createFilter(type));
}
@Override
public TaskCollection<T> matching(Spec<? super T> spec) {
return filtered(createFilter(spec));
}
@Override
public TaskCollection<T> matching(Closure spec) {
return matching(Specs.<T>convertClosureToSpec(spec));
}
/**
* Gets the list of all transformtasks
*
* @return
*/
private SortedMap<String, TransformTask> getTransformTasks() {
TaskCollection<TransformTask> transformTasks = project.getTasks()
.withType(TransformTask.class);
return transformTasks.getAsMap();
}
public void apply(Project project) {
TaskCollection<SampleGeneratorTask> generatorTasks = project.getTasks().withType(SampleGeneratorTask.class);
TaskCollection<GitRepoTask> repoTasks = project.getTasks().withType(GitRepoTask.class);
// Add project extension
SamplesExtension extension = project.getExtensions().create("samples", SamplesExtension.class, project);
// Add a task to generate the list of samples
TaskProvider<SamplesManifestTask> manifestTask = project.getTasks().register("samplesManifest", SamplesManifestTask.class, task -> {
task.getManifest().set(project.file("samples-list.txt"));
task.getSampleDirs().set(project.provider(() -> {
return generatorTasks.stream().map(generator -> {
return generator.getSampleDir().get().getAsFile().getAbsolutePath();
}).collect(Collectors.toList());
}));
task.getRepoDirs().set(project.provider(() -> {
return repoTasks.stream().map(generator -> {
return generator.getSampleDir().get().getAsFile().getAbsolutePath();
}).collect(Collectors.toList());
}));
});
// Add a task to clean the samples
project.getTasks().register("cleanSamples", CleanSamplesTask.class, task -> {
// Need the location without the task dependency as we want to clean whatever was generated last time, not whatever will be generated next time
task.getManifest().set(project.provider(() -> {
return manifestTask.get().getManifest().get();
}));
});
// Apply conventions to the generator tasks
generatorTasks.configureEach( task -> {
task.getTemplatesDir().set(project.file("src/templates"));
});
// Add a lifecycle task to generate the source files for the samples
TaskProvider<Task> generateSource = project.getTasks().register("generateSource", task -> {
task.dependsOn(generatorTasks);
task.dependsOn(manifestTask);
task.setGroup("source generation");
task.setDescription("generate the source files for all samples");
});
extension.getExternalRepos().all(it -> {
addTasksForRepo(it, generateSource, project);
});
extension.getSamples().all(it -> {
addTasksForSample(it, project);
});
// Add a lifecycle task to generate the repositories
project.getTasks().register("generateRepos", task -> {
task.dependsOn(repoTasks);
task.setGroup("source generation");
task.setDescription("generate the Git repositories for all samples");
});
}
@Override
public <S extends T> TaskCollection<S> withType(Class<S> type) {
return filtered(createFilter(type));
}
@Override
public TaskCollection<T> matching(Spec<? super T> spec) {
return filtered(createFilter(spec));
}
@Override
public TaskCollection<T> matching(Closure spec) {
return matching(Specs.<T>convertClosureToSpec(spec));
}
@Override
public <S extends T> TaskCollection<S> withType(Class<S> type) {
return filtered(createFilter(type));
}
@Override
public TaskCollection<T> matching(Spec<? super T> spec) {
return filtered(createFilter(spec));
}
@Override
public TaskCollection<T> matching(Closure spec) {
return matching(Specs.<T>convertClosureToSpec(spec));
}