1 package org.apache.maven.repository.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.index.Term;
20 import org.apache.lucene.search.TermQuery;
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.model.Model;
24 import org.apache.maven.repository.digest.Digester;
25 import org.apache.maven.repository.digest.DigesterException;
26 import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
27 import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
28 import org.apache.maven.repository.indexing.RepositoryIndexSearchException;
29 import org.apache.maven.repository.indexing.lucene.LuceneQuery;
30 import org.apache.maven.repository.indexing.record.StandardArtifactIndexRecord;
31 import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
34 import java.util.Iterator;
35 import java.util.List;
38 * Validates an artifact file for duplicates within the same groupId based from what's available in a repository index.
40 * @author Edwin Punzalan
41 * @plexus.component role="org.apache.maven.repository.reporting.ArtifactReportProcessor" role-hint="duplicate"
43 public class DuplicateArtifactFileReportProcessor
44 implements ArtifactReportProcessor
49 private Digester digester;
54 private RepositoryArtifactIndexFactory indexFactory;
57 * @plexus.configuration
59 private String indexDirectory;
61 public void processArtifact( Model model, Artifact artifact, ArtifactReporter reporter,
62 ArtifactRepository repository )
63 throws ReportProcessorException
65 if ( artifact.getFile() != null )
67 RepositoryArtifactIndex index = indexFactory.createStandardIndex( new File( indexDirectory ) );
72 checksum = digester.createChecksum( artifact.getFile(), Digester.MD5 );
74 catch ( DigesterException e )
76 throw new ReportProcessorException( "Failed to generate checksum", e );
81 List results = index.search( new LuceneQuery(
82 new TermQuery( new Term( StandardIndexRecordFields.MD5, checksum.toLowerCase() ) ) ) );
84 if ( results.isEmpty() )
86 reporter.addSuccess( artifact );
90 boolean hasDuplicates = false;
91 for ( Iterator i = results.iterator(); i.hasNext(); )
93 StandardArtifactIndexRecord result = (StandardArtifactIndexRecord) i.next();
95 //make sure it is not the same artifact
96 if ( !result.getFilename().equals( repository.pathOf( artifact ) ) )
98 //report only duplicates from the same groupId
99 String groupId = artifact.getGroupId();
100 if ( groupId.equals( result.getGroupId() ) )
102 hasDuplicates = true;
103 reporter.addFailure( artifact, "Found duplicate for " + artifact.getId() );
108 if ( !hasDuplicates )
110 reporter.addSuccess( artifact );
114 catch ( RepositoryIndexSearchException e )
116 throw new ReportProcessorException( "Failed to search in index", e );
121 reporter.addWarning( artifact, "Artifact file is null" );