gnodet commented on code in PR #13249: URL: https://github.com/apache/maven/pull/13249#discussion_r4094052340
########## impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/filter/CoordinatePredicate.java: ########## @@ -0,0 +1,248 @@ +/* + * 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.lifecycle.internal.filter; + +import org.apache.maven.api.MojoExecution; +import org.apache.maven.api.plugin.descriptor.PluginDescriptor; + +/** + * A {@link FilterPredicate} that matches {@link MojoExecution}s by plugin coordinate or prefix. + * + * <h2>Syntax: {@code ([G[:A]]|P)[:v][:g[@e]]}</h2> + * + * <p>The {@code @} separator for execution ID is compatible with Maven's existing + * {@code plugin:version:goal@executionId} notation used in + * {@code DefaultLifecycleExecutionPlanCalculator} for goal tasks. + * + * <p>Matching uses {@link MojoExecution#getDescriptor()} for goal-level fields and + * {@link MojoExecution#getPlugin()} for plugin-level coordinates (groupId, artifactId, version, + * goal prefix). + * + * <p>Forms: + * <ul> + * <li>{@code *} — matches every mojo execution</li> + * <li>{@code :A} — any groupId, specific artifactId (e.g. {@code :maven-enforcer-plugin})</li> + * <li>{@code G:A} — exact groupId:artifactId (e.g. {@code org.apache.maven.plugins:maven-enforcer-plugin}). + * The groupId <strong>must contain at least one {@code '.'}</strong> for this form to be recognised; + * groupIds without a dot (e.g. {@code commons-io}) are routed to prefix-based matching instead. + * Use the {@code :A} form to match by artifactId only when the groupId has no dot.</li> + * <li>{@code P} — plugin prefix (e.g. {@code enforcer}), resolved against + * {@link MojoExecution#getMojoDescriptor()} goal prefix</li> + * <li>{@code P:v:g} — prefix + version + goal</li> + * <li>{@code P:v:g@e} — prefix + version + goal + executionId</li> Review Comment: Fixed in 54de779c5a: added note to the `P` bullet in class-level Javadoc that prefixes containing a `.` (e.g. `io.smallrye`) are routed to G:A matching instead of prefix-based matching. ########## impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/filter/CoordinatePredicateTest.java: ########## @@ -0,0 +1,180 @@ +/* + * 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.lifecycle.internal.filter; + +import org.apache.maven.api.MojoExecution; +import org.apache.maven.api.Plugin; +import org.apache.maven.api.plugin.descriptor.PluginDescriptor; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link CoordinatePredicate}. + */ +class CoordinatePredicateTest { + + private static MojoExecution execution( + String groupId, String artifactId, String version, String goalPrefix, String goal, String executionId) { + PluginDescriptor descriptor = mock(PluginDescriptor.class); + when(descriptor.getGroupId()).thenReturn(groupId); + when(descriptor.getArtifactId()).thenReturn(artifactId); + when(descriptor.getVersion()).thenReturn(version); + when(descriptor.getGoalPrefix()).thenReturn(goalPrefix); + + Plugin plugin = mock(Plugin.class); + when(plugin.getDescriptor()).thenReturn(descriptor); + + MojoExecution exec = mock(MojoExecution.class); + when(exec.getPlugin()).thenReturn(plugin); + when(exec.getGoal()).thenReturn(goal); + when(exec.getExecutionId()).thenReturn(executionId); + return exec; + } + + private static MojoExecution surefire() { + return execution( + "org.apache.maven.plugins", "maven-surefire-plugin", "3.2.5", "surefire", "test", "default-test"); + } + + private static MojoExecution enforcer() { + return execution( + "org.apache.maven.plugins", "maven-enforcer-plugin", "3.4.1", "enforcer", "enforce", "enforce-rules"); + } + + @Test + void wildcardMatchesAll() { + assertTrue(CoordinatePredicate.parse("*").matches(surefire())); + assertTrue(CoordinatePredicate.parse("*").matches(enforcer())); + } + + @Test + void artifactIdOnlyMatchesByArtifactId() { + CoordinatePredicate p = CoordinatePredicate.parse(":maven-surefire-plugin"); + assertTrue(p.matches(surefire())); + assertFalse(p.matches(enforcer())); + } + + @Test + void colonAloneMatchesAll() { + CoordinatePredicate p = CoordinatePredicate.parse(":"); + assertTrue(p.matches(surefire())); + assertTrue(p.matches(enforcer())); + } + + @Test + void groupAndArtifactMatchesExact() { + CoordinatePredicate p = CoordinatePredicate.parse("org.apache.maven.plugins:maven-surefire-plugin"); + assertTrue(p.matches(surefire())); + assertFalse(p.matches(enforcer())); + } + + @Test + void groupWithWildcardArtifact() { + CoordinatePredicate p = CoordinatePredicate.parse("org.apache.maven.plugins:"); + assertTrue(p.matches(surefire())); + assertTrue(p.matches(enforcer())); + } + + @Test + void groupMismatchDoesNotMatch() { + CoordinatePredicate p = CoordinatePredicate.parse("com.example:maven-surefire-plugin"); + assertFalse(p.matches(surefire())); + } + + @Test + void prefixMatchesByGoalPrefix() { + CoordinatePredicate p = CoordinatePredicate.parse("surefire"); + assertTrue(p.matches(surefire())); + assertFalse(p.matches(enforcer())); + } + + @Test + void prefixWithGoalMatchesGoal() { + CoordinatePredicate p = CoordinatePredicate.parse("surefire::test"); + assertTrue(p.matches(surefire())); + assertFalse(p.matches(enforcer())); + } + + @Test + void prefixWithVersionGoalAndExecution() { + CoordinatePredicate p = CoordinatePredicate.parse("surefire:3.2.5:test@default-test"); + assertTrue(p.matches(surefire())); + } + + @Test + void prefixVersionMismatch() { + CoordinatePredicate p = CoordinatePredicate.parse("surefire:99.0:test@default-test"); + assertFalse(p.matches(surefire())); + } + + @Test + void prefixExecutionIdMismatch() { + CoordinatePredicate p = CoordinatePredicate.parse("surefire::test@other-id"); + assertFalse(p.matches(surefire())); + } + + @Test + void fullCoordinateWithGoalAndExecution() { + CoordinatePredicate p = + CoordinatePredicate.parse("org.apache.maven.plugins:maven-surefire-plugin:3.2.5:test@default-test"); + assertTrue(p.matches(surefire())); + } + + @Test + void coordinateGoalMismatch() { + CoordinatePredicate p = + CoordinatePredicate.parse("org.apache.maven.plugins:maven-surefire-plugin:3.2.5:other@default-test"); + assertFalse(p.matches(surefire())); + } + + @Test + void noDescriptorDoesNotMatchSpecificPlugin() { + MojoExecution exec = mock(MojoExecution.class); + when(exec.getPlugin()).thenReturn(null); + when(exec.getGoal()).thenReturn("test"); + when(exec.getExecutionId()).thenReturn("default-test"); + assertFalse(CoordinatePredicate.parse(":maven-surefire-plugin").matches(exec)); + } + + @Test + void wildcardMatchesEvenWithNullPlugin() { + MojoExecution exec = mock(MojoExecution.class); + when(exec.getPlugin()).thenReturn(null); + assertTrue(CoordinatePredicate.MATCH_ALL.matches(exec)); + } + + @Test + void dotlessGroupIdIsMisroutedToPrefixMode() { + // GroupIds without a '.' (e.g. "commons-io") cannot be used in G:A form — they are routed + // to prefix-based matching. Document this: the token "commons-io:commons-io" is treated as + // prefix="commons-io", version="commons-io" — it will NOT match an execution with groupId + // "commons-io". Users should use ":commons-io" (artifactId-only form) instead. + MojoExecution exec = execution("commons-io", "commons-io", "2.15.1", "commons-io", "copy", "default"); + // Prefix "commons-io" won't match goal prefix "commons-io" if the descriptor prefix happens + // to be the same, but the version segment "commons-io" will never match "2.15.1" → no match. + CoordinatePredicate p = CoordinatePredicate.parse("commons-io:commons-io"); Review Comment: Fixed in 54de779c5a: the comment now correctly states that the prefix check succeeds (goalPrefix="commons-io" matches) but the version check fails ("commons-io" != "2.15.1"), so the predicate returns false — demonstrating misrouting produces a non-matching predicate, not a silent match. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
