Move Features to api (#1232)

This commit is contained in:
Guillaume Nodet 2023-09-11 00:42:53 +02:00 committed by GitHub
parent dde369b6fc
commit 7d6f90e8d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 87 deletions

View File

@ -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<String, String> 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<String, ?> 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;
}
}
}

View File

@ -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<Artifact> artifacts) {
return artifacts.stream().anyMatch(a -> CONSUMER_POM_CLASSIFIER.equals(a.getClassifier()));
}

View File

@ -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());

View File

@ -32,10 +32,13 @@ under the License.
<description>The effective model builder, with inheritance, profile activation, interpolation, ...</description>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-xml-impl</artifactId>
<version>4.0.0-alpha-8-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>

View File

@ -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<String, String> userProperties) {
return new Feature(userProperties, "maven.experimental.buildconsumer", "true");
}
private static Map<String, String> 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<String, String> 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;
}
}
}

View File

@ -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();