diff --git a/maven-repository-indexer/pom.xml b/maven-repository-indexer/pom.xml index 022932a31..6b3479117 100644 --- a/maven-repository-indexer/pom.xml +++ b/maven-repository-indexer/pom.xml @@ -33,6 +33,10 @@ org.apache.maven maven-artifact-manager + + org.apache.maven + maven-project + org.apache.maven maven-model diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java index e422afb71..c6a124c69 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactory.java @@ -18,21 +18,22 @@ package org.apache.maven.repository.indexing.record; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.indexing.RepositoryIndexException; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3DomBuilder; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -64,6 +65,11 @@ public class StandardArtifactIndexRecordFactory */ private ArtifactFactory artifactFactory; + /** + * @plexus.requirement + */ + private MavenProjectBuilder projectBuilder; + private static final String PLUGIN_METADATA_NAME = "META-INF/maven/plugin.xml"; private static final String ARCHETYPE_METADATA_NAME = "META-INF/maven/archetype.xml"; @@ -120,12 +126,12 @@ public class StandardArtifactIndexRecordFactory artifact.getRepository().pathOf( pomArtifact ) ); if ( pomFile.exists() ) { - populatePomEntries( readPom( pomFile ), record ); + populatePomEntries( readPom( pomArtifact, artifact.getRepository() ), record ); } } else { - Model model = readPom( file ); + Model model = readPom( artifact, artifact.getRepository() ); if ( !"pom".equals( model.getPackaging() ) ) { @@ -158,33 +164,21 @@ public class StandardArtifactIndexRecordFactory */ } - private Model readPom( File file ) + private Model readPom( Artifact artifact, ArtifactRepository repository ) throws RepositoryIndexException { - MavenXpp3Reader r = new MavenXpp3Reader(); - - FileReader reader = null; + // TODO: will this pollute with local repo metadata? + Model model; try { - reader = new FileReader( file ); - return r.read( reader ); + MavenProject project = projectBuilder.buildFromRepository( artifact, Collections.EMPTY_LIST, repository ); + model = project.getModel(); } - catch ( FileNotFoundException e ) + catch ( ProjectBuildingException e ) { - throw new RepositoryIndexException( "Unable to find requested POM: " + e.getMessage(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Unable to read POM: " + e.getMessage(), e ); - } - catch ( XmlPullParserException xe ) - { - throw new RepositoryIndexException( "Unable to parse POM: " + xe.getMessage(), xe ); - } - finally - { - IOUtil.close( reader ); + throw new RepositoryIndexException( "Unable to read project: " + e.getMessage(), e ); } + return model; } private void populateArchiveEntries( List files, StandardArtifactIndexRecord record, File artifactFile ) diff --git a/maven-repository-indexer/src/site/apt/design.apt b/maven-repository-indexer/src/site/apt/design.apt index 5ecb476d5..65865777e 100644 --- a/maven-repository-indexer/src/site/apt/design.apt +++ b/maven-repository-indexer/src/site/apt/design.apt @@ -67,7 +67,7 @@ Indexer Design dependency conflict ID, Lucene cannot delete by a combination of terms, so would require storing an additional field in the index where the file already exists. - The plugin prefix can be found either from inside the plugin JAR (<<>>), or from the + The plugin prefix could be found either from inside the plugin JAR (<<>>), or from the repository metadata for the plugin's group. For simplicity, the first approach will be used. This means at present there is no need to index the repository metadata, however that may be considered in future. @@ -76,9 +76,10 @@ Indexer Design if found set its packaging to <<>>. In the future, this handling will be deprecated as the POMs can start using the appropriate packaging. - The index is shared among multiple repositories. The source repository is recorded in the index record. The indexer - should complain if an artifact is attempted to be updated from a different repository at a later date to avoid - duplicates. Ideally, the discovery/conversion mechanisms would deal with this before reaching the indexer. + The index is shared among multiple repositories. The source repository is recorded in the index record. The + discovery/conversion/reporting mechanisms are expected to deal with duplicates before reaching the indexer, so if the + indexer encounters an artifact from a different repository than it was already added, it will simply replace the + record. When indexing metadata from a POM, the POM should be loaded using the Maven project builder so that inheritance and interpolation are performed. This ensures that the record is as complete as possible, and that searching by diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java index 27219f877..4eb47c700 100644 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/record/StandardArtifactIndexRecordFactoryTest.java @@ -113,6 +113,34 @@ public class StandardArtifactIndexRecordFactoryTest assertEquals( "check record", expectedRecord, record ); } + public void testIndexedJarWithParentPom() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-child-pom" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( "A\nb.B\nb.c.C\n" ); + expectedRecord.setArtifactId( "test-child-pom" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setVersion( "1.0" ); + expectedRecord.setFiles( "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class\n" ); + expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); + expectedRecord.setType( "jar" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setPackaging( "jar" ); + expectedRecord.setProjectName( "Child Project" ); + expectedRecord.setProjectDescription( "Description" ); + expectedRecord.setInceptionYear( "2005" ); + + assertEquals( "check record", expectedRecord, record ); + } + public void testIndexedPom() throws RepositoryIndexException { diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/parent-pom/1/parent-pom-1.pom b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/parent-pom/1/parent-pom-1.pom new file mode 100644 index 000000000..162f02ac6 --- /dev/null +++ b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/parent-pom/1/parent-pom-1.pom @@ -0,0 +1,38 @@ + + + 4.0.0 + org.apache.maven.repository.record + parent-pom + 1 + pom + Test Parent POM + Description + 2005 + + + junit + junit + 3.8.1 + test + + + + test-child-pom + + + diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-child-pom/1.0/test-child-pom-1.0.jar b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-child-pom/1.0/test-child-pom-1.0.jar new file mode 100644 index 000000000..b78be2eb8 Binary files /dev/null and b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-child-pom/1.0/test-child-pom-1.0.jar differ diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-child-pom/1.0/test-child-pom-1.0.pom b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-child-pom/1.0/test-child-pom-1.0.pom new file mode 100644 index 000000000..696daf3aa --- /dev/null +++ b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/test-child-pom/1.0/test-child-pom-1.0.pom @@ -0,0 +1,28 @@ + + + + 4.0.0 + + org.apache.maven.repository.record + parent-pom + 1 + + test-child-pom + 1.0 + Child Project +