|
18 | 18 | import lombok.Getter; |
19 | 19 | import lombok.RequiredArgsConstructor; |
20 | 20 | import org.intellij.lang.annotations.Language; |
| 21 | +import org.jspecify.annotations.Nullable; |
21 | 22 | import org.openrewrite.ExecutionContext; |
22 | 23 | import org.openrewrite.Preconditions; |
23 | 24 | import org.openrewrite.Recipe; |
@@ -49,6 +50,7 @@ public class AddMockitoJavaAgentToMavenSurefirePlugin extends Recipe { |
49 | 50 |
|
50 | 51 | private static final String MAVEN_PLUGINS_GROUP_ID = "org.apache.maven.plugins"; |
51 | 52 | private static final String MAVEN_SUREFIRE_PLUGIN_ARTIFACT_ID = "maven-surefire-plugin"; |
| 53 | + private static final String MAVEN_DEPENDENCY_PLUGIN_ARTIFACT_ID = "maven-dependency-plugin"; |
52 | 54 |
|
53 | 55 | @Language("xpath") |
54 | 56 | private static final String MAVEN_DEPENDENCY_PLUGIN_EXECUTION_MATCHER = "/project/build/plugins/plugin[artifactId='maven-dependency-plugin']/executions/execution"; |
@@ -85,28 +87,46 @@ private Xml.Tag buildConfigurationTag(String argLineJavaAgentParam, boolean hasE |
85 | 87 |
|
86 | 88 | private void maybeAddMavenDependencyPluginWithPropertiesGoal() { |
87 | 89 | Optional<Plugin> mavenDependencyPlugin = getResolutionResult().getPom().getPlugins().stream() |
88 | | - .filter(plugin -> "org.apache.maven.plugins".equals(plugin.getGroupId()) && |
89 | | - "maven-dependency-plugin".equals(plugin.getArtifactId())).findFirst(); |
| 90 | + .filter(plugin -> isMavenPlugin(plugin, MAVEN_DEPENDENCY_PLUGIN_ARTIFACT_ID)).findFirst(); |
90 | 91 |
|
91 | 92 | if (!mavenDependencyPlugin.isPresent()) { |
92 | | - doAfterVisit(new AddPlugin("org.apache.maven.plugins", "maven-dependency-plugin", null, null, null, |
| 93 | + doAfterVisit(new AddPlugin(MAVEN_PLUGINS_GROUP_ID, MAVEN_DEPENDENCY_PLUGIN_ARTIFACT_ID, null, null, null, |
93 | 94 | "<executions>" + MAVEN_DEPENDENCY_PLUGIN_EXECUTION_TAG + "</executions>", "**/pom.xml").getVisitor()); |
94 | 95 | } else if (mavenDependencyPlugin.get().getExecutions().isEmpty()) { |
95 | | - doAfterVisit(new ChangePluginExecutions("org.apache.maven.plugins", "maven-dependency-plugin", MAVEN_DEPENDENCY_PLUGIN_EXECUTION_TAG).getVisitor()); |
| 96 | + doAfterVisit(new ChangePluginExecutions(MAVEN_PLUGINS_GROUP_ID, MAVEN_DEPENDENCY_PLUGIN_ARTIFACT_ID, MAVEN_DEPENDENCY_PLUGIN_EXECUTION_TAG).getVisitor()); |
96 | 97 | } else if (mavenDependencyPlugin.get().getExecutions().stream().noneMatch(execution -> execution.getGoals() != null)) { |
97 | 98 | doAfterVisit(new AddOrUpdateChildTag(MAVEN_DEPENDENCY_PLUGIN_EXECUTION_MATCHER, "<goals>" + MAVEN_DEPENDENCY_PLUGIN_PROPERTIES_GOAL + "</goals>", false).getVisitor()); |
98 | | - } else if (mavenDependencyPlugin.get().getExecutions().stream().noneMatch(execution -> execution.getGoals() != null && execution.getGoals().contains("properties"))) { |
| 99 | + } else if (!hasPropertiesGoal(mavenDependencyPlugin.get())) { |
99 | 100 | doAfterVisit(new AppendChildTagToParentVisitor( |
100 | 101 | new XPathMatcher(MAVEN_DEPENDENCY_PLUGIN_EXECUTION_MATCHER + "/goals"), |
101 | 102 | Xml.Tag.build(MAVEN_DEPENDENCY_PLUGIN_PROPERTIES_GOAL))); |
102 | 103 | } |
103 | 104 | } |
104 | 105 |
|
| 106 | + private @Nullable String surefireArgLineWithAgent(List<Plugin> plugins) { |
| 107 | + String agentArgument = getArgLineJavaAgentArgument(); |
| 108 | + return plugins.stream() |
| 109 | + .filter(plugin -> isMavenPlugin(plugin, MAVEN_SUREFIRE_PLUGIN_ARTIFACT_ID)) |
| 110 | + .map(plugin -> plugin.getConfigurationStringValue("argLine")) |
| 111 | + .filter(argLine -> argLine != null && argLine.contains(agentArgument)) |
| 112 | + .findFirst().orElse(null); |
| 113 | + } |
| 114 | + |
| 115 | + private boolean mavenDependencyPluginHasPropertiesGoal() { |
| 116 | + return getResolutionResult().getPom().getPlugins().stream() |
| 117 | + .anyMatch(plugin -> isMavenPlugin(plugin, MAVEN_DEPENDENCY_PLUGIN_ARTIFACT_ID) && hasPropertiesGoal(plugin)); |
| 118 | + } |
| 119 | + |
105 | 120 | @Override |
106 | 121 | public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { |
107 | | - if (getResolutionResult().getPom().getPluginManagement().stream().anyMatch( |
108 | | - plugin -> "org.apache.maven.plugins".equals(plugin.getGroupId()) && "maven-surefire-plugin" |
109 | | - .equals(plugin.getArtifactId()) && plugin.getConfigurationStringValue("argLine") != null && plugin.getConfigurationStringValue("argLine").contains(getArgLineJavaAgentArgument()))) { |
| 122 | + if (surefireArgLineWithAgent(getResolutionResult().getPom().getPluginManagement()) != null) { |
| 123 | + return document; |
| 124 | + } |
| 125 | + // Nothing to add when the surefire agent, the properties goal, and any `@{argLine}` property |
| 126 | + // are already present, whether declared here or inherited from a parent's `build/plugins` (#1164). |
| 127 | + String pluginArgLine = surefireArgLineWithAgent(getResolutionResult().getPom().getPlugins()); |
| 128 | + if (pluginArgLine != null && mavenDependencyPluginHasPropertiesGoal() && |
| 129 | + (!pluginArgLine.contains("@{argLine}") || getResolutionResult().getPom().getProperties().containsKey("argLine"))) { |
110 | 130 | return document; |
111 | 131 | } |
112 | 132 |
|
@@ -168,6 +188,20 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { |
168 | 188 | }); |
169 | 189 | } |
170 | 190 |
|
| 191 | + /** |
| 192 | + * Matches a plugin under the {@code org.apache.maven.plugins} group, tolerating an implied (omitted) groupId, |
| 193 | + * which Maven defaults to that group for {@code maven-*-plugin} declarations. |
| 194 | + */ |
| 195 | + private static boolean isMavenPlugin(Plugin plugin, String artifactId) { |
| 196 | + return artifactId.equals(plugin.getArtifactId()) && |
| 197 | + (plugin.getGroupId() == null || MAVEN_PLUGINS_GROUP_ID.equals(plugin.getGroupId())); |
| 198 | + } |
| 199 | + |
| 200 | + private static boolean hasPropertiesGoal(Plugin plugin) { |
| 201 | + return plugin.getExecutions().stream() |
| 202 | + .anyMatch(execution -> execution.getGoals() != null && execution.getGoals().contains("properties")); |
| 203 | + } |
| 204 | + |
171 | 205 | @RequiredArgsConstructor |
172 | 206 | private static class AppendChildTagToParentVisitor extends XmlIsoVisitor<ExecutionContext> { |
173 | 207 | private final XPathMatcher parentXPathMatcher; |
|
0 commit comments