Hi, I'd like to understand if there is any solutio...
# community-support
h
Hi, I'd like to understand if there is any solution available. I am trying to write an init-script which will do the following: 1. run all the tasks 2. once all tasks executed then collect the all-source code file, including the generated files 3. then run the external program this all works if I use afterExecute(Task task, TaskState state) of TaskExecutionListener but it does not work with configuration-cache. Is there any way to achieve the similar functionality using init-script when configuration cache is enabled I tried following code: #init-script_TaskExecutionListener.gradle #OS: Windows #Java: jdk1.8.0_311 #Gradle: Gradle 6.7 #gradle clean assemble --init-script C:\test\init-mytaskexelistener.gradle #---------------------------------------------------- taskGraph.addTaskExecutionListener( new MyTaskExecutionListener() ); class MyTaskExecutionListener implements org.gradle.api.execution.TaskExecutionListener { public void beforeExecute(Task task) { } public void afterExecute(Task task, TaskState state) { IterableFile srcFiles = null; Project project = task.getProject(); if (task instanceof JavaCompile) { srcFiles = task.getSource().getFiles(); } else if (task instanceof org.gradle.language.c.tasks.CCompile) { srcFiles = task.getSource().getFiles(); } else if (task instanceof org.gradle.language.cpp.tasks.CppCompile) { srcFiles = task.getSource().getFiles(); } else { System.out.println(task.getName()); } ListString cmdArgs = new ArrayListString(); ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("cmd.exe", "/c", "ping -n 1 google.com"); try { Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } int exitCode = process.waitFor(); System.out.println("\nExited with error code : " + exitCode); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return; } } #----------------------------------------------------- #Moved to TaskExecutionGraphListener #OS: Windows #JVM: 17.0.7 #Gradle: Gradle 8.7 #gradle clean assemble --init-script C:\test\init-mytaskexecutiongraph.gradle #------------------------------------------------------------ taskGraph.addTaskExecutionGraphListener( new MyTaskExecutionGraph() ); class MyTaskExecutionGraph implements org.gradle.api.execution.TaskExecutionGraphListener { public void graphPopulated(TaskExecutionGraph graph) { IterableFile srcFiles = null; boolean gradleVersionAbove75 = false; ListTask tasks = graph.getAllTasks(); for(Task task: tasks) { Project project = task.getProject(); if (task instanceof JavaCompile) { srcFiles = task.getSource().getFiles(); } else if (task instanceof org.gradle.language.c.tasks.CCompile) { srcFiles = task.getSource().getFiles(); } else if (task instanceof org.gradle.language.cpp.tasks.CppCompile) { srcFiles = task.getSource().getFiles(); } else { System.out.println(task.getName()); } String gradleVerStr = project.getGradle().getGradleVersion(); String[] verArray = gradleVerStr.split("\\."); Double verValue = Double.valueOf(verArray[0]) * 10 + Double.valueOf(verArray[1]); if (verValue >= 75) { gradleVersionAbove75 = true; } if (gradleVersionAbove75) { ListString cmdArgs = new ArrayListString(); cmdArgs.add("/c"); cmdArgs.add("ping -n 1 google.com"); def Output = project.providers.exec { ignoreExitValue = true; executable = 'cmd.exe' args(cmdArgs) }.standardOutput.asText.get() System.out.println("output : " + Output); } else { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("cmd.exe", "/c", "ping -n 1 google.com"); try { Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } int exitCode = process.waitFor(); System.out.println("\nExited with error code : " + exitCode); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return; } } } } The TaskExecutionListener (init-mytaskexelistener.gradle) script is not configuration-cache friendly and the TaskExecutionGraphListener (init-mytaskexecutiongraph.gradle) is configuration-cache friendly but I need to run script twice. Thanks in Advance!
v
Why do you need to wait for all compilation tasks to run, that seems like quite a waste of time of you only want to operate on the sources. Just add an
Exec
task that has the source files as input files, as long as the generated files are properly registered as source files and not hacked in uncleanly, that will also trigger their generation properly
h
Some task generates source code files, and I want to get all the Source Code files along with generated code then I can do complete analysis.
I tried BuildService with OperationCompletionListener where I override the onFinish method but it is not being called. I feel this can solve my problem, if I get the task wise source files details and in this method I will call external program. But I am not sure if it will be configuration cache friendly.
v
Some task generates source code files, and I want to get all the Source Code files along with generated code then I can do complete analysis.
As I said, if it is done properly, you get them. If you don't get them, the build is not configured properly.
h
Can you please share some suggestion, which one should follow to avoid generated code related issues.
v
• make sure the task is properly declaring its inputs and outputs (as always) • use the task as
srcDir
for the respective source directory set That's it. Any task needing source files will get all sources, including generated ones and will automatically have necessary task dependencies. Whenever you configure
dependsOn
or paths manually, you're doing it wrong.
h
Can I access Task objects to fetch Source Files details inside BuildService onFinish method?
v
Don't think so