David Ignjić
12/04/2024, 12:37 PMonlyIf(task.project.tasks.getByName('compileJava').state.skipped)
Vampire
12/04/2024, 12:46 PMOperationCompletionListener
. Register it as the latter. In the listener implementation check the outcome of the "other" task in question and store it in a property in the build service, then check that property in onlyIf { ... }
of the dependent task.Anze Sodja
12/04/2024, 1:29 PMVampire
12/04/2024, 1:32 PMclose
method while implementing AutoCloseable
and then just use the service, then it will automatically started only when necessary and shut down when necessary.David Ignjić
12/04/2024, 1:37 PMDavid Ignjić
12/04/2024, 1:40 PMVampire
12/04/2024, 2:30 PMDavid Ignjić
12/04/2024, 2:35 PMVampire
12/04/2024, 2:36 PMDavid Ignjić
12/04/2024, 2:38 PMVampire
12/04/2024, 2:42 PMDavid Ignjić
12/04/2024, 3:23 PMDavid Ignjić
12/04/2024, 6:49 PMpublic abstract class CompilationDetectionPlugin implements Plugin<Project> {
@Inject
public abstract BuildEventsListenerRegistry getEventsListenerRegistry();
public abstract static class CompileJavaDetectService implements BuildService<BuildServiceParameters.None>,
OperationCompletionListener {
private Map<String,Boolean> projectRebuild;
private String projectPath;
public boolean projectRebuild(String projectPath) {
return projectRebuild.getOrDefault(projectPath, false);
}
public CompileJavaDetectService() {
this.projectRebuild = new HashMap<>();
this.projectPath = ":compileJava";
}
@Override
public void onFinish(FinishEvent finishEvent) {
if (finishEvent instanceof TaskFinishEvent taskFinishEvent) {
String taskPath = taskFinishEvent.getDescriptor().getTaskPath();
if (taskPath.endsWith(projectPath)) {
if (taskFinishEvent.getResult() instanceof TaskSuccessResult result) {
boolean rebuild = !(result.isFromCache() || result.isUpToDate());
this.projectRebuild.put(taskPath.substring(0,taskPath.length() - projectPath.length()), rebuild);
}
}
}
}
}
@Override
public void apply(Project project) {
Provider<CompileJavaDetectService> serviceProvider =
project.getGradle().getSharedServices().registerIfAbsent(
"compilationJavaDetection", CompileJavaDetectService.class, spec -> {});
getEventsListenerRegistry().onTaskCompletion(serviceProvider);
}
}
And then using it:
project.plugins.apply(CompilationDetectionPlugin)
bootBuildImage {
afterEvaluate {
var projectRelativePathToRoot = project.getRootDir().relativePath(project.getProjectDir())
var service = project.gradle.sharedServices.registrations.compilationJavaDetection.service
var projectPath = project.path
onlyIf(task -> service.get().projectRebuild(projectPath)
|| changedFiles.stream().anyMatch(x -> x.startsWith(projectRelativePathToRoot)))
}}
Vampire
12/04/2024, 11:30 PMproject.plugins
(look at its JavaDoc, but directly project.apply
or better a plugins { ... }
block if you are in a build script or precompiled script plugin.
Why do you think you need the highly discouraged and bad practice afterEvaluate { ... }
?
The main earnings you get from using it are ordering problems, timing problems, and race conditions.
Besides that it is not obvious to me why it should change anything in that snippet.
You should probably declare the task's usage of the service with usesService(...)
.David Ignjić
12/05/2024, 7:22 AMproject.apply({ plugin(CompilationDetectionPlugin) })
. Another thing afterEvalution is used because in each submodule we define variable which is used as image name and when i remove this image name is not defined.David Ignjić
12/05/2024, 7:37 AMproject.provider { "$signedImageName" }
Vampire
12/05/2024, 10:06 AMor justproject.apply({ plugin(CompilationDetectionPlugin) })
project.apply(plugin: CompilationDetectionPlugin)
🙂
Btw. I strongly recommend Kotlin DSL. By now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support if you use a good IDE like IntelliJ IDEA or Android Studio. 🙂David Ignjić
12/05/2024, 10:15 AMThanks i refactored toFor moving from Groovy to Kotlin DSL we willl do ti later on.project.apply({ plugin(CompilationDetectionPlugin) })