diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/feature/Features.java b/api/maven-api-core/src/main/java/org/apache/maven/api/feature/Features.java new file mode 100644 index 0000000000..04ab33d864 --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/feature/Features.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.api.feature; + +import java.util.Map; +import java.util.Properties; + +import org.apache.maven.api.Session; +import org.apache.maven.api.annotations.Nullable; + +/** + * Centralized class for Maven Core feature information. + * Features configured are supposed to be final in a given maven session. + * + * @since 4.0.0 + */ +public final class Features { + + /** + * Name of the Maven user property to enable or disable the build/consumer POM feature. + */ + public static final String BUILDCONSUMER = "maven.experimental.buildconsumer"; + + private Features() {} + + /** + * Check if the build/consumer POM feature is active. + */ + public static boolean buildConsumer(@Nullable Properties userProperties) { + return doGet(userProperties, BUILDCONSUMER, true); + } + + /** + * Check if the build/consumer POM feature is active. + */ + public static boolean buildConsumer(@Nullable Map userProperties) { + return doGet(userProperties, BUILDCONSUMER, true); + } + + /** + * Check if the build/consumer POM feature is active. + */ + public static boolean buildConsumer(@Nullable Session session) { + return buildConsumer(session != null ? session.getUserProperties() : null); + } + + private static boolean doGet(Properties userProperties, String key, boolean def) { + return doGet(userProperties != null ? userProperties.get(key) : null, def); + } + + private static boolean doGet(Map userProperties, String key, boolean def) { + return doGet(userProperties != null ? userProperties.get(key) : null, def); + } + + private static boolean doGet(Object val, boolean def) { + if (val instanceof Boolean) { + return (Boolean) val; + } else if (val != null) { + return Boolean.parseBoolean(val.toString()); + } else { + return def; + } + } +} diff --git a/maven-core/src/main/java/org/apache/maven/internal/transformation/ConsumerPomArtifactTransformer.java b/maven-core/src/main/java/org/apache/maven/internal/transformation/ConsumerPomArtifactTransformer.java index 4f7f515c9e..41ac09d023 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/transformation/ConsumerPomArtifactTransformer.java +++ b/maven-core/src/main/java/org/apache/maven/internal/transformation/ConsumerPomArtifactTransformer.java @@ -36,7 +36,7 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; import java.util.function.BiConsumer; -import org.apache.maven.feature.Features; +import org.apache.maven.api.feature.Features; import org.apache.maven.model.building.DefaultBuildPomXMLFilterFactory; import org.apache.maven.model.building.TransformerContext; import org.apache.maven.model.transform.RawToConsumerPomXMLFilterFactory; @@ -68,7 +68,7 @@ public final class ConsumerPomArtifactTransformer { // If there is no build POM there is no reason to inject artifacts for the consumer POM. return; } - if (isActive(session)) { + if (Features.buildConsumer(session.getUserProperties())) { Path generatedFile; String buildDirectory = project.getBuild() != null ? project.getBuild().getDirectory() : null; @@ -103,23 +103,19 @@ public final class ConsumerPomArtifactTransformer { } public InstallRequest remapInstallArtifacts(RepositorySystemSession session, InstallRequest request) { - if (isActive(session) && consumerPomPresent(request.getArtifacts())) { + if (Features.buildConsumer(session.getUserProperties()) && consumerPomPresent(request.getArtifacts())) { request.setArtifacts(replacePom(request.getArtifacts())); } return request; } public DeployRequest remapDeployArtifacts(RepositorySystemSession session, DeployRequest request) { - if (isActive(session) && consumerPomPresent(request.getArtifacts())) { + if (Features.buildConsumer(session.getUserProperties()) && consumerPomPresent(request.getArtifacts())) { request.setArtifacts(replacePom(request.getArtifacts())); } return request; } - private boolean isActive(RepositorySystemSession session) { - return Features.buildConsumer(session.getUserProperties()).isActive(); - } - private boolean consumerPomPresent(Collection artifacts) { return artifacts.stream().anyMatch(a -> CONSUMER_POM_CLASSIFIER.equals(a.getClassifier())); } diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index b5effa9d06..1e1568e819 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -38,12 +38,12 @@ import java.util.Set; import java.util.stream.Collectors; import org.apache.maven.RepositoryUtils; +import org.apache.maven.api.feature.Features; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.InvalidArtifactRTException; import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.bridge.MavenRepositorySystem; -import org.apache.maven.feature.Features; import org.apache.maven.model.Build; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; @@ -390,7 +390,7 @@ public class DefaultProjectBuilder implements ProjectBuilder { Thread.currentThread().setContextClassLoader(oldContextClassLoader); } - if (Features.buildConsumer(request.getUserProperties()).isActive()) { + if (Features.buildConsumer(request.getUserProperties())) { request.getRepositorySession() .getData() .set(TransformerContext.KEY, config.transformerContextBuilder.build()); diff --git a/maven-model-builder/pom.xml b/maven-model-builder/pom.xml index d64e65cfa8..6e2975dd3b 100644 --- a/maven-model-builder/pom.xml +++ b/maven-model-builder/pom.xml @@ -32,10 +32,13 @@ under the License. The effective model builder, with inheritance, profile activation, interpolation, ... + + org.apache.maven + maven-api-core + org.apache.maven maven-xml-impl - 4.0.0-alpha-8-SNAPSHOT org.codehaus.plexus diff --git a/maven-model-builder/src/main/java/org/apache/maven/feature/Features.java b/maven-model-builder/src/main/java/org/apache/maven/feature/Features.java deleted file mode 100644 index 1cc6e6323e..0000000000 --- a/maven-model-builder/src/main/java/org/apache/maven/feature/Features.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.maven.feature; - -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.stream.Collectors; - -/** - * Centralized class for feature information - * - * @since 4.0.0 - */ -public final class Features { - private Features() {} - - public static Feature buildConsumer(Properties userProperties) { - return buildConsumer(toMap(userProperties)); - } - - public static Feature buildConsumer(Map userProperties) { - return new Feature(userProperties, "maven.experimental.buildconsumer", "true"); - } - - private static Map toMap(Properties properties) { - return properties.entrySet().stream() - .collect(Collectors.toMap( - e -> String.valueOf(e.getKey()), - e -> String.valueOf(e.getValue()), - (prev, next) -> next, - HashMap::new)); - } - - /** - * Represents some feature - * - * @since 4.0.0 - */ - public static class Feature { - private final boolean active; - - private final String name; - - Feature(Map userProperties, String name, String defaultValue) { - this.name = name; - this.active = "true".equals(userProperties.getOrDefault(name, defaultValue)); - } - - public boolean isActive() { - return active; - } - - public String propertyName() { - return name; - } - } -} diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index c3780525d3..9502c49ae0 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -41,13 +41,13 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; +import org.apache.maven.api.feature.Features; import org.apache.maven.api.model.Exclusion; import org.apache.maven.api.model.InputSource; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.building.Source; -import org.apache.maven.feature.Features; import org.apache.maven.model.Activation; import org.apache.maven.model.ActivationFile; import org.apache.maven.model.Build; @@ -1093,7 +1093,7 @@ public class DefaultModelBuilder implements ModelBuilder { } Model rawModel; - if (Features.buildConsumer(request.getUserProperties()).isActive() && modelSource instanceof FileModelSource) { + if (Features.buildConsumer(request.getUserProperties()) && modelSource instanceof FileModelSource) { rawModel = readFileModel(request, problems); File pomFile = ((FileModelSource) modelSource).getFile();