Skip to content

Commit

Permalink
Fix prepare optimizations task not being cacheable (#898)
Browse files Browse the repository at this point in the history
The task which prepares optimizations makes use of a properties file
as an input (the configuration of the optimizer). Unfortunately, this
properties file, generated by the config writer task, contains a
timestamp, which breaks cacheability.

This commit fixes the problem by making sure that comments from
the writer task are removed.

Fixes #888
  • Loading branch information
melix authored Dec 4, 2023
1 parent 58f3454 commit fd6f6eb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GString> calculatePossiblePackages(File outputDir) {
def list = new ArrayList()
outputDir.eachDirRecurse { list.add(subpath(it, outputDir)) }
Expand All @@ -256,4 +275,4 @@ class BasicMicronautAOTSpec extends AbstractAOTPluginSpec {
path.substring(basePath.getAbsolutePath().length() + 1, path.size()).replaceAll("\\\\", "/")
}

}
}

0 comments on commit fd6f6eb

Please sign in to comment.