Fix main class resolution failure when loaded from config cache

See gh-22922
This commit is contained in:
Andy Wilkinson 2020-10-25 08:05:59 +00:00
parent cb31d9547c
commit c85cc33ca9
2 changed files with 24 additions and 13 deletions

View File

@ -28,7 +28,9 @@ import org.gradle.api.DefaultTask;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.Transformer;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.Convention;
@ -136,19 +138,7 @@ public class ResolveMainClassName extends DefaultTask {
}
Provider<String> readMainClassName() {
return this.outputFile.map((file) -> {
if (file.getAsFile().length() == 0) {
throw new InvalidUserDataException(
"Main class name has not been configured and it could not be resolved");
}
Path output = file.getAsFile().toPath();
try {
return new String(Files.readAllBytes(output), StandardCharsets.UTF_8);
}
catch (IOException ex) {
throw new RuntimeException("Failed to read main class name from '" + output + "'");
}
});
return this.outputFile.map(new ClassNameReader());
}
static TaskProvider<ResolveMainClassName> registerForTask(String taskName, Project project,
@ -189,4 +179,23 @@ public class ResolveMainClassName extends DefaultTask {
}
}
private static final class ClassNameReader implements Transformer<String, RegularFile> {
@Override
public String transform(RegularFile file) {
if (file.getAsFile().length() == 0) {
throw new InvalidUserDataException(
"Main class name has not been configured and it could not be resolved");
}
Path output = file.getAsFile().toPath();
try {
return new String(Files.readAllBytes(output), StandardCharsets.UTF_8);
}
catch (IOException ex) {
throw new RuntimeException("Failed to read main class name from '" + output + "'");
}
}
}
}

View File

@ -198,6 +198,8 @@ abstract class AbstractBootArchiveIntegrationTests {
Attributes mainAttributes = jarFile.getManifest().getMainAttributes();
assertThat(mainAttributes.getValue("Start-Class")).isEqualTo("com.example.main.CustomMainClass");
}
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName).getOutcome())
.isEqualTo(TaskOutcome.UP_TO_DATE);
}
private void copyMainClassApplication() throws IOException {