View Javadoc

1   package org.apache.maven.lifecycle.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.execution.MavenSession;
23  import org.apache.maven.model.Plugin;
24  import org.apache.maven.model.PluginManagement;
25  import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
26  import org.apache.maven.plugin.version.PluginVersionRequest;
27  import org.apache.maven.plugin.version.PluginVersionResolutionException;
28  import org.apache.maven.plugin.version.PluginVersionResolver;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * @since 3.0
38   * @author Benjamin Bentmann
39   * @author Kristian Rosenvold (Extract class)
40   *         <p/>
41   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
42   */
43  @Component( role = LifecyclePluginResolver.class )
44  public class LifecyclePluginResolver
45  {
46      @Requirement
47      private PluginVersionResolver pluginVersionResolver;
48  
49  
50      public LifecyclePluginResolver( PluginVersionResolver pluginVersionResolver )
51      {
52          this.pluginVersionResolver = pluginVersionResolver;
53      }
54  
55      @SuppressWarnings( { "UnusedDeclaration" } )
56      public LifecyclePluginResolver()
57      {
58      }
59  
60      public void resolveMissingPluginVersions( MavenProject project, MavenSession session )
61          throws PluginVersionResolutionException
62      {
63          Map<String, String> versions = new HashMap<String, String>( 64 );
64  
65          for ( Plugin plugin : project.getBuildPlugins() )
66          {
67              if ( plugin.getVersion() == null )
68              {
69                  PluginVersionRequest request =
70                      new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
71                                                       project.getRemotePluginRepositories() );
72                  plugin.setVersion( pluginVersionResolver.resolve( request ).getVersion() );
73              }
74              versions.put( plugin.getKey(), plugin.getVersion() );
75          }
76  
77          PluginManagement pluginManagement = project.getPluginManagement();
78          if ( pluginManagement != null )
79          {
80              for ( Plugin plugin : pluginManagement.getPlugins() )
81              {
82                  if ( plugin.getVersion() == null )
83                  {
84                      plugin.setVersion( versions.get( plugin.getKey() ) );
85                      if ( plugin.getVersion() == null )
86                      {
87                          PluginVersionRequest request =
88                              new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
89                                                               project.getRemotePluginRepositories() );
90                          plugin.setVersion( pluginVersionResolver.resolve( request ).getVersion() );
91                      }
92                  }
93              }
94          }
95      }
96  }