-
Notifications
You must be signed in to change notification settings - Fork 2
/
QuarkusCachingConfig.java
212 lines (178 loc) · 10.1 KB
/
QuarkusCachingConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.gradle;
import com.gradle.maven.extension.api.cache.BuildCacheApi;
import com.gradle.maven.extension.api.cache.MojoMetadataProvider;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Caching instructions for the Quarkus build goal.
*/
final class QuarkusCachingConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomGradleEnterpriseConfig.class);
// Environment variable to disable caching
private static final String GRADLE_QUARKUS_KEY_CACHE_ENABLED = "GRADLE_QUARKUS_CACHE_ENABLED";
// Quarkus' properties used to configure application.
// This file is generated by the Quarkus build goal.
private static final String QUARKUS_FILE_PROPERTIES_DUMP = ".quarkus/quarkus-prod-config-dump";
// Quarkus' properties read by the Quarkus prepare mojo.
// This file is generated by the Quarkus track-config-changes goal.
private static final String QUARKUS_FILE_PROPERTIES_CHECK = "target/quarkus-prod-config-check";
// Quarkus' configuration keys
private static final List<String> QUARKUS_CONFIG_KEY_NATIVE_CONTAINER_BUILD = Arrays.asList("quarkus.native.container-build", "quarkus.native.remote-container-build");
private static final String QUARKUS_CONFIG_KEY_NATIVE_BUILDER_IMAGE = "quarkus.native.builder-image";
private static final String QUARKUS_CONFIG_KEY_PACKAGE_TYPE = "quarkus.package.type";
// Quarkus' cacheable package types
private static final List<String> QUARKUS_CACHEABLE_PACKAGE_TYPES = Arrays.asList("jar", "legacy-jar", "uber-jar", "native");
// Quarkus' properties which are considered as file inputs
private static final List<String> QUARKUS_KEYS_AS_FILE_INPUTS = Arrays.asList("quarkus.docker.dockerfile-native-path", "quarkus.docker.dockerfile-jvm-path", "quarkus.openshift.jvm-dockerfile", "quarkus.openshift.native-dockerfile");
void configureQuarkusPluginCache(BuildCacheApi buildCache) {
buildCache.registerMojoMetadataProvider(context -> {
context.withPlugin("quarkus-maven-plugin", () -> {
if("build".equals(context.getMojoExecution().getGoal())) {
if(isQuarkusCacheEnabled()) {
LOGGER.info("Configuring caching for Quarkus build");
configureQuarkusBuildGoal(context);
} else {
LOGGER.info("Quarkus caching is disabled (gradle.quarkus.cache.enabled=false)");
}
}
});
});
}
// Cache is enabled by default
private boolean isQuarkusCacheEnabled() {
return !Boolean.FALSE.toString().equals(System.getenv(GRADLE_QUARKUS_KEY_CACHE_ENABLED));
}
private void configureQuarkusBuildGoal(MojoMetadataProvider.Context context) {
// Load Quarkus properties
Properties quarkusProperties = loadQuarkusProperties();
// Check required configuration
if(isQuarkusBuildCacheable(quarkusProperties)) {
configureInputs(context, quarkusProperties);
configureOutputs(context);
} else {
LOGGER.info("Caching not possible for Quarkus goal");
}
}
private Properties loadQuarkusProperties() {
Properties quarkusProperties = new Properties();
if(new File(QUARKUS_FILE_PROPERTIES_DUMP).exists()) {
try (InputStream input = new FileInputStream(QUARKUS_FILE_PROPERTIES_DUMP)) {
quarkusProperties.load(input);
} catch (IOException e) {
LOGGER.error("Error while loading " + QUARKUS_FILE_PROPERTIES_DUMP, e);
}
}
return quarkusProperties;
}
private boolean isQuarkusBuildCacheable(Properties quarkusProperties) {
return isQuarkusPropertiesUnchanged(quarkusProperties)
&& isInContainerBuild(quarkusProperties)
&& isPackagingTypeSupported(quarkusProperties);
}
private boolean isQuarkusPropertiesUnchanged(Properties quarkusProperties) {
Properties quarkusCheckProperties = new Properties();
if(new File(QUARKUS_FILE_PROPERTIES_CHECK).exists()) {
try (InputStream input = new FileInputStream(QUARKUS_FILE_PROPERTIES_CHECK)) {
quarkusCheckProperties.load(input);
Set<Map.Entry<Object, Object>> quarkusPropertiesCopy = new HashSet<>(quarkusProperties.entrySet());
quarkusPropertiesCopy.removeAll(quarkusCheckProperties.entrySet());
if(quarkusPropertiesCopy.size() > 0) {
LOGGER.info("Quarkus properties have changed [" + quarkusPropertiesCopy.stream().map(e -> e.getKey().toString()).collect(Collectors.joining(", ")) + "]");
} else {
return true;
}
} catch (IOException e) {
LOGGER.error("Error while loading " + QUARKUS_FILE_PROPERTIES_CHECK, e);
}
} else {
LOGGER.info("Quarkus properties not found");
}
return false;
}
// Checking native package type is not required as the container build is set by default to true for non native
private boolean isInContainerBuild(Properties quarkusProperties) {
String builderImage = quarkusProperties.getProperty(QUARKUS_CONFIG_KEY_NATIVE_BUILDER_IMAGE);
if(builderImage == null || builderImage.isEmpty()) {
LOGGER.info("Quarkus build is not using a fixed image");
return false;
}
if(QUARKUS_CONFIG_KEY_NATIVE_CONTAINER_BUILD.stream().noneMatch(key -> Boolean.parseBoolean(quarkusProperties.getProperty(key)))) {
LOGGER.info("Quarkus build is not in-container");
return false;
}
return true;
}
private boolean isPackagingTypeSupported(Properties quarkusProperties) {
if(QUARKUS_CACHEABLE_PACKAGE_TYPES.stream().noneMatch(key -> key.equals(quarkusProperties.getProperty(QUARKUS_CONFIG_KEY_PACKAGE_TYPE)))) {
LOGGER.info("Quarkus package type is not cacheable");
return false;
}
return true;
}
private void configureInputs(MojoMetadataProvider.Context context, Properties quarkusProperties) {
context.inputs(inputs -> {
addOsInputs(inputs);
addClasspathInput(context, inputs);
addMojoInputs(inputs);
addQuarkusPropertiesInput(inputs);
addQuarkusFilesInputs(inputs, quarkusProperties);
});
}
private void addOsInputs(MojoMetadataProvider.Context.Inputs inputs) {
inputs.property("osName", System.getProperty("os.name"))
.property("osVersion", System.getProperty("os.version"))
.property("osArch", System.getProperty("os.arch"));
}
private void addClasspathInput(MojoMetadataProvider.Context context, MojoMetadataProvider.Context.Inputs inputs) {
try {
List<String> compileClasspathElements = context.getProject().getCompileClasspathElements();
inputs.fileSet("quarkusCompileClasspath", compileClasspathElements, fileSet -> fileSet.normalizationStrategy(MojoMetadataProvider.Context.FileSet.NormalizationStrategy.CLASSPATH));
} catch (DependencyResolutionRequiredException e) {
throw new IllegalStateException("Classpath can't be resolved");
}
}
private void addMojoInputs(MojoMetadataProvider.Context.Inputs inputs) {
inputs
.fileSet("generatedSourcesDirectory", fileSet -> {})
.properties("appArtifact", "closeBootstrappedApp", "finalName", "ignoredEntries", "manifestEntries", "manifestSections", "skip", "skipOriginalJarRename", "systemProperties", "properties")
.ignore("project", "buildDir", "mojoExecution", "session", "repoSession", "repos", "pluginRepos", "attachRunnerAsMainArtifact", "bootstrapId", "buildDirectory");
}
private void addQuarkusPropertiesInput(MojoMetadataProvider.Context.Inputs inputs) {
inputs.fileSet("quarkusCopyProperties", new File("target/quarkus-app-prod-config-dump"), fileSet -> fileSet.normalizationStrategy(MojoMetadataProvider.Context.FileSet.NormalizationStrategy.RELATIVE_PATH));
inputs.fileSet("generatedSourcesDirectory", fileSet -> {});
}
private void addQuarkusFilesInputs(MojoMetadataProvider.Context.Inputs inputs, Properties quarkusProperties) {
for(String quarkusFilePropertyKey : QUARKUS_KEYS_AS_FILE_INPUTS) {
String quarkusFilePropertyValue = quarkusProperties.getProperty(quarkusFilePropertyKey);
if(Utils.isNotEmpty(quarkusFilePropertyValue)) {
inputs.fileSet(quarkusFilePropertyKey, new File(quarkusFilePropertyValue), fileSet -> fileSet.normalizationStrategy(MojoMetadataProvider.Context.FileSet.NormalizationStrategy.RELATIVE_PATH));
}
}
}
private void configureOutputs(MojoMetadataProvider.Context context) {
context.outputs(outputs -> {
String quarkusExeFileName = "target/" + context.getProject().getBuild().getFinalName() + "-runner";
String quarkusJarFileName = "target/" + context.getProject().getBuild().getFinalName() + ".jar";
String quarkusUberJarFileName = "target/" + context.getProject().getBuild().getFinalName() + "-runner.jar";
outputs.cacheable("this plugin has CPU-bound goals with well-defined inputs and outputs");
outputs.file("quarkusExe", quarkusExeFileName);
outputs.file("quarkusJar", quarkusJarFileName);
outputs.file("quarkusUberJar", quarkusUberJarFileName);
// Do not declare dump config as goal output to avoid
// Goal execution marked as not cacheable: Build caching was not enabled for this goal execution because pre-existing files were modified. Cacheable goals may only create new files.
// outputs.file("quarkusProperties", QUARKUS_FILE_PROPERTIES_DUMP);
});
}
}