1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.lifecycle.internal;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.model.PluginManagement;
31 import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
32 import org.apache.maven.plugin.version.PluginVersionRequest;
33 import org.apache.maven.plugin.version.PluginVersionResolutionException;
34 import org.apache.maven.plugin.version.PluginVersionResolver;
35 import org.apache.maven.project.MavenProject;
36
37
38
39
40
41 @Named
42 @Singleton
43 public class LifecyclePluginResolver {
44 private final PluginVersionResolver pluginVersionResolver;
45
46 @Inject
47 public LifecyclePluginResolver(PluginVersionResolver pluginVersionResolver) {
48 this.pluginVersionResolver = pluginVersionResolver;
49 }
50
51 public void resolveMissingPluginVersions(MavenProject project, MavenSession session)
52 throws PluginVersionResolutionException {
53 Map<String, String> versions = new HashMap<>(64);
54
55 for (Plugin plugin : project.getBuildPlugins()) {
56 if (plugin.getVersion() == null) {
57 PluginVersionRequest request = new DefaultPluginVersionRequest(
58 plugin, session.getRepositorySession(), project.getRemotePluginRepositories());
59 plugin.setVersion(pluginVersionResolver.resolve(request).getVersion());
60 }
61 versions.put(plugin.getKey(), plugin.getVersion());
62 }
63
64 PluginManagement pluginManagement = project.getPluginManagement();
65 if (pluginManagement != null) {
66 for (Plugin plugin : pluginManagement.getPlugins()) {
67 if (plugin.getVersion() == null) {
68 plugin.setVersion(versions.get(plugin.getKey()));
69 if (plugin.getVersion() == null) {
70 PluginVersionRequest request = new DefaultPluginVersionRequest(
71 plugin, session.getRepositorySession(), project.getRemotePluginRepositories());
72 plugin.setVersion(pluginVersionResolver.resolve(request).getVersion());
73 }
74 }
75 }
76 }
77 }
78 }