[MRM-127] read project using project builder to correctly handle inheritence and interpolation

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@425953 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2006-07-27 04:23:46 +00:00
parent 1c20fd9fd8
commit 8da406db53
7 changed files with 123 additions and 30 deletions

View File

@ -33,6 +33,10 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact-manager</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>

View File

@ -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 )

View File

@ -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 (<<<META-INF/maven/plugin.xml>>>), or from the
The plugin prefix could be found either from inside the plugin JAR (<<<META-INF/maven/plugin.xml>>>), 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 <<<maven-archetype>>>. 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

View File

@ -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
{

View File

@ -0,0 +1,38 @@
<!--
~ Copyright 2005-2006 The Apache Software Foundation.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.repository.record</groupId>
<artifactId>parent-pom</artifactId>
<version>1</version>
<packaging>pom</packaging>
<name>Test Parent POM</name>
<description>Description</description>
<inceptionYear>2005</inceptionYear>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>test-child-pom</module>
</modules>
</project>

View File

@ -0,0 +1,28 @@
<!--
~ Copyright 2005-2006 The Apache Software Foundation.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven.repository.record</groupId>
<artifactId>parent-pom</artifactId>
<version>1</version>
</parent>
<artifactId>test-child-pom</artifactId>
<version>1.0</version>
<name>Child Project</name>
</project>