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.lifecycle.DefaultLifecycles;
23  import org.apache.maven.lifecycle.LifeCyclePluginAnalyzer;
24  import org.apache.maven.lifecycle.Lifecycle;
25  import org.apache.maven.lifecycle.mapping.LifecycleMapping;
26  import org.apache.maven.model.Plugin;
27  import org.apache.maven.model.PluginExecution;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.codehaus.plexus.component.annotations.Requirement;
30  import org.codehaus.plexus.logging.Logger;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  import java.util.LinkedHashMap;
34  import java.util.Map;
35  import java.util.Set;
36  
37  /**
38   * @since 3.0
39   * @author Benjamin Bentmann
40   * @author Jason van Zyl
41   * @author jdcasey
42   * @author Kristian Rosenvold (extracted class only)
43   *         <p/>
44   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
45   */
46  @Component( role = LifeCyclePluginAnalyzer.class )
47  public class DefaultLifecyclePluginAnalyzer
48      implements LifeCyclePluginAnalyzer
49  {
50  
51      @Requirement( role = LifecycleMapping.class )
52      private Map<String, LifecycleMapping> lifecycleMappings;
53  
54      @Requirement
55      private DefaultLifecycles defaultLifeCycles;
56  
57      @Requirement
58      private Logger logger;
59  
60      public DefaultLifecyclePluginAnalyzer()
61      {
62      }
63  
64      // These methods deal with construction intact Plugin object that look like they come from a standard
65      // <plugin/> block in a Maven POM. We have to do some wiggling to pull the sources of information
66      // together and this really shows the problem of constructing a sensible default configuration but
67      // it's all encapsulated here so it appears normalized to the POM builder.
68  
69      // We are going to take the project packaging and find all plugin in the default lifecycle and create
70      // fully populated Plugin objects, including executions with goals and default configuration taken
71      // from the plugin.xml inside a plugin.
72      //
73  
74      public Set<Plugin> getPluginsBoundByDefaultToAllLifecycles( String packaging )
75      {
76          if ( logger.isDebugEnabled() )
77          {
78              logger.debug( "Looking up lifecyle mappings for packaging " + packaging + " from " +
79                  Thread.currentThread().getContextClassLoader() );
80          }
81  
82          LifecycleMapping lifecycleMappingForPackaging = lifecycleMappings.get( packaging );
83  
84          if ( lifecycleMappingForPackaging == null )
85          {
86              return null;
87          }
88  
89          Map<Plugin, Plugin> plugins = new LinkedHashMap<Plugin, Plugin>();
90  
91          for ( Lifecycle lifecycle : defaultLifeCycles.getLifeCycles() )
92          {
93              org.apache.maven.lifecycle.mapping.Lifecycle lifecycleConfiguration =
94                  lifecycleMappingForPackaging.getLifecycles().get( lifecycle.getId() );
95  
96              Map<String, String> phaseToGoalMapping = null;
97  
98              if ( lifecycleConfiguration != null )
99              {
100                 phaseToGoalMapping = lifecycleConfiguration.getPhases();
101             }
102             else if ( lifecycle.getDefaultPhases() != null )
103             {
104                 phaseToGoalMapping = lifecycle.getDefaultPhases();
105             }
106 
107             if ( phaseToGoalMapping != null )
108             {
109                 // These are of the form:
110                 //
111                 // compile -> org.apache.maven.plugins:maven-compiler-plugin:compile[,gid:aid:goal,...]
112                 //
113                 for ( Map.Entry<String, String> goalsForLifecyclePhase : phaseToGoalMapping.entrySet() )
114                 {
115                     String phase = goalsForLifecyclePhase.getKey();
116                     String goals = goalsForLifecyclePhase.getValue();
117                     if ( goals != null )
118                     {
119                         parseLifecyclePhaseDefinitions( plugins, phase, goals );
120                     }
121                 }
122             }
123         }
124 
125         return plugins.keySet();
126     }
127 
128     private void parseLifecyclePhaseDefinitions( Map<Plugin, Plugin> plugins, String phase, String goals )
129     {
130         String[] mojos = StringUtils.split( goals, "," );
131 
132         for ( int i = 0; i < mojos.length; i++ )
133         {
134             // either <groupId>:<artifactId>:<goal> or <groupId>:<artifactId>:<version>:<goal>
135             String goal = mojos[i].trim();
136             String[] p = StringUtils.split( goal, ":" );
137 
138             PluginExecution execution = new PluginExecution();
139             execution.setId( "default-" + p[p.length - 1] );
140             execution.setPhase( phase );
141             execution.setPriority( i - mojos.length );
142             execution.getGoals().add( p[p.length - 1] );
143 
144             Plugin plugin = new Plugin();
145             plugin.setGroupId( p[0] );
146             plugin.setArtifactId( p[1] );
147             if ( p.length >= 4 )
148             {
149                 plugin.setVersion( p[2] );
150             }
151 
152             Plugin existing = plugins.get( plugin );
153             if ( existing != null )
154             {
155                 if ( existing.getVersion() == null )
156                 {
157                     existing.setVersion( plugin.getVersion() );
158                 }
159                 plugin = existing;
160             }
161             else
162             {
163                 plugins.put( plugin, plugin );
164             }
165 
166             plugin.getExecutions().add( execution );
167         }
168     }
169 
170 
171 }