1 package org.apache.maven.index.examples;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.MultiBits;
25 import org.apache.lucene.search.BooleanClause.Occur;
26 import org.apache.lucene.search.BooleanQuery;
27 import org.apache.lucene.search.IndexSearcher;
28 import org.apache.lucene.search.Query;
29 import org.apache.lucene.util.Bits;
30 import org.apache.maven.index.ArtifactInfo;
31 import org.apache.maven.index.ArtifactInfoFilter;
32 import org.apache.maven.index.ArtifactInfoGroup;
33 import org.apache.maven.index.Field;
34 import org.apache.maven.index.FlatSearchRequest;
35 import org.apache.maven.index.FlatSearchResponse;
36 import org.apache.maven.index.GroupedSearchRequest;
37 import org.apache.maven.index.GroupedSearchResponse;
38 import org.apache.maven.index.Grouping;
39 import org.apache.maven.index.Indexer;
40 import org.apache.maven.index.IteratorSearchRequest;
41 import org.apache.maven.index.IteratorSearchResponse;
42 import org.apache.maven.index.MAVEN;
43 import org.apache.maven.index.context.IndexCreator;
44 import org.apache.maven.index.context.IndexUtils;
45 import org.apache.maven.index.context.IndexingContext;
46 import org.apache.maven.index.expr.SourcedSearchExpression;
47 import org.apache.maven.index.expr.UserInputSearchExpression;
48 import org.apache.maven.index.search.grouping.GAGrouping;
49 import org.apache.maven.index.updater.IndexUpdateRequest;
50 import org.apache.maven.index.updater.IndexUpdateResult;
51 import org.apache.maven.index.updater.IndexUpdater;
52 import org.apache.maven.index.updater.ResourceFetcher;
53 import org.apache.maven.index.updater.WagonHelper;
54 import org.apache.maven.wagon.Wagon;
55 import org.apache.maven.wagon.events.TransferEvent;
56 import org.apache.maven.wagon.events.TransferListener;
57 import org.apache.maven.wagon.observers.AbstractTransferListener;
58 import org.codehaus.plexus.DefaultContainerConfiguration;
59 import org.codehaus.plexus.DefaultPlexusContainer;
60 import org.codehaus.plexus.PlexusConstants;
61 import org.codehaus.plexus.PlexusContainer;
62 import org.codehaus.plexus.PlexusContainerException;
63 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
64 import org.codehaus.plexus.util.StringUtils;
65 import org.eclipse.aether.util.version.GenericVersionScheme;
66 import org.eclipse.aether.version.InvalidVersionSpecificationException;
67 import org.eclipse.aether.version.Version;
68
69 import java.io.File;
70 import java.io.IOException;
71 import java.util.ArrayList;
72 import java.util.Collections;
73 import java.util.Date;
74 import java.util.List;
75 import java.util.Map;
76
77
78
79
80 public class BasicUsageExample
81 {
82 public static void main( String[] args )
83 throws Exception
84 {
85 new BasicUsageExample().perform();
86 }
87
88
89
90 private final PlexusContainer plexusContainer;
91
92 private final Indexer indexer;
93
94 private final IndexUpdater indexUpdater;
95
96 private final Wagon httpWagon;
97
98 private IndexingContext centralContext;
99
100 public BasicUsageExample()
101 throws PlexusContainerException, ComponentLookupException
102 {
103
104
105
106
107
108 final DefaultContainerConfiguration config = new DefaultContainerConfiguration();
109 config.setClassPathScanning( PlexusConstants.SCANNING_INDEX );
110 this.plexusContainer = new DefaultPlexusContainer( config );
111
112
113 this.indexer = plexusContainer.lookup( Indexer.class );
114 this.indexUpdater = plexusContainer.lookup( IndexUpdater.class );
115
116 this.httpWagon = plexusContainer.lookup( Wagon.class, "http" );
117
118 }
119
120 public void perform()
121 throws IOException, ComponentLookupException, InvalidVersionSpecificationException
122 {
123
124 File centralLocalCache = new File( "target/central-cache" );
125 File centralIndexDir = new File( "target/central-index" );
126
127
128 List<IndexCreator> indexers = new ArrayList<>();
129 indexers.add( plexusContainer.lookup( IndexCreator.class, "min" ) );
130 indexers.add( plexusContainer.lookup( IndexCreator.class, "jarContent" ) );
131 indexers.add( plexusContainer.lookup( IndexCreator.class, "maven-plugin" ) );
132
133
134 centralContext =
135 indexer.createIndexingContext( "central-context", "central", centralLocalCache, centralIndexDir,
136 "https://repo1.maven.org/maven2", null, true, true, indexers );
137
138
139
140
141
142
143 if ( true )
144 {
145 System.out.println( "Updating Index..." );
146 System.out.println( "This might take a while on first run, so please be patient!" );
147
148
149 TransferListener listener = new AbstractTransferListener()
150 {
151 public void transferStarted( TransferEvent transferEvent )
152 {
153 System.out.print( " Downloading " + transferEvent.getResource().getName() );
154 }
155
156 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
157 {
158 }
159
160 public void transferCompleted( TransferEvent transferEvent )
161 {
162 System.out.println( " - Done" );
163 }
164 };
165 ResourceFetcher resourceFetcher = new WagonHelper.WagonFetcher( httpWagon, listener, null, null );
166
167 Date centralContextCurrentTimestamp = centralContext.getTimestamp();
168 IndexUpdateRequest updateRequest = new IndexUpdateRequest( centralContext, resourceFetcher );
169 IndexUpdateResult updateResult = indexUpdater.fetchAndUpdateIndex( updateRequest );
170 if ( updateResult.isFullUpdate() )
171 {
172 System.out.println( "Full update happened!" );
173 }
174 else if ( updateResult.getTimestamp().equals( centralContextCurrentTimestamp ) )
175 {
176 System.out.println( "No update needed, index is up to date!" );
177 }
178 else
179 {
180 System.out.println(
181 "Incremental update happened, change covered " + centralContextCurrentTimestamp + " - "
182 + updateResult.getTimestamp() + " period." );
183 }
184
185 System.out.println();
186 }
187
188 System.out.println();
189 System.out.println( "Using index" );
190 System.out.println( "===========" );
191 System.out.println();
192
193
194
195
196
197
198 if ( false )
199 {
200 final IndexSearcher searcher = centralContext.acquireIndexSearcher();
201 try
202 {
203 final IndexReader ir = searcher.getIndexReader();
204 Bits liveDocs = MultiBits.getLiveDocs( ir );
205 for ( int i = 0; i < ir.maxDoc(); i++ )
206 {
207 if ( liveDocs == null || liveDocs.get( i ) )
208 {
209 final Document doc = ir.document( i );
210 final ArtifactInfo ai = IndexUtils.constructArtifactInfo( doc, centralContext );
211 System.out.println( ai.getGroupId() + ":" + ai.getArtifactId() + ":" + ai.getVersion() + ":"
212 + ai.getClassifier() + " (sha1=" + ai.getSha1() + ")" );
213 }
214 }
215 }
216 finally
217 {
218 centralContext.releaseIndexSearcher( searcher );
219 }
220 }
221
222
223
224
225
226 final GenericVersionScheme versionScheme = new GenericVersionScheme();
227 final String versionString = "3.1.0";
228 final Version version = versionScheme.parseVersion( versionString );
229
230
231 final Query groupIdQ =
232 indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.maven" ) );
233 final Query artifactIdQ =
234 indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "maven-plugin-api" ) );
235
236 final BooleanQuery query = new BooleanQuery.Builder()
237 .add( groupIdQ, Occur.MUST )
238 .add( artifactIdQ, Occur.MUST )
239
240 .add( indexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression( "jar" ) ), Occur.MUST )
241
242
243 .add( indexer.constructQuery( MAVEN.CLASSIFIER,
244 new SourcedSearchExpression( Field.NOT_PRESENT ) ), Occur.MUST_NOT )
245 .build();
246
247
248 final ArtifactInfoFilter versionFilter = ( ctx, ai ) ->
249 {
250 try
251 {
252 final Version aiV = versionScheme.parseVersion( ai.getVersion() );
253
254 return aiV.compareTo( version ) > 0;
255 }
256 catch ( InvalidVersionSpecificationException e )
257 {
258
259 return true;
260 }
261 };
262
263 System.out.println(
264 "Searching for all GAVs with org.apache.maven:maven-plugin-api having V greater than 3.1.0" );
265 final IteratorSearchRequest request =
266 new IteratorSearchRequest( query, Collections.singletonList( centralContext ), versionFilter );
267 final IteratorSearchResponse response = indexer.searchIterator( request );
268 for ( ArtifactInfo ai : response )
269 {
270 System.out.println( ai.toString() );
271 }
272
273
274
275
276 Query gidQ =
277 indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.maven.indexer" ) );
278 Query aidQ = indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "indexer-core" ) );
279
280 BooleanQuery bq = new BooleanQuery.Builder()
281 .add( gidQ, Occur.MUST )
282 .add( aidQ, Occur.MUST )
283 .build();
284
285 searchAndDump( indexer, "all artifacts under GA org.apache.maven.indexer:indexer-core", bq );
286
287
288 bq = new BooleanQuery.Builder()
289 .add( gidQ, Occur.MUST )
290 .add( aidQ, Occur.MUST )
291 .add( indexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression( "*" ) ), Occur.MUST_NOT )
292 .build();
293
294 searchAndDump( indexer, "main artifacts under GA org.apache.maven.indexer:indexer-core", bq );
295
296
297 searchAndDump( indexer, "SHA1 7ab67e6b20e5332a7fb4fdf2f019aec4275846c2",
298 indexer.constructQuery( MAVEN.SHA1,
299 new SourcedSearchExpression( "7ab67e6b20e5332a7fb4fdf2f019aec4275846c2" )
300 )
301 );
302
303 searchAndDump( indexer, "SHA1 7ab67e6b20 (partial hash)",
304 indexer.constructQuery( MAVEN.SHA1, new UserInputSearchExpression( "7ab67e6b20" ) ) );
305
306
307 searchAndDump( indexer, "classname DefaultNexusIndexer (note: Central does not publish classes in the index)",
308 indexer.constructQuery( MAVEN.CLASSNAMES,
309 new UserInputSearchExpression( "DefaultNexusIndexer" ) ) );
310
311
312 bq = new BooleanQuery.Builder()
313 .add( indexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression( "maven-plugin" ) ), Occur.MUST )
314 .add( indexer.constructQuery( MAVEN.GROUP_ID,
315 new SourcedSearchExpression( "org.apache.maven.plugins" ) ), Occur.MUST )
316 .build();
317
318 searchGroupedAndDumpFlat( indexer, "all \"canonical\" maven plugins", bq, new GAGrouping() );
319
320
321 searchGroupedAndDump( indexer, "all maven archetypes (latest versions)",
322 indexer.constructQuery( MAVEN.PACKAGING,
323 new SourcedSearchExpression( "maven-archetype" ) ),
324 new GAGrouping() );
325
326
327 indexer.closeIndexingContext( centralContext, false );
328 }
329
330 public void searchAndDump( Indexer nexusIndexer, String descr, Query q )
331 throws IOException
332 {
333 System.out.println( "Searching for " + descr );
334
335 FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q, centralContext ) );
336
337 for ( ArtifactInfo ai : response.getResults() )
338 {
339 System.out.println( ai.toString() );
340 }
341
342 System.out.println( "------" );
343 System.out.println( "Total: " + response.getTotalHitsCount() );
344 System.out.println();
345 }
346
347 private static final int MAX_WIDTH = 60;
348
349 public void searchGroupedAndDumpFlat( Indexer nexusIndexer, String descr, Query q, Grouping g )
350 throws IOException
351 {
352 System.out.println( "Searching for " + descr );
353
354 GroupedSearchResponse response = nexusIndexer.searchGrouped( new GroupedSearchRequest( q, g, centralContext ) );
355
356 for ( Map.Entry<String, ArtifactInfoGroup> entry : response.getResults().entrySet() )
357 {
358 ArtifactInfo ai = entry.getValue().getArtifactInfos().iterator().next();
359 System.out.println( "* " + ai.getGroupId() + ":" + ai.getArtifactId() + ":" + ai.getVersion() );
360 }
361
362 System.out.println( "------" );
363 System.out.println( "Total record hits: " + response.getTotalHitsCount() );
364 System.out.println();
365 }
366
367 public void searchGroupedAndDump( Indexer nexusIndexer, String descr, Query q, Grouping g )
368 throws IOException
369 {
370 System.out.println( "Searching for " + descr );
371
372 GroupedSearchResponse response = nexusIndexer.searchGrouped( new GroupedSearchRequest( q, g, centralContext ) );
373
374 for ( Map.Entry<String, ArtifactInfoGroup> entry : response.getResults().entrySet() )
375 {
376 ArtifactInfo ai = entry.getValue().getArtifactInfos().iterator().next();
377 System.out.println( "* Entry " + ai );
378 System.out.println( " Latest version: " + ai.getVersion() );
379 System.out.println( StringUtils.isBlank( ai.getDescription() )
380 ? "No description in plugin's POM."
381 : StringUtils.abbreviate( ai.getDescription(), MAX_WIDTH ) );
382 System.out.println();
383 }
384
385 System.out.println( "------" );
386 System.out.println( "Total record hits: " + response.getTotalHitsCount() );
387 System.out.println();
388 }
389 }