Skip to content

Commit

Permalink
Merge pull request #25802 from aloubyansky/extension-excluded-resources
Browse files Browse the repository at this point in the history
Extension excluded resources in quarkus-extension.properties
  • Loading branch information
aloubyansky authored May 31, 2022
2 parents 68d4dc6 + f8eeaa1 commit 310485e
Show file tree
Hide file tree
Showing 24 changed files with 592 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.GACT;

/**
* Represents resources to be removed from a dependency when packaging the application.
*/
public final class RemovedResourceBuildItem extends MultiBuildItem {

private final GACT artifact;
private final ArtifactKey artifact;
private final Set<String> resources;

@Deprecated
public RemovedResourceBuildItem(ArtifactKey artifact, Set<String> resources) {
this.artifact = new GACT(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), artifact.getType());
this.artifact = artifact;
this.resources = resources;
}

/**
* @deprecated In favor of {@link #RemovedResourceBuildItem(ArtifactKey, Set)}
* @param artifact artifact key
* @param resources resources to be removed from the application
*/
@Deprecated(forRemoval = true)
public RemovedResourceBuildItem(GACT artifact, Set<String> resources) {
this.artifact = artifact;
this.resources = resources;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package io.quarkus.deployment.steps;

import java.util.Map;
import java.util.Set;

import io.quarkus.deployment.BootstrapConfig;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.AppModelProviderBuildItem;
import io.quarkus.deployment.builditem.RemovedResourceBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.maven.dependency.ArtifactKey;

public class CurateOutcomeBuildStep {

Expand All @@ -13,4 +19,15 @@ public class CurateOutcomeBuildStep {
CurateOutcomeBuildItem curateOutcome(AppModelProviderBuildItem appModelProvider) {
return new CurateOutcomeBuildItem(appModelProvider.validateAndGet(config));
}

@BuildStep
void removeResources(CurateOutcomeBuildItem curateOutcome,
BuildProducer<RemovedResourceBuildItem> removedResourceProducer) {
final Map<ArtifactKey, Set<String>> excludedResources = curateOutcome.getApplicationModel().getRemovedResources();
if (!excludedResources.isEmpty()) {
for (Map.Entry<ArtifactKey, Set<String>> removed : excludedResources.entrySet()) {
removedResourceProducer.produce(new RemovedResourceBuildItem(removed.getKey(), removed.getValue()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.quarkus.deployment.builditem.nativeimage.NativeImageAllowIncompleteClasspathBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.maven.dependency.GACT;
import io.quarkus.maven.dependency.ArtifactKey;

/**
* The Oracle JDBC driver includes a {@literal META-INF/native-image} which enables a set
Expand Down Expand Up @@ -118,13 +118,13 @@ NativeImageAllowIncompleteClasspathBuildItem naughtyDriver() {

@BuildStep
RemovedResourceBuildItem overrideSubstitutions() {
return new RemovedResourceBuildItem(GACT.fromString("com.oracle.database.jdbc:ojdbc11"),
return new RemovedResourceBuildItem(ArtifactKey.fromString("com.oracle.database.jdbc:ojdbc11"),
Collections.singleton("oracle/nativeimage/Target_java_io_ObjectStreamClass.class"));
}

@BuildStep
RemovedResourceBuildItem enhancedCharsetSubstitutions() {
return new RemovedResourceBuildItem(GACT.fromString("com.oracle.database.jdbc:ojdbc11"),
return new RemovedResourceBuildItem(ArtifactKey.fromString("com.oracle.database.jdbc:ojdbc11"),
Collections.singleton("oracle/nativeimage/CharacterSetFeature.class"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,10 @@ public Set<ArtifactKey> getLowerPriorityArtifacts() {
public Set<ArtifactKey> getReloadableWorkspaceDependencies() {
return new HashSet<>(localProjectArtifacts);
}

@Override
public Map<ArtifactKey, Set<String>> getRemovedResources() {
// not supported
return Map.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ default Map<String, String> getPlatformProperties() {

Set<ArtifactKey> getReloadableWorkspaceDependencies();

/**
* Resources that should be removed from the classpath.
*
* @return resources that should be removed from the classpath
*/
Map<ArtifactKey, Set<String>> getRemovedResources();

default WorkspaceModule getApplicationModule() {
return getAppArtifact().getWorkspaceModule();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ApplicationModelBuilder {
public static final String PARENT_FIRST_ARTIFACTS = "parent-first-artifacts";
public static final String RUNNER_PARENT_FIRST_ARTIFACTS = "runner-parent-first-artifacts";
public static final String EXCLUDED_ARTIFACTS = "excluded-artifacts";
public static final String REMOVED_RESOURCES_DOT = "removed-resources.";
public static final String LESSER_PRIORITY_ARTIFACTS = "lesser-priority-artifacts";

private static final Logger log = Logger.getLogger(ApplicationModelBuilder.class);
Expand All @@ -35,6 +36,7 @@ public class ApplicationModelBuilder {
final Set<ArtifactKey> parentFirstArtifacts = new HashSet<>();
final Set<ArtifactKey> runnerParentFirstArtifacts = new HashSet<>();
final Set<ArtifactKey> excludedArtifacts = new HashSet<>();
final Map<ArtifactKey, Set<String>> excludedResources = new HashMap<>(0);
final Set<ArtifactKey> lesserPriorityArtifacts = new HashSet<>();
final Set<ArtifactKey> reloadableWorkspaceModules = new HashSet<>();
final List<ExtensionCapabilities> extensionCapabilities = new ArrayList<>();
Expand Down Expand Up @@ -102,6 +104,11 @@ public ApplicationModelBuilder addExcludedArtifacts(List<ArtifactKey> deps) {
return this;
}

public ApplicationModelBuilder addRemovedResources(ArtifactKey key, Set<String> resources) {
this.excludedResources.computeIfAbsent(key, k -> new HashSet<>(resources.size())).addAll(resources);
return this;
}

public ApplicationModelBuilder addLesserPriorityArtifact(ArtifactKey deps) {
this.lesserPriorityArtifacts.add(deps);
return this;
Expand Down Expand Up @@ -134,34 +141,69 @@ public WorkspaceModule.Mutable getOrCreateProjectModule(WorkspaceModuleId id, Fi
* @param props The quarkus-extension.properties file
*/
public void handleExtensionProperties(Properties props, String extension) {
String parentFirst = props.getProperty(PARENT_FIRST_ARTIFACTS);
if (parentFirst != null) {
String[] artifacts = parentFirst.split(",");
for (String artifact : artifacts) {
parentFirstArtifacts.add(new GACT(artifact.split(":")));
for (Map.Entry<Object, Object> prop : props.entrySet()) {
if (prop.getValue() == null) {
continue;
}
}
String runnerParentFirst = props.getProperty(RUNNER_PARENT_FIRST_ARTIFACTS);
if (runnerParentFirst != null) {
String[] artifacts = runnerParentFirst.split(",");
for (String artifact : artifacts) {
runnerParentFirstArtifacts.add(new GACT(artifact.split(":")));
final String value = prop.getValue().toString();
if (value.isBlank()) {
continue;
}
}
String excluded = props.getProperty(EXCLUDED_ARTIFACTS);
if (excluded != null) {
String[] artifacts = excluded.split(",");
for (String artifact : artifacts) {
excludedArtifacts.add(new GACT(artifact.split(":")));
log.debugf("Extension %s is excluding %s", extension, artifact);
}
}
String lesserPriority = props.getProperty(LESSER_PRIORITY_ARTIFACTS);
if (lesserPriority != null) {
String[] artifacts = lesserPriority.split(",");
for (String artifact : artifacts) {
lesserPriorityArtifacts.add(new GACT(artifact.split(":")));
log.debugf("Extension %s is making %s a lesser priority artifact", extension, artifact);
final String name = prop.getKey().toString();
switch (name) {
case PARENT_FIRST_ARTIFACTS:
for (String artifact : value.split(",")) {
parentFirstArtifacts.add(new GACT(artifact.split(":")));
}
break;
case RUNNER_PARENT_FIRST_ARTIFACTS:
for (String artifact : value.split(",")) {
runnerParentFirstArtifacts.add(new GACT(artifact.split(":")));
}
break;
case EXCLUDED_ARTIFACTS:
for (String artifact : value.split(",")) {
excludedArtifacts.add(new GACT(artifact.split(":")));
log.debugf("Extension %s is excluding %s", extension, artifact);
}
break;
case LESSER_PRIORITY_ARTIFACTS:
String[] artifacts = value.split(",");
for (String artifact : artifacts) {
lesserPriorityArtifacts.add(new GACT(artifact.split(":")));
log.debugf("Extension %s is making %s a lesser priority artifact", extension, artifact);
}
break;
default:
if (name.startsWith(REMOVED_RESOURCES_DOT)) {
final String keyStr = name.substring(REMOVED_RESOURCES_DOT.length());
if (!keyStr.isBlank()) {
ArtifactKey key = null;
try {
key = ArtifactKey.fromString(keyStr);
} catch (IllegalArgumentException e) {
log.warnf("Failed to parse artifact key %s in %s from descriptor of extension %s", keyStr, name,
extension);
}
if (key != null) {
final Set<String> resources;
Collection<String> existingResources = excludedResources.get(key);
if (existingResources == null || existingResources.isEmpty()) {
resources = Set.of(value.split(","));
} else {
final String[] split = value.split(",");
resources = new HashSet<>(existingResources.size() + split.length);
resources.addAll(existingResources);
for (String s : split) {
resources.add(s);
}
}
log.debugf("Extension %s is excluding resources %s from artifact %s", extension, resources,
key);
excludedResources.put(key, resources);
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DefaultApplicationModel implements ApplicationModel, Serializable {
Expand All @@ -19,6 +20,7 @@ public class DefaultApplicationModel implements ApplicationModel, Serializable {
private final Set<ArtifactKey> runnerParentFirstArtifacts;
private final Set<ArtifactKey> lesserPriorityArtifacts;
private final Set<ArtifactKey> localProjectArtifacts;
private final Map<ArtifactKey, Set<String>> excludedResources;

public DefaultApplicationModel(ApplicationModelBuilder builder) {
this.appArtifact = builder.appArtifact;
Expand All @@ -29,6 +31,7 @@ public DefaultApplicationModel(ApplicationModelBuilder builder) {
this.runnerParentFirstArtifacts = builder.runnerParentFirstArtifacts;
this.lesserPriorityArtifacts = builder.lesserPriorityArtifacts;
this.localProjectArtifacts = builder.reloadableWorkspaceModules;
this.excludedResources = builder.excludedResources;
}

@Override
Expand Down Expand Up @@ -70,4 +73,9 @@ public Set<ArtifactKey> getLowerPriorityArtifacts() {
public Set<ArtifactKey> getReloadableWorkspaceDependencies() {
return localProjectArtifacts;
}

@Override
public Map<ArtifactKey, Set<String>> getRemovedResources() {
return excludedResources;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand All @@ -33,6 +34,7 @@ public class MutableJarApplicationModel implements Serializable {
private Set<ArtifactKey> runnerParentFirstArtifacts;
private Set<ArtifactKey> lesserPriorityArtifacts;
private Set<ArtifactKey> localProjectArtifacts;
private Map<ArtifactKey, Set<String>> excludedResources;
private Collection<ExtensionCapabilities> capabilitiesContracts;
private PlatformImports platformImports;
private String userProvidersDirectory;
Expand All @@ -51,6 +53,7 @@ public MutableJarApplicationModel(String baseName, Map<ArtifactKey, List<String>
parentFirstArtifacts = new HashSet<>(appModel.getParentFirst());
runnerParentFirstArtifacts = new HashSet<>(appModel.getRunnerParentFirst());
lesserPriorityArtifacts = new HashSet<>(appModel.getLowerPriorityArtifacts());
excludedResources = new HashMap<>(appModel.getRemovedResources());
capabilitiesContracts = new ArrayList<>(appModel.getExtensionCapabilities());
this.platformImports = appModel.getPlatforms();
}
Expand All @@ -77,6 +80,9 @@ public ApplicationModel getAppModel(Path root) {
for (ArtifactKey i : localProjectArtifacts) {
model.addReloadableWorkspaceModule(i);
}
for (Map.Entry<ArtifactKey, Set<String>> i : excludedResources.entrySet()) {
model.addRemovedResources(i.getKey(), i.getValue());
}
for (ExtensionCapabilities ec : capabilitiesContracts) {
model.addExtensionCapabilities(ec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -108,6 +107,17 @@ public ConfiguredClassLoading build() {
}
}
}

if (!appModel.getRemovedResources().isEmpty()) {
for (Map.Entry<ArtifactKey, Set<String>> e : appModel.getRemovedResources().entrySet()) {
Collection<String> resources = removedResources.get(e.getKey());
if (resources == null) {
removedResources.put(e.getKey(), e.getValue());
} else {
resources.addAll(e.getValue());
}
}
}
}

return ConfiguredClassLoading.this;
Expand Down Expand Up @@ -135,7 +145,11 @@ private void collectRemovedResources(String baseConfigKey, Properties properties
if (key.startsWith(baseConfigKey)) {
String artifactId = key.substring(baseConfigKey.length());
artifactId = artifactId.replace("\"", "");
List<String> resources = Arrays.asList(value.split(","));
final String[] split = value.split(",");
List<String> resources = new ArrayList<>(split.length);
for (String s : split) {
resources.add(s);
}
removedResources.put(new GACT(artifactId.split(":")), resources);
}
}
Expand Down
Loading

0 comments on commit 310485e

Please sign in to comment.