Skip to content

Commit 8b04b43

Browse files
authored
Merge pull request #33901 from snazy/gradle-app-cds-read-only
Gradle-plugin: properly handle read-only files
2 parents 42f5f1d + 5ee63a0 commit 8b04b43

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuild.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ private void assembleFastJar() {
308308
getLogger().info("Synchronizing Quarkus build for {} packaging from {} and {} into {}", packageType(),
309309
appBuildDir, depBuildDir, appTargetDir);
310310
getFileSystemOperations().sync(sync -> {
311+
sync.eachFile(new CopyActionDeleteNonWriteableTarget(appTargetDir.toPath()));
311312
sync.into(appTargetDir);
312313
sync.from(appBuildDir, depBuildDir);
313314
});

devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildTask.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package io.quarkus.gradle.tasks;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
46
import java.nio.file.Path;
57
import java.util.Map;
68
import java.util.stream.Collectors;
79

810
import javax.inject.Inject;
911

12+
import org.gradle.api.Action;
1013
import org.gradle.api.GradleException;
1114
import org.gradle.api.file.FileCollection;
15+
import org.gradle.api.file.FileCopyDetails;
1216
import org.gradle.api.file.FileSystemOperations;
1317
import org.gradle.api.logging.LogLevel;
1418
import org.gradle.api.tasks.Classpath;
@@ -168,6 +172,9 @@ void generateBuild() {
168172
PackageConfig.BuiltInType packageType = packageType();
169173
getLogger().info("Building Quarkus app for package type {} in {}", packageType, genDir);
170174

175+
// Need to delete app-cds.jsa specially, because it's usually read-only and Gradle's delete file-system
176+
// operation doesn't delete "read only" files :(
177+
deleteFileIfExists(genDir.resolve(outputDirectory()).resolve("app-cds.jsa"));
171178
getFileSystemOperations().delete(delete -> {
172179
// Caching and "up-to-date" checks depend on the inputs, this 'delete()' should ensure that the up-to-date
173180
// checks work against "clean" outputs, considering that the outputs depend on the package-type.
@@ -223,6 +230,7 @@ void generateBuild() {
223230
getFileSystemOperations().copy(copy -> {
224231
copy.from(buildDir);
225232
copy.into(genDir);
233+
copy.eachFile(new CopyActionDeleteNonWriteableTarget(genDir));
226234
switch (packageType) {
227235
case NATIVE:
228236
copy.include(nativeRunnerFileName());
@@ -261,4 +269,32 @@ void abort(String message, Object... args) {
261269
});
262270
throw new StopExecutionException();
263271
}
272+
273+
public static final class CopyActionDeleteNonWriteableTarget implements Action<FileCopyDetails> {
274+
private final Path destDir;
275+
276+
public CopyActionDeleteNonWriteableTarget(Path destDir) {
277+
this.destDir = destDir;
278+
}
279+
280+
@Override
281+
public void execute(FileCopyDetails details) {
282+
// Delete a pre-existing non-writeable file, otherwise a copy or sync operation would fail.
283+
// This situation happens for 'app-cds.jsa' files, which are created as "read only" files,
284+
// prefer to keep those files read-only.
285+
286+
Path destFile = destDir.resolve(details.getPath());
287+
if (Files.exists(destFile) && !Files.isWritable(destFile)) {
288+
deleteFileIfExists(destFile);
289+
}
290+
}
291+
}
292+
293+
protected static void deleteFileIfExists(Path file) {
294+
try {
295+
Files.deleteIfExists(file);
296+
} catch (IOException e) {
297+
throw new RuntimeException(e);
298+
}
299+
}
264300
}

devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.gradle.api.file.FileCollection;
1616
import org.gradle.api.tasks.CacheableTask;
1717
import org.gradle.api.tasks.CompileClasspath;
18+
import org.gradle.api.tasks.Input;
1819
import org.gradle.api.tasks.InputFiles;
1920
import org.gradle.api.tasks.OutputDirectory;
2021
import org.gradle.api.tasks.PathSensitive;
@@ -62,6 +63,11 @@ public void setCompileClasspath(Configuration compileClasspath) {
6263
this.compileClasspath = compileClasspath;
6364
}
6465

66+
@Input
67+
public Map<String, String> getCachingRelevantInput() {
68+
return extension().baseConfig().quarkusProperties();
69+
}
70+
6571
@InputFiles
6672
@PathSensitive(PathSensitivity.RELATIVE)
6773
public Set<File> getInputDirectory() {

0 commit comments

Comments
 (0)