From 25eac2b1f80ff04f5085a853dd6e9d6a0da7f037 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Thu, 27 Jul 2006 03:32:25 +0000 Subject: [PATCH] [MRM-127] read plugin metadata from inside the JAR instead of from the repository metadata git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@425940 13f79535-47bb-0310-9956-ffa450edef68 --- .../AbstractArtifactIndexRecordFactory.java | 33 +++++-- .../StandardArtifactIndexRecordFactory.java | 88 ++++++++++++------- .../repository/record/maven-metadata.xml | 25 ------ 3 files changed, 83 insertions(+), 63 deletions(-) delete mode 100644 maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/maven-metadata.xml diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java index 89ded1b62..f077da863 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/record/AbstractArtifactIndexRecordFactory.java @@ -61,13 +61,21 @@ public abstract class AbstractArtifactIndexRecordFactory throws IOException { ZipFile zipFile = new ZipFile( file ); - List files = new ArrayList( zipFile.size() ); - - for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) + List files; + try { - ZipEntry entry = (ZipEntry) entries.nextElement(); + files = new ArrayList( zipFile.size() ); - files.add( entry.getName() ); + for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) + { + ZipEntry entry = (ZipEntry) entries.nextElement(); + + files.add( entry.getName() ); + } + } + finally + { + closeQuietly( zipFile ); } return files; } @@ -77,4 +85,19 @@ public abstract class AbstractArtifactIndexRecordFactory // TODO: verify if class is public or protected (this might require the original ZipEntry) return name.endsWith( ".class" ) && name.lastIndexOf( "$" ) < 0; } + + protected static void closeQuietly( ZipFile zipFile ) + { + try + { + if ( zipFile != null ) + { + zipFile.close(); + } + } + catch ( IOException e ) + { + // ignored + } + } } 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 ac93097fb..eb706d4f0 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,29 +18,30 @@ 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.metadata.GroupRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.Metadata; -import org.apache.maven.artifact.repository.metadata.Plugin; -import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; 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.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; /** * An index record type for the standard index. @@ -65,6 +66,8 @@ public class StandardArtifactIndexRecordFactory */ private ArtifactFactory artifactFactory; + private static final String PLUGIN_METADATA_NAME = "META-INF/maven/plugin.xml"; + public RepositoryIndexRecord createRecord( Artifact artifact ) throws RepositoryIndexException { @@ -105,7 +108,7 @@ public class StandardArtifactIndexRecordFactory record.setRepository( artifact.getRepository().getId() ); if ( files != null ) { - populateArchiveEntries( files, record ); + populateArchiveEntries( files, record, artifact.getFile() ); } if ( !"pom".equals( artifact.getType() ) ) @@ -124,20 +127,6 @@ public class StandardArtifactIndexRecordFactory { populatePomEntries( readPom( file ), record ); } - - if ( "maven-plugin".equals( record.getPackaging() ) ) - { - // Typically discovered as a JAR - record.setType( record.getPackaging() ); - - RepositoryMetadata metadata = new GroupRepositoryMetadata( artifact.getGroupId() ); - File metadataFile = new File( artifact.getRepository().getBasedir(), - artifact.getRepository().pathOfRemoteRepositoryMetadata( metadata ) ); - if ( metadataFile.exists() ) - { - populatePluginEntries( readMetadata( metadataFile ), record ); - } - } } } @@ -217,7 +206,8 @@ public class StandardArtifactIndexRecordFactory } } - private void populateArchiveEntries( List files, StandardArtifactIndexRecord record ) + private void populateArchiveEntries( List files, StandardArtifactIndexRecord record, File artifactFile ) + throws RepositoryIndexException { StringBuffer classes = new StringBuffer(); StringBuffer fileBuffer = new StringBuffer(); @@ -235,6 +225,13 @@ public class StandardArtifactIndexRecordFactory { classes.append( name.substring( 0, name.length() - 6 ).replace( '/', '.' ) ).append( "\n" ); } + else + { + if ( PLUGIN_METADATA_NAME.equals( name ) ) + { + populatePluginEntries( readPluginMetadata( artifactFile ), record ); + } + } } } @@ -242,23 +239,48 @@ public class StandardArtifactIndexRecordFactory record.setFiles( fileBuffer.toString() ); } - public void populatePluginEntries( Metadata metadata, StandardArtifactIndexRecord record ) + private Xpp3Dom readPluginMetadata( File file ) + throws RepositoryIndexException { - Map prefixes = new HashMap(); - for ( Iterator i = metadata.getPlugins().iterator(); i.hasNext(); ) - { - Plugin plugin = (Plugin) i.next(); + // TODO: would be more efficient with original ZipEntry still around - prefixes.put( plugin.getArtifactId(), plugin.getPrefix() ); + Xpp3Dom xpp3Dom; + ZipFile zipFile = null; + try + { + zipFile = new ZipFile( file ); + ZipEntry entry = zipFile.getEntry( PLUGIN_METADATA_NAME ); + xpp3Dom = Xpp3DomBuilder.build( new InputStreamReader( zipFile.getInputStream( entry ) ) ); } + catch ( ZipException e ) + { + throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); + } + catch ( IOException e ) + { + throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); + } + catch ( XmlPullParserException e ) + { + throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); + } + finally + { + closeQuietly( zipFile ); + } + return xpp3Dom; + } + + public void populatePluginEntries( Xpp3Dom metadata, StandardArtifactIndexRecord record ) + { + // Typically discovered as a JAR + record.setType( "maven-plugin" ); + + Xpp3Dom prefix = metadata.getChild( "goalPrefix" ); - if ( record.getGroupId().equals( metadata.getGroupId() ) ) + if ( prefix != null ) { - String prefix = (String) prefixes.get( record.getArtifactId() ); - if ( prefix != null ) - { - record.setPluginPrefix( prefix ); - } + record.setPluginPrefix( prefix.getValue() ); } } } diff --git a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/maven-metadata.xml b/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/maven-metadata.xml deleted file mode 100644 index afbc79b86..000000000 --- a/maven-repository-indexer/src/test/managed-repository/org/apache/maven/repository/record/maven-metadata.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - org.apache.maven.repository.record - - - test - test-plugin - - - \ No newline at end of file -- 2.39.5