Skip to content

Commit 0691e55

Browse files
authored
Do not force md download always (#11908)
My proposal, go back to original behaviour with a twist: * discover via project (if present) -- but added slight heuristics to try to prioritize plugins that have prefix in A (ie "spotless" -> "spotless-maven-plugin"), and then try all the remainers if not found * discover via G level metadata Rationale: original change was IMHO wrong: not only it allowed "takeover" of prefix, user was not able to do anything about it. The original code (where this PR goes back to) was done like this on purpose: the user input (POM) is mandatory, so if user defines a plugin, and it has given prefix, that must be used, whatever any remote repository G level metadata says. Only if there is no user instruction, go for prefix discovery via metadata. Code uses `Set` and gets rid of the possible double resolution of same plugin, as they may coexist (usually is) in project/build/plugins and project/build/pluginManagement/plugins. Fixes #11905
1 parent f8f5335 commit 0691e55

1 file changed

Lines changed: 39 additions & 10 deletions

File tree

maven-core/src/main/java/org/apache/maven/plugin/prefix/internal/DefaultPluginPrefixResolver.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525
import java.io.IOException;
2626
import java.util.ArrayList;
27+
import java.util.Collection;
2728
import java.util.Collections;
2829
import java.util.LinkedHashMap;
30+
import java.util.LinkedHashSet;
2931
import java.util.List;
3032
import java.util.Map;
3133
import java.util.Set;
@@ -105,16 +107,20 @@ public PluginPrefixResult resolve(PluginPrefixRequest request) throws NoPluginFo
105107
LinkedHashMap::new,
106108
Collectors.mapping(Plugin::getArtifactId, Collectors.toSet())));
107109
request.getPluginGroups().forEach(g -> candidates.put(g, null));
108-
PluginPrefixResult result = resolveFromRepository(request, candidates);
109-
110-
// If we haven't been able to resolve the plugin from the repository,
111-
// as a last resort, we go through all declared plugins, load them
110+
PluginPrefixResult result = null;
111+
// First, we go through all declared plugins, load them
112112
// one by one, and try to find a matching prefix.
113-
if (result == null && build != null) {
114-
result = resolveFromProject(request, build.getPlugins());
115-
if (result == null && management != null) {
116-
result = resolveFromProject(request, management.getPlugins());
117-
}
113+
if (build != null) {
114+
result = resolveFromProject(
115+
request,
116+
build.getPlugins(),
117+
management != null ? management.getPlugins() : Collections.emptyList());
118+
}
119+
120+
// Second, we go use G level metadata to discover prefix
121+
// This order allows user managed clashing prefixes (they can declare them in POM)
122+
if (result == null) {
123+
result = resolveFromRepository(request, candidates);
118124
}
119125

120126
if (result == null) {
@@ -132,7 +138,30 @@ public PluginPrefixResult resolve(PluginPrefixRequest request) throws NoPluginFo
132138
return result;
133139
}
134140

135-
private PluginPrefixResult resolveFromProject(PluginPrefixRequest request, List<Plugin> plugins) {
141+
private PluginPrefixResult resolveFromProject(
142+
PluginPrefixRequest request, List<Plugin> plugins, List<Plugin> pluginMgmt) {
143+
if (plugins.isEmpty() && pluginMgmt.isEmpty()) {
144+
return null;
145+
}
146+
PluginPrefixResult result = null;
147+
// try optimistically; first if A contains prefix?
148+
Set<Plugin> candidates = new LinkedHashSet<>();
149+
Stream.concat(plugins.stream(), pluginMgmt.stream())
150+
.filter(p -> p.getArtifactId().contains(request.getPrefix()))
151+
.forEach(candidates::add);
152+
if (!candidates.isEmpty()) {
153+
result = doResolveFromProject(request, candidates);
154+
}
155+
// if no luck; try the rest
156+
if (result == null) {
157+
Set<Plugin> remainder = new LinkedHashSet<>(plugins);
158+
remainder.removeAll(candidates);
159+
result = doResolveFromProject(request, remainder);
160+
}
161+
return result;
162+
}
163+
164+
private PluginPrefixResult doResolveFromProject(PluginPrefixRequest request, Collection<Plugin> plugins) {
136165
for (Plugin plugin : plugins) {
137166
try {
138167
PluginDescriptor pluginDescriptor =

0 commit comments

Comments
 (0)