-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ApplicationArchiveBuildStep.java
331 lines (302 loc) · 15.4 KB
/
ApplicationArchiveBuildStep.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package io.quarkus.deployment.index;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
import io.quarkus.bootstrap.classloading.ClassPathElement;
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.deployment.ApplicationArchive;
import io.quarkus.deployment.ApplicationArchiveImpl;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.AdditionalApplicationArchiveBuildItem;
import io.quarkus.deployment.builditem.AdditionalApplicationArchiveMarkerBuildItem;
import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem;
import io.quarkus.deployment.builditem.ApplicationIndexBuildItem;
import io.quarkus.deployment.builditem.ArchiveRootBuildItem;
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import io.quarkus.deployment.builditem.QuarkusBuildCloseablesBuildItem;
import io.quarkus.deployment.configuration.ClassLoadingConfig;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.GACT;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.paths.DirectoryPathTree;
import io.quarkus.paths.MultiRootPathTree;
import io.quarkus.paths.OpenPathTree;
import io.quarkus.paths.PathTree;
import io.quarkus.paths.PathVisit;
import io.quarkus.paths.PathVisitor;
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
public class ApplicationArchiveBuildStep {
private static final Logger LOGGER = Logger.getLogger(ApplicationArchiveBuildStep.class);
IndexDependencyConfiguration config;
@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
static final class IndexDependencyConfiguration {
/**
* Artifacts on the classpath that should also be indexed.
* <p>
* Their classes will be in the index and processed by Quarkus processors.
*/
@ConfigItem(name = ConfigItem.PARENT)
@ConfigDocSection
@ConfigDocMapKey("dependency-name")
Map<String, IndexDependencyConfig> indexDependency;
}
@BuildStep
void addConfiguredIndexedDependencies(BuildProducer<IndexDependencyBuildItem> indexDependencyBuildItemBuildProducer) {
for (IndexDependencyConfig indexDependencyConfig : config.indexDependency.values()) {
indexDependencyBuildItemBuildProducer.produce(new IndexDependencyBuildItem(indexDependencyConfig.groupId,
indexDependencyConfig.artifactId, indexDependencyConfig.classifier.orElse(null)));
}
}
@BuildStep
ApplicationArchivesBuildItem build(
QuarkusBuildCloseablesBuildItem buildCloseables,
ArchiveRootBuildItem root, ApplicationIndexBuildItem appindex,
List<AdditionalApplicationArchiveMarkerBuildItem> appMarkers,
List<AdditionalApplicationArchiveBuildItem> additionalApplicationArchiveBuildItem,
List<IndexDependencyBuildItem> indexDependencyBuildItems,
LiveReloadBuildItem liveReloadContext,
CurateOutcomeBuildItem curateOutcomeBuildItem,
ClassLoadingConfig classLoadingConfig) throws IOException {
IndexCache indexCache = liveReloadContext.getContextObject(IndexCache.class);
if (indexCache == null) {
indexCache = new IndexCache();
liveReloadContext.setContextObject(IndexCache.class, indexCache);
}
Map<ArtifactKey, Set<String>> removedResources = new HashMap<>();
for (Map.Entry<String, Set<String>> entry : classLoadingConfig.removedResources.entrySet()) {
removedResources.put(new GACT(entry.getKey().split(":")), entry.getValue());
}
// Add resources removed from the classpath by extensions
removedResources.putAll(curateOutcomeBuildItem.getApplicationModel().getRemovedResources());
List<ApplicationArchive> applicationArchives = scanForOtherIndexes(buildCloseables,
appMarkers, root, additionalApplicationArchiveBuildItem, indexDependencyBuildItems, indexCache,
curateOutcomeBuildItem, removedResources);
final OpenPathTree tree;
if (root.getRootDirectories().size() == 1) {
tree = new DirectoryPathTree(root.getRootDirectories().iterator().next());
} else {
final PathTree[] trees = new PathTree[root.getRootDirectories().size()];
int i = 0;
for (Path p : root.getRootDirectories()) {
trees[i++] = new DirectoryPathTree(p);
}
tree = new MultiRootPathTree(trees);
}
return new ApplicationArchivesBuildItem(
new ApplicationArchiveImpl(appindex.getIndex(), tree,
curateOutcomeBuildItem.getApplicationModel().getAppArtifact().getKey()),
applicationArchives);
}
private List<ApplicationArchive> scanForOtherIndexes(QuarkusBuildCloseablesBuildItem buildCloseables,
List<AdditionalApplicationArchiveMarkerBuildItem> appMarkers,
ArchiveRootBuildItem root, List<AdditionalApplicationArchiveBuildItem> additionalApplicationArchives,
List<IndexDependencyBuildItem> indexDependencyBuildItem, IndexCache indexCache,
CurateOutcomeBuildItem curateOutcomeBuildItem, Map<ArtifactKey, Set<String>> removedResources)
throws IOException {
List<ApplicationArchive> appArchives = new ArrayList<>();
Set<Path> indexedPaths = new HashSet<>();
//get paths that are included via marker files
final Set<String> markers = new HashSet<>(appMarkers.size() + 1);
for (AdditionalApplicationArchiveMarkerBuildItem i : appMarkers) {
final String marker = i.getFile();
markers.add(marker.endsWith("/") ? marker.substring(0, marker.length() - 1) : marker);
}
markers.add(IndexingUtil.JANDEX_INDEX);
addMarkerFilePaths(markers, root, curateOutcomeBuildItem, indexedPaths, appArchives, buildCloseables,
indexCache, removedResources);
//get paths that are included via index-dependencies
addIndexDependencyPaths(indexDependencyBuildItem, root, indexedPaths, appArchives, buildCloseables,
indexCache, curateOutcomeBuildItem, removedResources);
for (AdditionalApplicationArchiveBuildItem i : additionalApplicationArchives) {
for (Path apPath : i.getResolvedPaths()) {
if (!root.getResolvedPaths().contains(apPath) && indexedPaths.add(apPath)) {
appArchives.add(createApplicationArchive(buildCloseables, indexCache, apPath, null,
removedResources));
}
}
}
return appArchives;
}
private void addIndexDependencyPaths(List<IndexDependencyBuildItem> indexDependencyBuildItems, ArchiveRootBuildItem root,
Set<Path> indexedDeps, List<ApplicationArchive> appArchives,
QuarkusBuildCloseablesBuildItem buildCloseables, IndexCache indexCache,
CurateOutcomeBuildItem curateOutcomeBuildItem,
Map<ArtifactKey, Set<String>> removedResources) {
if (indexDependencyBuildItems.isEmpty()) {
return;
}
final Collection<ResolvedDependency> userDeps = curateOutcomeBuildItem.getApplicationModel()
.getRuntimeDependencies();
final Map<ArtifactKey, ResolvedDependency> userMap = new HashMap<>(userDeps.size());
for (ResolvedDependency dep : userDeps) {
userMap.put(dep.getKey(), dep);
}
try {
for (IndexDependencyBuildItem indexDependencyBuildItem : indexDependencyBuildItems) {
final ArtifactKey key = ArtifactKey.of(indexDependencyBuildItem.getGroupId(),
indexDependencyBuildItem.getArtifactId(),
indexDependencyBuildItem.getClassifier(),
ArtifactCoords.TYPE_JAR);
final ResolvedDependency artifact = userMap.get(key);
if (artifact == null) {
throw new RuntimeException(
"Could not resolve artifact " + key + " among the runtime dependencies of the application");
}
for (Path path : artifact.getContentTree().getRoots()) {
if (!root.isExcludedFromIndexing(path) && !root.getResolvedPaths().contains(path)
&& indexedDeps.add(path)) {
appArchives.add(createApplicationArchive(buildCloseables, indexCache, path, key,
removedResources));
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static ApplicationArchive createApplicationArchive(QuarkusBuildCloseablesBuildItem buildCloseables,
IndexCache indexCache, Path dep, ArtifactKey artifactKey, Map<ArtifactKey, Set<String>> removedResources)
throws IOException {
LOGGER.debugf("Indexing dependency: %s", dep);
final Set<String> removed = removedResources.get(artifactKey);
final OpenPathTree openTree;
final IndexView index;
if (Files.isDirectory(dep)) {
openTree = new DirectoryPathTree(dep);
index = indexPathTree(openTree, removed);
} else {
openTree = buildCloseables.add(PathTree.ofArchive(dep).open());
index = handleJarPath(dep, indexCache, removed);
}
return new ApplicationArchiveImpl(index, openTree, artifactKey);
}
private static void addMarkerFilePaths(Set<String> applicationArchiveMarkers,
ArchiveRootBuildItem root, CurateOutcomeBuildItem curateOutcomeBuildItem, Set<Path> indexedPaths,
List<ApplicationArchive> appArchives, QuarkusBuildCloseablesBuildItem buildCloseables,
IndexCache indexCache, Map<ArtifactKey, Set<String>> removed)
throws IOException {
final QuarkusClassLoader cl = ((QuarkusClassLoader) Thread.currentThread().getContextClassLoader());
final Set<ArtifactKey> indexedElements = new HashSet<>();
for (String marker : applicationArchiveMarkers) {
final List<ClassPathElement> elements = cl.getElementsWithResource(marker, false);
if (elements.isEmpty()) {
continue;
}
for (ClassPathElement cpe : elements) {
if (!cpe.isRuntime()) {
continue;
}
final ArtifactKey dependencyKey = cpe.getDependencyKey();
if (dependencyKey == null || !indexedElements.add(dependencyKey)) {
continue;
}
cpe.apply(tree -> {
indexedPaths.addAll(tree.getOriginalTree().getRoots());
final Path rootPath = tree.getOriginalTree().getRoots().size() == 1
? tree.getOriginalTree().getRoots().iterator().next()
: null;
if (rootPath != null && !Files.isDirectory(rootPath)) {
if (root.isExcludedFromIndexing(rootPath)) {
return null;
}
Index index = indexCache.cache.get(rootPath);
if (index == null) {
try {
index = IndexingUtil.indexTree(tree,
dependencyKey == null ? Collections.emptySet() : removed.get(dependencyKey));
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
indexCache.cache.put(rootPath, index);
}
appArchives.add(new ApplicationArchiveImpl(index, tree, dependencyKey));
return null;
}
final ApplicationArchive archive = tree.apply(marker, visit -> {
if (visit == null || root.isExcludedFromIndexing(visit.getRoot())) {
return null;
}
final List<IndexView> indexes = new ArrayList<>(tree.getRoots().size());
try {
indexes.add(indexPathTree(tree, removed.get(dependencyKey)));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return new ApplicationArchiveImpl(
indexes.size() == 1 ? indexes.get(0) : CompositeIndex.create(indexes),
tree, dependencyKey);
});
if (archive != null) {
appArchives.add(archive);
}
return null;
});
}
}
}
private static Index indexPathTree(PathTree tree, Set<String> removed) throws IOException {
Indexer indexer = new Indexer();
tree.walk(new PathVisitor() {
@Override
public void visitPath(PathVisit visit) {
final Path path = visit.getPath();
final Path fileName = path.getFileName();
if (fileName == null
|| !fileName.toString().endsWith(".class")
|| Files.isDirectory(path)
|| removed != null && removed.contains(visit.getRelativePath("/"))) {
return;
}
try (InputStream in = Files.newInputStream(path)) {
indexer.index(in);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
return indexer.complete();
}
private static Index handleJarPath(Path path, IndexCache indexCache, Set<String> removed) {
return indexCache.cache.computeIfAbsent(path, new Function<Path, Index>() {
@Override
public Index apply(Path path) {
try {
return IndexingUtil.indexJar(path, removed);
} catch (IOException e) {
throw new RuntimeException("Failed to process " + path, e);
}
}
});
}
/**
* When running in hot deployment mode we know that java archives will never change, there is no need
* to re-index them each time. We cache them here to reduce the hot reload time.
*/
private static final class IndexCache {
final Map<Path, Index> cache = new HashMap<>();
}
}