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
42
43 @Named
44 @Singleton
45 public class LifecyclePluginResolver {
46 private final PluginVersionResolver pluginVersionResolver;
47
48 @Inject
49 public LifecyclePluginResolver(PluginVersionResolver pluginVersionResolver) {
50 this.pluginVersionResolver = pluginVersionResolver;
51 }
52
53 public void resolveMissingPluginVersions(MavenProject project, MavenSession session)
54 throws PluginVersionResolutionException {
55 Map<String, String> versions = new HashMap<>(64);
56
57 for (Plugin plugin : project.getBuildPlugins()) {
58 if (plugin.getVersion() == null) {
59 PluginVersionRequest request = new DefaultPluginVersionRequest(
60 plugin, session.getRepositorySession(), project.getRemotePluginRepositories());
61 plugin.setVersion(pluginVersionResolver.resolve(request).getVersion());
62 }
63 versions.put(plugin.getKey(), plugin.getVersion());
64 }
65
66 PluginManagement pluginManagement = project.getPluginManagement();
67 if (pluginManagement != null) {
68 for (Plugin plugin : pluginManagement.getPlugins()) {
69 if (plugin.getVersion() == null) {
70 plugin.setVersion(versions.get(plugin.getKey()));
71 if (plugin.getVersion() == null) {
72 PluginVersionRequest request = new DefaultPluginVersionRequest(
73 plugin, session.getRepositorySession(), project.getRemotePluginRepositories());
74 plugin.setVersion(pluginVersionResolver.resolve(request).getVersion());
75 }
76 }
77 }
78 }
79 }
80 }