diff --git a/aot-plugin/src/main/java/io/micronaut/gradle/aot/MicronautAOTConfigWriterTask.java b/aot-plugin/src/main/java/io/micronaut/gradle/aot/MicronautAOTConfigWriterTask.java index ee784f06..c3036fcb 100644 --- a/aot-plugin/src/main/java/io/micronaut/gradle/aot/MicronautAOTConfigWriterTask.java +++ b/aot-plugin/src/main/java/io/micronaut/gradle/aot/MicronautAOTConfigWriterTask.java @@ -45,12 +45,13 @@ import org.gradle.api.tasks.PathSensitivity; import org.gradle.api.tasks.TaskAction; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; +import java.io.PrintWriter; +import java.nio.file.Files; import java.util.List; import java.util.Properties; @@ -60,6 +61,8 @@ */ public abstract class MicronautAOTConfigWriterTask extends DefaultTask { + public static final String GENERATED_BY_GRADLE_COMMENT = "Generated by Gradle"; + @InputFile @PathSensitive(PathSensitivity.NONE) @Optional @@ -140,8 +143,22 @@ void writeConfigFile() { } File outputFile = getOutputFile().getAsFile().get(); if (outputFile.getParentFile().isDirectory() || outputFile.getParentFile().mkdirs()) { - try (OutputStream out = new FileOutputStream(outputFile)) { - props.store(out, "Generated by Gradle"); + var baos = new ByteArrayOutputStream(); + try { + props.store(baos, GENERATED_BY_GRADLE_COMMENT); + } catch (IOException e) { + throw new GradleException("Unable to write output file: " + outputFile, e); + } + String content = baos.toString(); + try (var writer = new PrintWriter(Files.newBufferedWriter(outputFile.toPath()))) { + content.lines() + .filter(line -> { + if (line.startsWith("#") && !line.contains(GENERATED_BY_GRADLE_COMMENT)) { + return false; + } + return true; + }) + .forEach(writer::println); } catch (IOException e) { throw new GradleException("Unable to write output file: " + outputFile, e); } diff --git a/functional-tests/src/test/groovy/io/micronaut/gradle/aot/BasicMicronautAOTSpec.groovy b/functional-tests/src/test/groovy/io/micronaut/gradle/aot/BasicMicronautAOTSpec.groovy index 448f2d35..44dbb543 100644 --- a/functional-tests/src/test/groovy/io/micronaut/gradle/aot/BasicMicronautAOTSpec.groovy +++ b/functional-tests/src/test/groovy/io/micronaut/gradle/aot/BasicMicronautAOTSpec.groovy @@ -245,6 +245,25 @@ class BasicMicronautAOTSpec extends AbstractAOTPluginSpec { } + @Issue("https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/888") + def "prepare optimizations task is cacheable"() { + withSample("aot/basic-app") + withPlugins(Plugins.MINIMAL_APPLICATION) + file("gradle.properties") << "org.gradle.caching=true" + + when: + def result = build "prepareJitOptimizations" + + then: + result.task(":prepareJitOptimizations").outcome == TaskOutcome.SUCCESS + + when: + result = build "clean", "prepareJitOptimizations" + + then: + result.task(":prepareJitOptimizations").outcome == TaskOutcome.FROM_CACHE + } + private List calculatePossiblePackages(File outputDir) { def list = new ArrayList() outputDir.eachDirRecurse { list.add(subpath(it, outputDir)) } @@ -256,4 +275,4 @@ class BasicMicronautAOTSpec extends AbstractAOTPluginSpec { path.substring(basePath.getAbsolutePath().length() + 1, path.size()).replaceAll("\\\\", "/") } -} \ No newline at end of file +}