Skip to content

Commit

Permalink
Move Features to the api
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Sep 7, 2023
1 parent c5b4ace commit 1bb46cd
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 feature information
*
* @since 4.0.0
*/
public final class Features {

public static final String BUILDCONSUMER = "maven.experimental.buildconsumer";

private Features() {}

public static boolean buildConsumer(@Nullable Properties userProperties) {
return doGet(userProperties, BUILDCONSUMER, true);
}

public static boolean buildConsumer(@Nullable Map<String, String> userProperties) {
return doGet(userProperties, BUILDCONSUMER, true);
}

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
import java.util.stream.Collectors;

import org.apache.maven.api.Repository;
import org.apache.maven.api.feature.Features;
import org.apache.maven.api.model.DistributionManagement;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.ModelBase;
import org.apache.maven.api.model.Profile;
import org.apache.maven.feature.Features;
import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingRequest;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void injectTransformedArtifacts(MavenProject project, RepositorySystemSes
// 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 buildDir =
project.getBuild() != null ? Paths.get(project.getBuild().getDirectory()) : null;
if (buildDir != null) {
Expand Down Expand Up @@ -134,23 +134,19 @@ private void doDeleteFiles() {
}

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 -> "pom".equals(a.getExtension()) && CONSUMER_POM_CLASSIFIER.equals(a.getClassifier()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
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;
Expand Down Expand Up @@ -390,7 +390,7 @@ public List<ProjectBuildingResult> build(List<File> pomFiles, boolean recursive,
Thread.currentThread().setContextClassLoader(oldContextClassLoader);
}

if (Features.buildConsumer(request.getUserProperties()).isActive()) {
if (Features.buildConsumer(request.getUserProperties())) {
request.getRepositorySession()
.getData()
.set(TransformerContext.KEY, config.transformerContextBuilder.build());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
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;
Expand Down Expand Up @@ -1125,7 +1125,7 @@ private Model readRawModel(ModelBuildingRequest request, DefaultModelProblemColl
}

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

Expand Down

0 comments on commit 1bb46cd

Please sign in to comment.