mirror of
https://github.com/apache/archiva.git
synced 2025-03-06 16:39:13 +00:00
[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
This commit is contained in:
parent
ab0ca9f0f0
commit
25eac2b1f8
@ -61,13 +61,21 @@ protected List readFilesInArchive( File file )
|
||||
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 @@ protected static boolean isClass( String name )
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,29 +18,30 @@
|
||||
|
||||
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 @@ record = new StandardArtifactIndexRecord();
|
||||
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 @@ record = new StandardArtifactIndexRecord();
|
||||
{
|
||||
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 @@ private Metadata readMetadata( File file )
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ private void populateArchiveEntries( List files, StandardArtifactIndexRecord rec
|
||||
{
|
||||
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 @@ private void populateArchiveEntries( List files, StandardArtifactIndexRecord rec
|
||||
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 ) ) );
|
||||
}
|
||||
|
||||
if ( record.getGroupId().equals( metadata.getGroupId() ) )
|
||||
catch ( ZipException e )
|
||||
{
|
||||
String prefix = (String) prefixes.get( record.getArtifactId() );
|
||||
if ( prefix != null )
|
||||
{
|
||||
record.setPluginPrefix( prefix );
|
||||
}
|
||||
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 ( prefix != null )
|
||||
{
|
||||
record.setPluginPrefix( prefix.getValue() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
<!--
|
||||
~ 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.
|
||||
-->
|
||||
|
||||
<metadata>
|
||||
<groupId>org.apache.maven.repository.record</groupId>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<prefix>test</prefix>
|
||||
<artifactId>test-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</metadata>
|
Loading…
x
Reference in New Issue
Block a user