View Javadoc
1   package org.apache.maven.archetype.ui.generation;
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.commons.lang.StringUtils;
23  import org.apache.maven.archetype.ArchetypeGenerationRequest;
24  import org.apache.maven.archetype.ArchetypeManager;
25  import org.apache.maven.archetype.catalog.Archetype;
26  import org.apache.maven.archetype.exception.ArchetypeNotDefined;
27  import org.apache.maven.archetype.exception.ArchetypeSelectionFailure;
28  import org.apache.maven.archetype.exception.UnknownArchetype;
29  import org.apache.maven.archetype.exception.UnknownGroup;
30  import org.apache.maven.archetype.ui.ArchetypeDefinition;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.component.annotations.Requirement;
33  import org.codehaus.plexus.components.interactivity.PrompterException;
34  import org.codehaus.plexus.logging.AbstractLogEnabled;
35  
36  import java.io.IOException;
37  import java.util.HashMap;
38  import java.util.LinkedHashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  @Component( role = ArchetypeSelector.class, hint = "default" )
43  public class DefaultArchetypeSelector
44      extends AbstractLogEnabled
45      implements ArchetypeSelector
46  {
47      static final String DEFAULT_ARCHETYPE_GROUPID = "org.apache.maven.archetypes";
48  
49      static final String DEFAULT_ARCHETYPE_VERSION = "1.0";
50  
51      static final String DEFAULT_ARCHETYPE_ARTIFACTID = "maven-archetype-quickstart";
52  
53      @Requirement
54      private ArchetypeSelectionQueryer archetypeSelectionQueryer;
55  
56      @Requirement
57      private ArchetypeManager archetypeManager;
58  
59      public void selectArchetype( ArchetypeGenerationRequest request, Boolean interactiveMode, String catalogs )
60          throws ArchetypeNotDefined, UnknownArchetype, UnknownGroup, IOException, PrompterException,
61          ArchetypeSelectionFailure
62      {
63          ArchetypeDefinition definition = new ArchetypeDefinition( request );
64  
65          if ( definition.isDefined() && StringUtils.isNotEmpty( request.getArchetypeRepository() ) )
66          {
67              getLogger().info( "Archetype defined by properties" );
68              return;
69          }
70  
71          Map<String, List<Archetype>> archetypes = getArchetypesByCatalog( catalogs );
72  
73          if ( StringUtils.isNotBlank( request.getFilter() ) )
74          {
75              // applying some filtering depending on filter parameter
76              archetypes = ArchetypeSelectorUtils.getFilteredArchetypesByCatalog( archetypes, request.getFilter() );
77              if ( archetypes.isEmpty() )
78              {
79                  getLogger().info( "Your filter doesn't match any archetype, so try again with another value." );
80                  return;
81              }
82          }
83  
84          if ( definition.isDefined() && StringUtils.isEmpty( request.getArchetypeRepository() ) )
85          {
86              Map.Entry<String, Archetype> found =
87                  findArchetype( archetypes, request.getArchetypeGroupId(), request.getArchetypeArtifactId() );
88  
89              if ( found != null )
90              {
91                  String catalogKey = found.getKey();
92                  Archetype archetype = found.getValue();
93  
94                  updateRepository( definition, archetype, catalogKey );
95  
96                  getLogger().info( "Archetype repository not defined. Using the one from " + archetype + " found in catalog "
97                                        + catalogKey );
98              }
99              else
100             {
101                 getLogger().warn(
102                     "Archetype not found in any catalog. Falling back to central repository (http://repo.maven.apache.org/maven2)." );
103                 getLogger().warn(
104                     "Use -DarchetypeRepository=<your repository> if archetype's repository is elsewhere." );
105 
106                 definition.setRepository( "http://repo.maven.apache.org/maven2" );
107             }
108         }
109 
110         if ( !definition.isDefined() && definition.isPartiallyDefined() )
111         {
112             Map.Entry<String, Archetype> found =
113                 findArchetype( archetypes, request.getArchetypeGroupId(), request.getArchetypeArtifactId() );
114 
115             if ( found != null )
116             {
117                 String catalogKey = found.getKey();
118                 Archetype archetype = found.getValue();
119 
120                 updateDefinition( definition, archetype, catalogKey );
121 
122                 getLogger().info( "Archetype " + archetype + " found in catalog " + catalogKey );
123             }
124             else
125             {
126                 getLogger().warn( "Specified archetype not found." );
127                 if ( interactiveMode.booleanValue() )
128                 {
129                     definition.setVersion( null );
130                     definition.setGroupId( null );
131                     definition.setArtifactId( null );
132                 }
133             }
134         }
135 
136         // set the defaults - only group and version can be auto-defaulted
137         if ( definition.getGroupId() == null )
138         {
139             definition.setGroupId( DEFAULT_ARCHETYPE_GROUPID );
140         }
141         if ( definition.getVersion() == null )
142         {
143             definition.setVersion( DEFAULT_ARCHETYPE_VERSION );
144         }
145 
146         if ( !definition.isPartiallyDefined() )
147         {
148             // if artifact ID is set to its default, we still prompt to confirm
149             if ( definition.getArtifactId() == null )
150             {
151                 getLogger().info(
152                     "No archetype defined. Using " + DEFAULT_ARCHETYPE_ARTIFACTID + " (" + definition.getGroupId() + ":"
153                         + DEFAULT_ARCHETYPE_ARTIFACTID + ":" + definition.getVersion() + ")" );
154                 definition.setArtifactId( DEFAULT_ARCHETYPE_ARTIFACTID );
155             }
156 
157             if ( interactiveMode.booleanValue() && ( archetypes.size() > 0 ) )
158             {
159                 Archetype selectedArchetype = archetypeSelectionQueryer.selectArchetype( archetypes, definition );
160 
161                 String catalogKey = getCatalogKey( archetypes, selectedArchetype );
162 
163                 updateDefinition( definition, selectedArchetype, catalogKey );
164             }
165 
166             // Make sure the groupId and artifactId are valid, the version may just default to
167             // the latest release.
168             if ( !definition.isPartiallyDefined() )
169             {
170                 throw new ArchetypeSelectionFailure( "No valid archetypes could be found to choose." );
171             }
172         }
173 
174         // finally update the request with gathered information
175         definition.updateRequest( request );
176     }
177 
178 
179     private Map<String, List<Archetype>> getArchetypesByCatalog( String catalogs )
180     {
181         if ( catalogs == null )
182         {
183             throw new NullPointerException( "Catalogs cannot be null" );
184         }
185 
186         Map<String, List<Archetype>> archetypes = new LinkedHashMap<String, List<Archetype>>();
187 
188         for ( String catalog : StringUtils.split( catalogs, "," ) )
189         {
190             if ( "internal".equalsIgnoreCase( catalog ) )
191             {
192                 archetypes.put( "internal", archetypeManager.getInternalCatalog().getArchetypes() );
193             }
194             else if ( "local".equalsIgnoreCase( catalog ) )
195             {
196                 archetypes.put( "local", archetypeManager.getDefaultLocalCatalog().getArchetypes() );
197             }
198             else if ( "remote".equalsIgnoreCase( catalog ) )
199             {
200                 List<Archetype> archetypesFromRemote = archetypeManager.getRemoteCatalog().getArchetypes();
201                 if ( archetypesFromRemote.size() > 0 )
202                 {
203                     archetypes.put( "remote", archetypesFromRemote );
204                 }
205                 else
206                 {
207                     getLogger().warn( "No archetype found in remote catalog. Defaulting to internal catalog" );
208                     archetypes.put( "internal", archetypeManager.getInternalCatalog().getArchetypes() );
209                 }
210             }
211             else if ( catalog.startsWith( "file://" ) )
212             {
213                 String path = catalog.substring( 7 );
214                 archetypes.put( catalog, archetypeManager.getLocalCatalog( path ).getArchetypes() );
215             }
216             else if ( catalog.startsWith( "http://" ) || catalog.startsWith( "https://" ) )
217             {
218                 archetypes.put( catalog, archetypeManager.getRemoteCatalog( catalog ).getArchetypes() );
219             }
220         }
221 
222         if ( archetypes.size() == 0 )
223         {
224             getLogger().info( "No catalog defined. Using internal catalog" );
225 
226             archetypes.put( "internal", archetypeManager.getInternalCatalog().getArchetypes() );
227         }
228         return archetypes;
229     }
230 
231     private void updateRepository( ArchetypeDefinition definition, Archetype archetype, String catalogKey )
232     {
233         String repository = archetype.getRepository();
234         if ( StringUtils.isNotEmpty( repository ) )
235         {
236             definition.setRepository( repository );
237         }
238         else if ( catalogKey.indexOf( ':' ) > 1 )
239         {
240             // file: or http:
241             int lastIndex = catalogKey.lastIndexOf( '/' );
242             String catalogBase = catalogKey.substring( 0, ( lastIndex > 7 ? lastIndex : catalogKey.length() ) );
243             definition.setRepository( catalogBase );
244         }
245     }
246 
247     private void updateDefinition( ArchetypeDefinition definition, Archetype archetype, String catalogKey )
248     {
249         definition.setGroupId( archetype.getGroupId() );
250         definition.setArtifactId( archetype.getArtifactId() );
251         definition.setVersion( archetype.getVersion() );
252         definition.setName( archetype.getArtifactId() );
253         updateRepository( definition, archetype, catalogKey );
254         definition.setGoals( StringUtils.join( archetype.getGoals().iterator(), "," ) );
255     }
256 
257     public void setArchetypeSelectionQueryer( ArchetypeSelectionQueryer archetypeSelectionQueryer )
258     {
259         this.archetypeSelectionQueryer = archetypeSelectionQueryer;
260     }
261 
262     private String getCatalogKey( Map<String, List<Archetype>> archetypes, Archetype selectedArchetype )
263     {
264         for ( Map.Entry<String, List<Archetype>> entry : archetypes.entrySet() )
265         {
266             List<Archetype> catalog = entry.getValue();
267 
268             if ( catalog.contains( selectedArchetype ) )
269             {
270                 return entry.getKey();
271             }
272         }
273         return "";
274     }
275 
276     private Map.Entry<String, Archetype> findArchetype( Map<String, List<Archetype>> archetypes, String groupId,
277                                                         String artifactId )
278     {
279         Archetype example = new Archetype();
280         example.setGroupId( groupId );
281         example.setArtifactId( artifactId );
282 
283         for ( Map.Entry<String, List<Archetype>> entry : archetypes.entrySet() )
284         {
285             List<Archetype> catalog = entry.getValue();
286 
287             if ( catalog.contains( example ) )
288             {
289                 Archetype archetype = catalog.get( catalog.indexOf( example ) );
290 
291                 return newMapEntry( entry.getKey(), archetype );
292             }
293         }
294 
295         return null;
296     }
297 
298     private static <K, V> Map.Entry<K, V> newMapEntry( K key, V value )
299     {
300         Map<K, V> map = new HashMap<K, V>( 1 );
301         map.put( key, value );
302 
303         return map.entrySet().iterator().next();
304     }
305 }