this is my code
.withType(JavaCompile.class) Is there a better way to handle it
class ResourcesPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
if (filterSpringBootPlugins(project)) {
project.getTasks()
.withType(JavaCompile.class)
.matching(new Spec<JavaCompile>() {
@Override
boolean isSatisfiedBy(JavaCompile javaCompile) {
return javaCompile.name == JavaPlugin.COMPILE_JAVA_TASK_NAME
}
})
.find()
.doFirst(new Action<Task>() {
@Override
void execute(Task task) {
copyResources(task.project)
}
})
}
}
private static boolean filterSpringBootPlugins(Project project) {
return !project.plugins.hasPlugin(SpringBootPlugin.class)
}
private static void copyResources(Project project) {
project.copy(new Action<CopySpec>() {
@Override
void execute(CopySpec copySpec) {
copySpec.from("src/main/resources")
.into("build/resources/main")
}
})
}
}
Is this just supposed to be a “simple example”? Gradle already does this for you - its called the processResources
task.
Personally I just always create a task named compile
like so:
task compile(dependsOn: [compileJava, processResources, compileTestJava, processTestResources] )
I only mention it because I think this may be what you are trying to achieve.
Also, matching based on javaCompile.name == JavaPlugin.COMPILE_JAVA_TASK_NAME
means you are looking for a task with a specific name, but to find it you iterate ALL tasks (effectively - Gradle needs to find the tasks of type JavaCompile
for you first) to find it. Why not just get it by name?
if ( filterSpringBootPlugins(project) ) {
project.getTasks()
.getByName( JavaPlugin.COMPILE_JAVA_TASK_NAME )
.doFirst( ... )
}
Thanks for your answer, I think the first task is the solution I want, but I use build cache in my gradle project, I don’t know if this task can still take effect
Thanks for your guidance, I verified that copy resources can also be executed using cached builds
class ResourcesPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.tasks
.getByName(JavaPlugin.COMPILE_JAVA_TASK_NAME)
.dependsOn(JavaPlugin.PROCESS_RESOURCES_TASK_NAME)
}
}