From 4b66d166fa91abcb4fedae4be9c1f04899b9538f Mon Sep 17 00:00:00 2001 From: Alexey Loubyansky Date: Wed, 21 Jul 2021 22:51:39 +0200 Subject: [PATCH] Common utility to parse conditional dependency config --- .../ConditionalDependenciesEnabler.java | 25 ++++++++------- .../bootstrap/util/BootstrapUtils.java | 31 +++++++++++++++++++ .../DeploymentInjectingDependencyVisitor.java | 16 +++------- 3 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/util/BootstrapUtils.java diff --git a/devtools/gradle/src/main/java/io/quarkus/gradle/dependency/ConditionalDependenciesEnabler.java b/devtools/gradle/src/main/java/io/quarkus/gradle/dependency/ConditionalDependenciesEnabler.java index c90b0a9167590..51638cf230dc3 100644 --- a/devtools/gradle/src/main/java/io/quarkus/gradle/dependency/ConditionalDependenciesEnabler.java +++ b/devtools/gradle/src/main/java/io/quarkus/gradle/dependency/ConditionalDependenciesEnabler.java @@ -7,6 +7,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -25,6 +26,7 @@ import io.quarkus.bootstrap.BootstrapConstants; import io.quarkus.bootstrap.model.AppArtifactCoords; import io.quarkus.bootstrap.model.AppArtifactKey; +import io.quarkus.bootstrap.util.BootstrapUtils; import io.quarkus.bootstrap.util.ZipUtils; public class ConditionalDependenciesEnabler { @@ -191,20 +193,21 @@ private ExtensionDependency loadExtensionInfo(Path descriptorPath, ModuleVersion } AppArtifactCoords deploymentModule = AppArtifactCoords .fromString(extensionProperties.getProperty(BootstrapConstants.PROP_DEPLOYMENT_ARTIFACT)); - List conditionalDependencies = new ArrayList<>(); + final List conditionalDependencies; if (extensionProperties.containsKey(BootstrapConstants.CONDITIONAL_DEPENDENCIES)) { - String conditionalDeps = extensionProperties.get(BootstrapConstants.CONDITIONAL_DEPENDENCIES).toString(); - for (String conditionalDep : conditionalDeps.split("\\s+")) { + final String[] deps = BootstrapUtils + .splitByWhitespace(extensionProperties.getProperty(BootstrapConstants.CONDITIONAL_DEPENDENCIES)); + conditionalDependencies = new ArrayList<>(deps.length); + for (String conditionalDep : deps) { conditionalDependencies.add(DependencyUtils.create(project.getDependencies(), conditionalDep)); } + } else { + conditionalDependencies = Collections.emptyList(); } - List constraints = new ArrayList<>(); - if (extensionProperties.containsKey(BootstrapConstants.DEPENDENCY_CONDITION)) { - String constraintDeps = extensionProperties.getProperty(BootstrapConstants.DEPENDENCY_CONDITION); - for (String constraint : constraintDeps.split("\\s+")) { - constraints.add(AppArtifactKey.fromString(constraint)); - } - } - return new ExtensionDependency(exentionId, deploymentModule, conditionalDependencies, constraints); + + final AppArtifactKey[] constraints = BootstrapUtils + .parseDependencyCondition(extensionProperties.getProperty(BootstrapConstants.DEPENDENCY_CONDITION)); + return new ExtensionDependency(exentionId, deploymentModule, conditionalDependencies, + constraints == null ? Collections.emptyList() : Arrays.asList(constraints)); } } diff --git a/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/util/BootstrapUtils.java b/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/util/BootstrapUtils.java new file mode 100644 index 0000000000000..cbd1fb039aa3f --- /dev/null +++ b/independent-projects/bootstrap/app-model/src/main/java/io/quarkus/bootstrap/util/BootstrapUtils.java @@ -0,0 +1,31 @@ +package io.quarkus.bootstrap.util; + +import io.quarkus.bootstrap.model.AppArtifactKey; +import java.util.regex.Pattern; + +public class BootstrapUtils { + + private static Pattern splitByWs; + + public static String[] splitByWhitespace(String s) { + if (s == null) { + return null; + } + if (splitByWs == null) { + splitByWs = Pattern.compile("\\s+"); + } + return splitByWs.split(s); + } + + public static AppArtifactKey[] parseDependencyCondition(String s) { + final String[] strArr = splitByWhitespace(s); + if (strArr == null) { + return null; + } + final AppArtifactKey[] keys = new AppArtifactKey[strArr.length]; + for (int i = 0; i < strArr.length; ++i) { + keys[i] = AppArtifactKey.fromString(strArr[i]); + } + return keys; + } +} diff --git a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/DeploymentInjectingDependencyVisitor.java b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/DeploymentInjectingDependencyVisitor.java index bac243760d0c8..1d544fc8660dd 100644 --- a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/DeploymentInjectingDependencyVisitor.java +++ b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/DeploymentInjectingDependencyVisitor.java @@ -9,6 +9,7 @@ import io.quarkus.bootstrap.model.CapabilityContract; import io.quarkus.bootstrap.model.PathsCollection; import io.quarkus.bootstrap.resolver.AppModelResolverException; +import io.quarkus.bootstrap.util.BootstrapUtils; import io.quarkus.bootstrap.util.DependencyNodeUtils; import io.quarkus.bootstrap.util.ZipUtils; import java.io.BufferedReader; @@ -438,7 +439,7 @@ private class ExtensionInfo { value = props.getProperty(BootstrapConstants.CONDITIONAL_DEPENDENCIES); if (value != null) { - final String[] deps = value.split("\\s+"); + final String[] deps = BootstrapUtils.splitByWhitespace(value); conditionalDeps = new Artifact[deps.length]; for (int i = 0; i < deps.length; ++i) { try { @@ -452,17 +453,8 @@ private class ExtensionInfo { conditionalDeps = NO_ARTIFACTS; } - value = props.getProperty(BootstrapConstants.DEPENDENCY_CONDITION); - if (value != null) { - final String[] split = value.split("\\s+"); - dependencyCondition = new AppArtifactKey[split.length]; - for (int i = 0; i < split.length; ++i) { - final String trigger = split[i]; - dependencyCondition[i] = AppArtifactKey.fromString(trigger); - } - } else { - dependencyCondition = null; - } + dependencyCondition = BootstrapUtils + .parseDependencyCondition(props.getProperty(BootstrapConstants.DEPENDENCY_CONDITION)); } void ensureActivated() {