1 package org.apache.maven.index.creator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27
28 import org.apache.lucene.document.Document;
29 import org.apache.maven.index.ArtifactContext;
30 import org.apache.maven.index.ArtifactInfo;
31 import org.apache.maven.index.IndexerField;
32 import org.apache.maven.index.context.IndexCreator;
33 import org.apache.maven.index.util.zip.ZipFacade;
34 import org.apache.maven.index.util.zip.ZipHandle;
35 import org.codehaus.plexus.component.annotations.Component;
36
37
38
39
40
41
42
43
44
45 @Component( role = IndexCreator.class, hint = MavenArchetypeArtifactInfoIndexCreator.ID )
46 public class MavenArchetypeArtifactInfoIndexCreator
47 extends AbstractIndexCreator
48 {
49 public static final String ID = "maven-archetype";
50
51 private static final String MAVEN_ARCHETYPE_PACKAGING = "maven-archetype";
52
53 private static final String[] ARCHETYPE_XML_LOCATIONS = { "META-INF/maven/archetype.xml", "META-INF/archetype.xml",
54 "META-INF/maven/archetype-metadata.xml" };
55
56 public MavenArchetypeArtifactInfoIndexCreator()
57 {
58 super( ID, Arrays.asList( MinimalArtifactInfoIndexCreator.ID ) );
59 }
60
61 public void populateArtifactInfo( ArtifactContext ac )
62 {
63 File artifact = ac.getArtifact();
64
65 ArtifactInfo ai = ac.getArtifactInfo();
66
67
68 if ( artifact != null && artifact.isFile() && !MAVEN_ARCHETYPE_PACKAGING.equals( ai.packaging )
69 && artifact.getName().endsWith( ".jar" ) )
70 {
71
72
73
74
75 checkMavenArchetype( ai, artifact );
76 }
77 }
78
79
80
81
82
83
84
85 private void checkMavenArchetype( ArtifactInfo ai, File artifact )
86 {
87 ZipHandle handle = null;
88
89 try
90 {
91 handle = ZipFacade.getZipHandle( artifact );
92
93 for ( String path : ARCHETYPE_XML_LOCATIONS )
94 {
95 if ( handle.hasEntry( path ) )
96 {
97 ai.packaging = MAVEN_ARCHETYPE_PACKAGING;
98
99 return;
100 }
101 }
102 }
103 catch ( Exception e )
104 {
105 if ( getLogger().isDebugEnabled() )
106 {
107 getLogger().info(
108 "Failed to parse Maven artifact " + artifact.getAbsolutePath() + " due to exception:", e );
109 }
110 else
111 {
112 getLogger().info(
113 "Failed to parse Maven artifact " + artifact.getAbsolutePath() + " due to " + e.getMessage() );
114 }
115 }
116 finally
117 {
118 try
119 {
120 ZipFacade.close( handle );
121 }
122 catch ( IOException ex )
123 {
124 }
125 }
126 }
127
128 public void updateDocument( ArtifactInfo ai, Document doc )
129 {
130
131 }
132
133 public boolean updateArtifactInfo( Document doc, ArtifactInfo ai )
134 {
135
136
137 return false;
138 }
139
140
141
142 @Override
143 public String toString()
144 {
145 return ID;
146 }
147
148 public Collection<IndexerField> getIndexerFields()
149 {
150
151 return Collections.emptyList();
152 }
153 }