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) {
Iterable
File 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());
}
List
String cmdArgs = new ArrayList
String();
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) {
Iterable
File srcFiles = null;
boolean gradleVersionAbove75 = false;
List
Task 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) {
List
String cmdArgs = new ArrayList
String();
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!