Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8006] SPI to contribute to effective properties and more #1384

Merged
merged 20 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.api.annotations.Experimental;
Expand Down Expand Up @@ -133,4 +134,10 @@ default String getId() {

@Nonnull
List<RemoteRepository> getRemotePluginRepositories();

/**
* Returns the effective project properties.
*/
@Nonnull
Map<String, String> getProperties();
gnodet marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.annotations.ThreadSafe;
import org.apache.maven.api.model.Repository;
import org.apache.maven.api.services.DependencyCoordinateFactory;
Expand Down Expand Up @@ -73,15 +74,12 @@ public interface Session {
Map<String, String> getSystemProperties();

/**
* Gets the immutable effective properties to use for interpolation. The effective properties are collected from
* {@link #getSystemProperties()} and {@link #getUserProperties()} in this order, and more.
* <p>
* These properties are "effective" in a way they are built obeying all "user overrides" and others.
* Gets the effective properties to use for interpolation.
*
* @return the config properties, never {@code null}
* @return the effective properties, never {@code null}
gnodet marked this conversation as resolved.
Show resolved Hide resolved
*/
@Nonnull
Map<String, Object> getEffectiveProperties();
Map<String, String> getEffectiveProperties(@Nullable Project project);
gnodet marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the current maven version
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.services;

import java.util.Map;

import org.apache.maven.api.*;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;

/**
* @since 4.0.0
*/
@Experimental
public interface Properties extends Service {

/**
* Creates a {@link org.apache.maven.api.Project} from a POM file.
gnodet marked this conversation as resolved.
Show resolved Hide resolved
*
* @param project {@link Project} or {@code null}.
* @return the {@link Map} containing the effective properties.
* @throws IllegalArgumentException if an argument is {@code null} or invalid
*/
@Nonnull
Map<String, String> effectiveProperties(@Nonnull Session session, @Nullable Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.api.*;
Expand Down Expand Up @@ -154,6 +151,11 @@ public List<RemoteRepository> getRemotePluginRepositories() {
return new MappedList<>(project.getRemotePluginRepositories(), session::getRemoteRepository);
}

@Override
public Map<String, String> getProperties() {
return new PropertiesAsMap(project.getProperties());
}

@Nonnull
private DependencyCoordinate toDependency(org.apache.maven.api.model.Dependency dependency) {
return new DependencyCoordinate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.internal.impl;

import javax.inject.Named;
import javax.inject.Singleton;

import java.util.HashMap;
import java.util.Map;

import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.services.*;

import static org.apache.maven.internal.impl.Utils.nonNull;

@Named
@Singleton
public class DefaultProperties implements Properties {
@Nonnull
@Override
public Map<String, String> effectiveProperties(@Nonnull Session session, @Nullable Project project) {
nonNull(session);
HashMap<String, String> result = new HashMap<>(session.getSystemProperties());
if (project != null) {
result.putAll(project.getProperties());
}
result.putAll(session.getUserProperties());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.maven.api.services.Lookup;
import org.apache.maven.api.services.LookupException;
import org.apache.maven.api.services.MavenException;
import org.apache.maven.api.services.Properties;
import org.apache.maven.api.settings.Settings;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.bridge.MavenRepositorySystem;
Expand Down Expand Up @@ -107,19 +108,19 @@ public Settings getSettings() {
@Nonnull
@Override
public Map<String, String> getUserProperties() {
return session.getUserProperties();
return new PropertiesAsMap(mavenSession.getUserProperties());
}

@Nonnull
@Override
public Map<String, String> getSystemProperties() {
return session.getSystemProperties();
return new PropertiesAsMap(mavenSession.getSystemProperties());
}

@Nonnull
@Override
public Map<String, Object> getEffectiveProperties() {
return session.getConfigProperties();
public Map<String, String> getEffectiveProperties(@Nullable Project project) {
return getService(Properties.class).effectiveProperties(this, project);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import org.apache.maven.api.MojoExecution;
import org.apache.maven.api.Project;
Expand Down Expand Up @@ -54,15 +53,15 @@
* @see MojoExecution
*/
public class PluginParameterExpressionEvaluatorV4 implements TypeAwareExpressionEvaluator {
private Session session;
private final Session session;

private MojoExecution mojoExecution;
private final MojoExecution mojoExecution;

private Project project;
private final Project project;

private Path basedir;
private final Path basedir;

private Properties properties;
private final Map<String, String> properties;

public PluginParameterExpressionEvaluatorV4(Session session, Project project) {
this(session, project, null);
Expand All @@ -71,16 +70,9 @@ public PluginParameterExpressionEvaluatorV4(Session session, Project project) {
public PluginParameterExpressionEvaluatorV4(Session session, Project project, MojoExecution mojoExecution) {
this.session = session;
this.mojoExecution = mojoExecution;
this.properties = new Properties();
this.properties = session.getEffectiveProperties(project);
this.project = project;

//
// Maven4: We may want to evaluate how this is used but we add these separate as the
// getExecutionProperties is deprecated in MavenSession.
//
this.properties.putAll(session.getUserProperties());
this.properties.putAll(session.getSystemProperties());

Path basedir = null;

if (project != null) {
Expand Down Expand Up @@ -198,11 +190,7 @@ public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationEx
// to run a single test so I want to specify that class on the cli as
// a parameter.

value = properties.getProperty(expression);
}

if ((value == null) && ((project != null) && (project.getModel().getProperties() != null))) {
value = project.getModel().getProperties().get(expression);
value = properties.get(expression);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.internal.impl.DefaultLookup;
import org.apache.maven.internal.impl.DefaultSessionFactory;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
Expand Down Expand Up @@ -147,7 +148,7 @@ protected MavenSession createMavenSession(File pom, Properties executionProperti
initRepoSession(configuration);

DefaultSessionFactory defaultSessionFactory =
new DefaultSessionFactory(mock(RepositorySystem.class), null, null, null);
new DefaultSessionFactory(mock(RepositorySystem.class), null, new DefaultLookup(container), null);

MavenSession session = new MavenSession(
getContainer(), configuration.getRepositorySession(), request, new DefaultMavenExecutionResult());
Expand Down
Loading