View Javadoc
1   package org.apache.maven.archetype.repositorycrawler;
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.io.FileUtils;
23  
24  import org.apache.maven.archetype.catalog.Archetype;
25  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
26  import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer;
27  import org.apache.maven.archetype.common.ArchetypeArtifactManager;
28  import org.apache.maven.archetype.exception.UnknownArchetype;
29  import org.apache.maven.model.Model;
30  
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.component.annotations.Requirement;
33  import org.codehaus.plexus.logging.AbstractLogEnabled;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.WriterFactory;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  
38  import java.io.File;
39  import java.io.IOException;
40  import java.io.Writer;
41  
42  import java.util.Iterator;
43  
44  /**
45   * @author            rafale
46   */
47  @Component( role = RepositoryCrawler.class )
48  public class DefaultRepositoryCrawler
49      extends AbstractLogEnabled
50      implements RepositoryCrawler
51  {
52      @Requirement
53      private ArchetypeArtifactManager archetypeArtifactManager;
54  
55      @Override
56      public ArchetypeCatalog crawl( File repository )
57      {
58          if ( !repository.isDirectory() )
59          {
60              getLogger().warn( "File is not a directory" );
61              return null;
62          }
63  
64          ArchetypeCatalog catalog = new ArchetypeCatalog();
65          @SuppressWarnings( "unchecked" )
66          Iterator<File> jars = FileUtils.listFiles( repository, new String[] { "jar" }, true ).iterator();
67  
68          while ( jars.hasNext() )
69          {
70              File jar = jars.next();
71              getLogger().info( "Scanning " + jar );
72              if ( archetypeArtifactManager.isFileSetArchetype( jar ) || archetypeArtifactManager.isOldArchetype( jar ) )
73              {
74                  try
75                  {
76                      Archetype archetype = new Archetype();
77  
78                      Model pom = archetypeArtifactManager.getArchetypePom( jar );
79  
80                      if ( archetypeArtifactManager.isFileSetArchetype( jar ) )
81                      {
82                          org.apache.maven.archetype.metadata.ArchetypeDescriptor descriptor =
83                              archetypeArtifactManager.getFileSetArchetypeDescriptor( jar );
84                          archetype.setDescription( descriptor.getName() );
85                      }
86                      else
87                      {
88                          org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor descriptor =
89                              archetypeArtifactManager.getOldArchetypeDescriptor( jar );
90                          archetype.setDescription( descriptor.getId() );
91                      }
92                      if ( pom != null )
93                      {
94                          if ( pom.getGroupId() != null )
95                          {
96                              archetype.setGroupId( pom.getGroupId() );
97                          }
98                          else
99                          {
100                             archetype.setGroupId( pom.getParent().getGroupId() );
101                         }
102                         archetype.setArtifactId( pom.getArtifactId() );
103                         if ( pom.getVersion() != null )
104                         {
105                             archetype.setVersion( pom.getVersion() );
106                         }
107                         else
108                         {
109                             archetype.setVersion( pom.getParent().getVersion() );
110                         }
111                         getLogger().info( "\tArchetype " + archetype + " found in pom" );
112                     }
113                     else
114                     {
115                         String version = jar.getParentFile().getName();
116 
117                         String artifactId = jar.getParentFile().getParentFile().getName();
118 
119                         String groupIdSep =
120                             StringUtils.replace( jar.getParentFile().getParentFile().getParentFile().getPath(),
121                                                  repository.getPath(), "" );
122                         String groupId = groupIdSep.replace( File.separatorChar, '.' );
123                         groupId = groupId.startsWith( "." ) ? groupId.substring( 1 ) : groupId;
124                         groupId = groupId.endsWith( "." ) ? groupId.substring( 0, groupId.length() - 1 ) : groupId;
125 
126                         archetype.setGroupId( groupId );
127                         archetype.setArtifactId( artifactId );
128                         archetype.setVersion( version );
129 
130                         getLogger().info( "\tArchetype " + archetype + " defined by repository path" );
131                     } // end if-else
132 
133                     catalog.addArchetype( archetype );
134                 }
135                 catch ( XmlPullParserException ex )
136                 {
137                     ex.printStackTrace();
138                 }
139                 catch ( IOException ex )
140                 {
141                     ex.printStackTrace();
142                 }
143                 catch ( UnknownArchetype ex )
144                 {
145                     ex.printStackTrace();
146                 } // end try-catch
147             } // end if
148         } // end while
149         return catalog;
150     }
151 
152     @Override
153     public boolean writeCatalog( ArchetypeCatalog archetypeCatalog, File archetypeCatalogFile )
154     {
155         ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
156 
157         try ( Writer fileWriter = WriterFactory.newXmlWriter( archetypeCatalogFile ) )
158         {
159             catalogWriter.write( fileWriter, archetypeCatalog );
160             return true;
161         }
162         catch ( IOException ex )
163         {
164             getLogger().warn( "Catalog can not be writen to " + archetypeCatalogFile, ex );
165             return false;
166         }
167     }
168 }