Skip to content

Commit

Permalink
[MNG-7706] Deprecate 'localRepository' parameter expression (#1009) (#…
Browse files Browse the repository at this point in the history
…1012)

This PR deprecates the 'localRepository' mojo parameter expression, and Core will emit warning if used by any Mojo.

---

https://issues.apache.org/jira/browse/MNG-7706
  • Loading branch information
cstamas authored Feb 23, 2023
1 parent b1bffd6 commit 22d2b47
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
/**
* Abstraction of an artifact repository. Artifact repositories can be remote, local, or even build reactor or
* IDE workspace.
*
* @deprecated Avoid use of this type, if you need access to local repository use repository system classes instead.
*/
@Deprecated
public interface ArtifactRepository {
String pathOf(Artifact artifact);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
* Describes a set of policies for a repository to use under certain conditions.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @deprecated Avoid use of this type, if you need access to local repository use repository system session instead.
*/
@Deprecated
public class ArtifactRepositoryPolicy {
public static final String UPDATE_POLICY_NEVER = "never";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;

/** @author jdcasey */
/**
* Repository layout.
*
* @author jdcasey
* @deprecated Avoid use of this type, if you need access to local repository use repository system session instead.
*/
@Deprecated
public interface ArtifactRepositoryLayout {
String ROLE = ArtifactRepositoryLayout.class.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* <tr><td><code>session</code></td> <td></td> <td>the actual {@link MavenSession}</td></tr>
* <tr><td><code>session.*</code></td> <td>(since Maven 3)</td><td></td></tr>
* <tr><td><code>localRepository</code></td> <td></td>
* <td>{@link MavenSession#getLocalRepository()}</td></tr>
* <td>{@link MavenSession#getLocalRepository()} DEPRECATED: Avoid use of {@link org.apache.maven.artifact.repository.ArtifactRepository} type. If you need access to local repository, switch to '${repositorySystemSession}' expression and get LRM from it instead. See <a href="https://issues.apache.org/jira/browse/MNG-7706">MNG-7706</a></td></tr>
* <tr><td><code>reactorProjects</code></td> <td></td> <td>{@link MavenSession#getProjects()}</td></tr>
* <tr><td><code>repositorySystemSession</code></td><td> (since Maven 3)</td>
* <td>{@link MavenSession#getRepositorySession()}</td></tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.plugin.internal;

import java.util.Arrays;
import java.util.List;

/**
* Common implementations for plugin parameters configuration validation that relies on Mojo descriptor (leaves out
* core parameters by default).
*
* @author Slawomir Jaranowski
*/
abstract class AbstractMavenPluginDescriptorSourcedParametersValidator extends AbstractMavenPluginParametersValidator {

// plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
// so, we need ignore those

// source org.apache.maven.plugin.PluginParameterExpressionEvaluator
private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
"basedir",
"executedProject",
"localRepository",
"mojo",
"mojoExecution",
"plugin",
"project",
"reactorProjects",
"session",
"settings");

private static final List<String> IGNORED_PROPERTY_PREFIX =
Arrays.asList("mojo.", "pom.", "plugin.", "project.", "session.", "settings.");

@Override
protected boolean isIgnoredProperty(String strValue) {
if (!strValue.startsWith("${")) {
return false;
}

String propertyName = strValue.replace("${", "").replace("}", "");

if (IGNORED_PROPERTY_VALUES.contains(propertyName)) {
return true;
}

return IGNORED_PROPERTY_PREFIX.stream().anyMatch(propertyName::startsWith);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
*/
package org.apache.maven.plugin.internal;

import java.util.Arrays;
import java.util.List;

import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.shared.utils.logging.MessageBuilder;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Common implementations for plugin parameters configuration validation.
Expand All @@ -36,28 +35,9 @@
*/
abstract class AbstractMavenPluginParametersValidator implements MavenPluginConfigurationValidator {

// plugin author can provide @Parameter( property = "session" ) in this case property will always evaluate
// so, we need ignore those

// source org.apache.maven.plugin.PluginParameterExpressionEvaluator
private static final List<String> IGNORED_PROPERTY_VALUES = Arrays.asList(
"basedir",
"executedProject",
"localRepository",
"mojo",
"mojoExecution",
"plugin",
"project",
"reactorProjects",
"session",
"settings");

private static final List<String> IGNORED_PROPERTY_PREFIX =
Arrays.asList("mojo.", "plugin.", "project.", "session.", "settings.");
protected final Logger logger = LoggerFactory.getLogger(getClass());

protected abstract Logger getLogger();

protected static boolean isValueSet(PlexusConfiguration config, ExpressionEvaluator expressionEvaluator) {
protected boolean isValueSet(PlexusConfiguration config, ExpressionEvaluator expressionEvaluator) {
if (config == null) {
return false;
}
Expand Down Expand Up @@ -91,18 +71,25 @@ protected static boolean isValueSet(PlexusConfiguration config, ExpressionEvalua
return false;
}

private static boolean isIgnoredProperty(String strValue) {
if (!strValue.startsWith("${")) {
return false;
@Override
public final void validate(
MojoDescriptor mojoDescriptor,
PlexusConfiguration pomConfiguration,
ExpressionEvaluator expressionEvaluator) {
if (!logger.isWarnEnabled()) {
return;
}

String propertyName = strValue.replace("${", "").replace("}", "");
doValidate(mojoDescriptor, pomConfiguration, expressionEvaluator);
}

if (IGNORED_PROPERTY_VALUES.contains(propertyName)) {
return true;
}
protected abstract void doValidate(
MojoDescriptor mojoDescriptor,
PlexusConfiguration pomConfiguration,
ExpressionEvaluator expressionEvaluator);

return IGNORED_PROPERTY_PREFIX.stream().anyMatch(propertyName::startsWith);
protected boolean isIgnoredProperty(String strValue) {
return false;
}

protected abstract String getParameterLogReason(Parameter parameter);
Expand All @@ -120,6 +107,6 @@ protected void logParameter(Parameter parameter) {

messageBuilder.warning(" ").warning(getParameterLogReason(parameter));

getLogger().warn(messageBuilder.toString());
logger.warn(messageBuilder.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.plugin.internal;

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

import java.util.HashMap;

import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.configuration.PlexusConfiguration;

/**
* Print warnings if deprecated core parameters are used in mojo.
*
* @since 3.9.1
*/
@Singleton
@Named
class DeprecatedCoreExpressionValidator extends AbstractMavenPluginParametersValidator {
private static final HashMap<String, String> DEPRECATED_CORE_PARAMETERS;

private static final String ARTIFACT_REPOSITORY_REASON =
"Avoid use of ArtifactRepository type. If you need access to local repository, switch to '${repositorySystemSession}' expression and get LRM from it instead.";

static {
HashMap<String, String> deprecatedCoreParameters = new HashMap<>();
deprecatedCoreParameters.put("localRepository", ARTIFACT_REPOSITORY_REASON);
deprecatedCoreParameters.put("session.localRepository", ARTIFACT_REPOSITORY_REASON);
DEPRECATED_CORE_PARAMETERS = deprecatedCoreParameters;
}

@Override
protected String getParameterLogReason(Parameter parameter) {
return "is deprecated core expression; " + DEPRECATED_CORE_PARAMETERS.get(parameter.getName());
}

@Override
protected void doValidate(
MojoDescriptor mojoDescriptor,
PlexusConfiguration pomConfiguration,
ExpressionEvaluator expressionEvaluator) {
if (mojoDescriptor.getParameters() == null) {
return;
}

mojoDescriptor.getParameters().stream()
.filter(parameter -> DEPRECATED_CORE_PARAMETERS.containsKey(parameter.getName()))
.forEach(this::logParameter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,25 @@
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Print warnings if deprecated mojo or parameters of plugin are used in configuration.
*
* @author Slawomir Jaranowski
*/
@Named
@Singleton
class DeprecatedPluginValidator extends AbstractMavenPluginParametersValidator {
private static final Logger LOGGER = LoggerFactory.getLogger(DeprecatedPluginValidator.class);

@Override
protected Logger getLogger() {
return LOGGER;
}

@Named
class DeprecatedPluginValidator extends AbstractMavenPluginDescriptorSourcedParametersValidator {
@Override
protected String getParameterLogReason(Parameter parameter) {
return "is deprecated: " + parameter.getDeprecated();
}

@Override
public void validate(
protected void doValidate(
MojoDescriptor mojoDescriptor,
PlexusConfiguration pomConfiguration,
ExpressionEvaluator expressionEvaluator) {
if (!LOGGER.isWarnEnabled()) {
return;
}

if (mojoDescriptor.getDeprecated() != null) {
logDeprecatedMojo(mojoDescriptor);
}
Expand Down Expand Up @@ -85,6 +72,6 @@ private void logDeprecatedMojo(MojoDescriptor mojoDescriptor) {
.warning(mojoDescriptor.getDeprecated())
.toString();

LOGGER.warn(message);
logger.warn(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.apache.maven.plugin.descriptor.Parameter;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Print warnings if read-only parameters of a plugin are used in configuration.
Expand All @@ -35,25 +33,18 @@
*/
@Named
@Singleton
public class ReadOnlyPluginParametersValidator extends AbstractMavenPluginParametersValidator {
private static final Logger LOGGER = LoggerFactory.getLogger(ReadOnlyPluginParametersValidator.class);

@Override
protected Logger getLogger() {
return LOGGER;
}

class ReadOnlyPluginParametersValidator extends AbstractMavenPluginDescriptorSourcedParametersValidator {
@Override
protected String getParameterLogReason(Parameter parameter) {
return "is read-only, must not be used in configuration";
}

@Override
public void validate(
protected void doValidate(
MojoDescriptor mojoDescriptor,
PlexusConfiguration pomConfiguration,
ExpressionEvaluator expressionEvaluator) {
if (!LOGGER.isWarnEnabled()) {
if (mojoDescriptor.getParameters() == null) {
return;
}

Expand All @@ -62,7 +53,7 @@ public void validate(
.forEach(parameter -> checkParameter(parameter, pomConfiguration, expressionEvaluator));
}

protected void checkParameter(
private void checkParameter(
Parameter parameter, PlexusConfiguration pomConfiguration, ExpressionEvaluator expressionEvaluator) {
PlexusConfiguration config = pomConfiguration.getChild(parameter.getName(), false);

Expand Down

0 comments on commit 22d2b47

Please sign in to comment.