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