Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gradle task configuration avoidance #2299

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2299.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Avoid creating and configuring gradle tasks
links:
- https://github.com/palantir/gradle-baseline/pull/2299
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ class BaselineEclipse extends AbstractBaselinePlugin {
}

// Run eclipseTemplate when eclipse task is run
project.tasks.eclipse.dependsOn(eclipseTemplate)
project.tasks.named("eclipse").configure {
dependsOn(eclipseTemplate)
}

// Override default Eclipse JRE.
project.tasks.eclipseClasspath.doFirst {
String eclipseClassPath = "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-" + project.sourceCompatibility;
project.eclipse.classpath {
containers.clear()
containers.add(eclipseClassPath)
project.tasks.named("eclipseClasspath").configure {
doFirst {
String eclipseClassPath = "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-" + project.sourceCompatibility;
project.eclipse.classpath {
containers.clear()
containers.add(eclipseClassPath)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class BaselineIdea extends AbstractBaselinePlugin {
}
}

project.getTasks().findByName("idea").doLast(cleanup)
project.getTasks().named("idea").configure(idea -> idea.doLast(cleanup))
}

void applyToRootProject(Project rootProject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ public void apply(Project project) {
project.getExtensions().getByType(SourceSetContainer.class).configureEach(sourceSet -> {
project.getTasks()
.named(sourceSet.getCompileJavaTaskName(), JavaCompile.class)
.get()
.getOptions()
.getCompilerArgumentProviders()
// Use an anonymous class because tasks with lambda inputs cannot be cached
.add(new CommandLineArgumentProvider() {
@Override
public Iterable<String> asArguments() {
if (hasImmutablesProcessor(project, sourceSet)) {
return Collections.singletonList("-Aimmutables.gradle.incremental");
}
.configure(javaCompileTask -> {
javaCompileTask
.getOptions()
.getCompilerArgumentProviders()
// Use an anonymous class because tasks with lambda inputs cannot be cached
.add(new CommandLineArgumentProvider() {
@Override
public Iterable<String> asArguments() {
if (hasImmutablesProcessor(project, sourceSet)) {
return Collections.singletonList("-Aimmutables.gradle.incremental");
}

return Collections.emptyList();
}
return Collections.emptyList();
}
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.GroovyCompile;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.api.tasks.javadoc.Javadoc;
Expand Down Expand Up @@ -71,15 +72,15 @@ public void execute(JavaToolchainSpec javaToolchainSpec) {
configureExecutionTasks(project, javaToolchains.forVersion(extension.runtime()));

// Validation
project.getTasks()
TaskProvider<CheckJavaVersionsTask> checkJavaVersions = project.getTasks()
.register("checkJavaVersions", CheckJavaVersionsTask.class, new Action<CheckJavaVersionsTask>() {
@Override
public void execute(CheckJavaVersionsTask task) {
task.getTargetVersion().set(extension.target());
task.getRuntimeVersion().set(extension.runtime());
project.getTasks().getByName("check").dependsOn(task);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a problem because check would only depend on checkJavaVersions if checkJavaVersions was configured.

}
});
project.getTasks().named("check").configure(check -> check.dependsOn(checkJavaVersions));
});
}

Expand Down