View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
39   * @since 3.0
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  }