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.BufferedInputStream; |
23 | |
import java.io.File; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Arrays; |
29 | |
import java.util.Collection; |
30 | |
|
31 | |
import org.apache.lucene.document.Document; |
32 | |
import org.apache.lucene.document.Field.Index; |
33 | |
import org.apache.lucene.document.Field.Store; |
34 | |
import org.apache.maven.index.ArtifactContext; |
35 | |
import org.apache.maven.index.ArtifactInfo; |
36 | |
import org.apache.maven.index.IndexerField; |
37 | |
import org.apache.maven.index.IndexerFieldVersion; |
38 | |
import org.apache.maven.index.MAVEN; |
39 | |
import org.apache.maven.index.context.IndexCreator; |
40 | |
import org.apache.maven.index.util.zip.ZipFacade; |
41 | |
import org.apache.maven.index.util.zip.ZipHandle; |
42 | |
import org.codehaus.plexus.component.annotations.Component; |
43 | |
import org.codehaus.plexus.configuration.PlexusConfiguration; |
44 | |
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; |
45 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
@Component( role = IndexCreator.class, hint = MavenPluginArtifactInfoIndexCreator.ID ) |
55 | |
public class MavenPluginArtifactInfoIndexCreator |
56 | |
extends AbstractIndexCreator |
57 | |
{ |
58 | |
public static final String ID = "maven-plugin"; |
59 | |
|
60 | |
private static final String MAVEN_PLUGIN_PACKAGING = "maven-plugin"; |
61 | |
|
62 | 1 | public static final IndexerField FLD_PLUGIN_PREFIX = new IndexerField( MAVEN.PLUGIN_PREFIX, IndexerFieldVersion.V1, |
63 | |
"px", "MavenPlugin prefix (as keyword, stored)", Store.YES, Index.NOT_ANALYZED ); |
64 | |
|
65 | 1 | public static final IndexerField FLD_PLUGIN_GOALS = new IndexerField( MAVEN.PLUGIN_GOALS, IndexerFieldVersion.V1, |
66 | |
"gx", "MavenPlugin goals (as keyword, stored)", Store.YES, Index.ANALYZED ); |
67 | |
|
68 | |
public MavenPluginArtifactInfoIndexCreator() |
69 | |
{ |
70 | 205 | super( ID, Arrays.asList( MinimalArtifactInfoIndexCreator.ID ) ); |
71 | 205 | } |
72 | |
|
73 | |
public void populateArtifactInfo( ArtifactContext ac ) |
74 | |
{ |
75 | 5313 | File artifact = ac.getArtifact(); |
76 | |
|
77 | 5313 | ArtifactInfo ai = ac.getArtifactInfo(); |
78 | |
|
79 | |
|
80 | 5313 | if ( artifact != null && MAVEN_PLUGIN_PACKAGING.equals( ai.packaging ) && artifact.getName().endsWith( ".jar" ) ) |
81 | |
{ |
82 | |
|
83 | |
|
84 | |
|
85 | 335 | checkMavenPlugin( ai, artifact ); |
86 | |
} |
87 | 5313 | } |
88 | |
|
89 | |
private void checkMavenPlugin( ArtifactInfo ai, File artifact ) |
90 | |
{ |
91 | 335 | ZipHandle handle = null; |
92 | |
|
93 | |
try |
94 | |
{ |
95 | 335 | handle = ZipFacade.getZipHandle( artifact ); |
96 | |
|
97 | 335 | final String pluginDescriptorPath = "META-INF/maven/plugin.xml"; |
98 | |
|
99 | 335 | if ( handle.hasEntry( pluginDescriptorPath ) ) |
100 | |
{ |
101 | 335 | InputStream is = new BufferedInputStream( handle.getEntryContent( pluginDescriptorPath ) ); |
102 | |
|
103 | |
try |
104 | |
{ |
105 | |
|
106 | 335 | PlexusConfiguration plexusConfig = |
107 | |
new XmlPlexusConfiguration( Xpp3DomBuilder.build( new InputStreamReader( is ) ) ); |
108 | |
|
109 | 335 | ai.prefix = plexusConfig.getChild( "goalPrefix" ).getValue(); |
110 | |
|
111 | 335 | ai.goals = new ArrayList<String>(); |
112 | |
|
113 | 335 | PlexusConfiguration[] mojoConfigs = plexusConfig.getChild( "mojos" ).getChildren( "mojo" ); |
114 | |
|
115 | 1833 | for ( PlexusConfiguration mojoConfig : mojoConfigs ) |
116 | |
{ |
117 | 1498 | ai.goals.add( mojoConfig.getChild( "goal" ).getValue() ); |
118 | |
} |
119 | |
} |
120 | |
finally |
121 | |
{ |
122 | 335 | is.close(); |
123 | 335 | } |
124 | |
} |
125 | |
} |
126 | 0 | catch ( Exception e ) |
127 | |
{ |
128 | 0 | if ( getLogger().isDebugEnabled() ) |
129 | |
{ |
130 | 0 | getLogger().info( |
131 | |
"Failed to parse Maven artifact " + artifact.getAbsolutePath() + " due to exception:", e ); |
132 | |
} |
133 | |
else |
134 | |
{ |
135 | 0 | getLogger().info( |
136 | |
"Failed to parse Maven artifact " + artifact.getAbsolutePath() + " due to " + e.getMessage() ); |
137 | |
} |
138 | |
} |
139 | |
finally |
140 | |
{ |
141 | 0 | try |
142 | |
{ |
143 | 335 | ZipFacade.close( handle ); |
144 | |
} |
145 | 0 | catch ( IOException e ) |
146 | |
{ |
147 | 335 | } |
148 | 0 | } |
149 | 335 | } |
150 | |
|
151 | |
public void updateDocument( ArtifactInfo ai, Document doc ) |
152 | |
{ |
153 | 6424 | if ( ai.prefix != null ) |
154 | |
{ |
155 | 382 | doc.add( FLD_PLUGIN_PREFIX.toField( ai.prefix ) ); |
156 | |
} |
157 | |
|
158 | 6424 | if ( ai.goals != null ) |
159 | |
{ |
160 | 382 | doc.add( FLD_PLUGIN_GOALS.toField( ArtifactInfo.lst2str( ai.goals ) ) ); |
161 | |
} |
162 | 6424 | } |
163 | |
|
164 | |
public boolean updateArtifactInfo( Document doc, ArtifactInfo ai ) |
165 | |
{ |
166 | 221525 | boolean res = false; |
167 | |
|
168 | 221525 | if ( "maven-plugin".equals( ai.packaging ) ) |
169 | |
{ |
170 | 736 | ai.prefix = doc.get( ArtifactInfo.PLUGIN_PREFIX ); |
171 | |
|
172 | 736 | String goals = doc.get( ArtifactInfo.PLUGIN_GOALS ); |
173 | |
|
174 | 736 | if ( goals != null ) |
175 | |
{ |
176 | 729 | ai.goals = ArtifactInfo.str2lst( goals ); |
177 | |
} |
178 | |
|
179 | 736 | res = true; |
180 | |
} |
181 | |
|
182 | 221525 | return res; |
183 | |
} |
184 | |
|
185 | |
@Override |
186 | |
public String toString() |
187 | |
{ |
188 | 0 | return ID; |
189 | |
} |
190 | |
|
191 | |
public Collection<IndexerField> getIndexerFields() |
192 | |
{ |
193 | 337 | return Arrays.asList( FLD_PLUGIN_GOALS, FLD_PLUGIN_PREFIX ); |
194 | |
} |
195 | |
} |