Skip to content

Fix prepare optimizations task not being cacheable #898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("\\\\", "/")
}

}
}