You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2024/01/06 20:40:54 UTC
(maven) branch MNG-6776-maven-3.9.x updated (16ee6837dd -> b23afbc984)
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a change to branch MNG-6776-maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git
discard 16ee6837dd [MNG-6776] Inconsistent list of parameters for MojoDescriptor (#584)
new b23afbc984 [MNG-6776] Inconsistent list of parameters for MojoDescriptor (#584)
This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version. This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:
* -- * -- B -- O -- O -- O (16ee6837dd)
\
N -- N -- N refs/heads/MNG-6776-maven-3.9.x (b23afbc984)
You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.
Any revisions marked "omit" are not gone; other references still
refer to them. Any revisions marked "discard" are gone forever.
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../maven/plugin/descriptor/MojoDescriptorTest.java | 19 -------------------
1 file changed, 19 deletions(-)
(maven) 01/01: [MNG-6776] Inconsistent list of parameters for MojoDescriptor (#584)
Posted by mi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a commit to branch MNG-6776-maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git
commit b23afbc9849d57b0a4ff0f2bdd3fff5520a95f7c
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Mon Feb 14 12:26:51 2022 +0100
[MNG-6776] Inconsistent list of parameters for MojoDescriptor (#584)
It may lead to confusion as parameters (list) and
parametersMap (map) may "fall apart" easily.
Also, parametersMap did not honor parameter
ordering while parameters list makes it look
that order is important.
Simply, rebuild the map always and retain
ordering as well.
Added UT and also removed some cruft provided
by java8.
Co-authored-by: Marc Wrobel <ma...@gmail.com>
Co-authored-by: Slawomir Jaranowski <s....@gmail.com>
(cherry picked from commit cc51006f2973356a1046ae0757325d5e9be75327)
(cherry picked from commit 5ce5591ae341188230d6ecca76c90bd0a786fcb2)
(cherry picked from commit 47e2d689d687358a85c2cdecc176cac36093817e)
---
https://issues.apache.org/jira/browse/MNG-7309
---
.../maven/plugin/descriptor/MojoDescriptor.java | 79 +++++++---------------
.../plugin/descriptor/MojoDescriptorTest.java | 48 +++++++++++++
2 files changed, 71 insertions(+), 56 deletions(-)
diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java
index 11c90a3b54..6e3d1a012c 100644
--- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java
+++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java
@@ -18,10 +18,11 @@
*/
package org.apache.maven.plugin.descriptor;
-import java.util.HashMap;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import org.apache.maven.plugin.Mojo;
import org.codehaus.plexus.component.repository.ComponentDescriptor;
@@ -51,9 +52,7 @@ public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneab
private static final String DEFAULT_LANGUAGE = "java";
- private List<Parameter> parameters;
-
- private Map<String, Parameter> parameterMap;
+ private final ArrayList<Parameter> parameters;
/** By default, the execution strategy is "once-per-session" */
private String executionStrategy = SINGLE_PASS_EXEC_STRATEGY;
@@ -91,7 +90,7 @@ public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneab
private String deprecated;
/**
- * Flags this Mojo to run it in a multi module way, i.e. aggregate the build with the set of projects listed as
+ * Flags this Mojo to run it in a multi-module way, i.e. aggregate the build with the set of projects listed as
* modules. By default, no need to aggregate the Maven project and its child modules
*/
private boolean aggregator = false;
@@ -140,6 +139,7 @@ public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneab
* Default constructor.
*/
public MojoDescriptor() {
+ this.parameters = new ArrayList<>();
setInstantiationStrategy(DEFAULT_INSTANTIATION_STRATEGY);
setComponentFactory(DEFAULT_LANGUAGE);
}
@@ -177,10 +177,11 @@ public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneab
}
/**
- * @return the list of parameters
+ * @return the list of parameters copy. Any change to returned list is NOT reflected on this instance. To add
+ * parameters, use {@link #addParameter(Parameter)} method.
*/
public List<Parameter> getParameters() {
- return parameters;
+ return new ArrayList<>(parameters);
}
/**
@@ -188,6 +189,7 @@ public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneab
* @throws DuplicateParameterException if any
*/
public void setParameters(List<Parameter> parameters) throws DuplicateParameterException {
+ this.parameters.clear();
for (Parameter parameter : parameters) {
addParameter(parameter);
}
@@ -198,31 +200,25 @@ public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneab
* @throws DuplicateParameterException if any
*/
public void addParameter(Parameter parameter) throws DuplicateParameterException {
- if (parameters != null && parameters.contains(parameter)) {
+ if (parameters.contains(parameter)) {
throw new DuplicateParameterException(parameter.getName()
+ " has been declared multiple times in mojo with goal: " + getGoal() + " (implementation: "
+ getImplementation() + ")");
}
- if (parameters == null) {
- parameters = new LinkedList<>();
- }
-
parameters.add(parameter);
}
/**
- * @return the list parameters as a Map
+ * @return the list parameters as a Map (keyed by {@link Parameter#getName()}) that is built from
+ * {@link #parameters} list on each call. In other words, the map returned is built on fly and is a copy.
+ * Any change to this map is NOT reflected on list and other way around!
*/
public Map<String, Parameter> getParameterMap() {
- if (parameterMap == null) {
- parameterMap = new HashMap<>();
-
- if (parameters != null) {
- for (Parameter pd : parameters) {
- parameterMap.put(pd.getName(), pd);
- }
- }
+ LinkedHashMap<String, Parameter> parameterMap = new LinkedHashMap<>();
+
+ for (Parameter pd : parameters) {
+ parameterMap.put(pd.getName(), pd);
}
return parameterMap;
@@ -321,14 +317,14 @@ public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneab
}
/**
- * @return the binded phase name of the Mojo
+ * @return the bound phase name of the Mojo
*/
public String getPhase() {
return phase;
}
/**
- * @param phase the new binded phase name of the Mojo
+ * @param phase the new bound phase name of the Mojo
*/
public void setPhase(String phase) {
this.phase = phase;
@@ -482,45 +478,16 @@ public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneab
if (object instanceof MojoDescriptor) {
MojoDescriptor other = (MojoDescriptor) object;
- if (!compareObjects(getPluginDescriptor(), other.getPluginDescriptor())) {
- return false;
- }
-
- return compareObjects(getGoal(), other.getGoal());
+ return Objects.equals(getPluginDescriptor(), other.getPluginDescriptor())
+ && Objects.equals(getGoal(), other.getGoal());
}
return false;
}
- private boolean compareObjects(Object first, Object second) {
- if (first == second) {
- return true;
- }
-
- if (first == null || second == null) {
- return false;
- }
-
- return first.equals(second);
- }
-
/** {@inheritDoc} */
public int hashCode() {
- int result = 1;
-
- String goal = getGoal();
-
- if (goal != null) {
- result += goal.hashCode();
- }
-
- PluginDescriptor pd = getPluginDescriptor();
-
- if (pd != null) {
- result -= pd.hashCode();
- }
-
- return result;
+ return Objects.hash(getGoal(), getPluginDescriptor());
}
/**
diff --git a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java
new file mode 100644
index 0000000000..846ca8154b
--- /dev/null
+++ b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.descriptor;
+
+import junit.framework.TestCase;
+
+public class MojoDescriptorTest extends TestCase {
+
+ public void testGetParameterMap() throws DuplicateParameterException {
+ MojoDescriptor mojoDescriptor = new MojoDescriptor();
+ Parameter param1 = new Parameter();
+ param1.setName("param1");
+ param1.setDefaultValue("value1");
+ mojoDescriptor.addParameter(param1);
+
+ assertEquals(1, mojoDescriptor.getParameters().size());
+
+ assertEquals(
+ mojoDescriptor.getParameters().size(),
+ mojoDescriptor.getParameterMap().size());
+
+ Parameter param2 = new Parameter();
+ param2.setName("param2");
+ param2.setDefaultValue("value2");
+ mojoDescriptor.addParameter(param2);
+
+ assertEquals(2, mojoDescriptor.getParameters().size());
+ assertEquals(
+ mojoDescriptor.getParameters().size(),
+ mojoDescriptor.getParameterMap().size());
+ }
+}