lucene 如何解析/解压缩/解压缩Nexus生成的Maven存储库索引

ncecgwcz  于 2022-11-07  发布在  Lucene
关注(0)|答案(4)|浏览(233)

我已经从http://mirrors.ibiblio.org/pub/mirrors/maven2/dot-index/nexus-maven-repository-index.gz下载了为Maven Central生成的索引
我想列出这些索引文件中的工件信息(例如groupId、artifactId、版本)。我读到有一个高级API。看来我必须使用下面的maven依赖项。但是,我不知道要使用什么入口点(哪个类?)以及如何使用它来访问这些文件:

<dependency>
    <groupId>org.sonatype.nexus</groupId>
    <artifactId>nexus-indexer</artifactId>
    <version>3.0.4</version>
</dependency>
pvcm50d1

pvcm50d11#

看一看https://github.com/cstamas/maven-indexer-examples项目。
简而言之:你不需要手动下载GZ/ZIP(新/旧格式),它会为你做索引器(此外,如果可能的话,它也会为你处理增量更新)。
GZ是“新”格式,独立于Lucene索引格式(因此,独立于Lucene版本),只包含数据,而ZIP是“旧”格式,实际上是普通的Lucene 2.4.x索引压缩。目前没有数据内容变化,但计划在未来进行。
正如我所说的,两者之间没有数据内容差异,但有些字段(如您所注意到的)是索引的,但不是存储在索引上,因此,如果您使用ZIP格式,您将使它们可搜索,但不可检索。

jfgube3f

jfgube3f2#

https://github.com/cstamas/maven-indexer-examples已过时。并且构建失败(测试未通过)。
Nexus Indexer已经沿着发展,也包括了这些示例:https://github.com/apache/maven-indexer/tree/master/indexer-examples
这样就可以构建代码,并且代码可以正常工作。
如果你想自己卷,这里是一个简化的版本:
玛文:

<dependencies>
    <dependency>
        <groupId>org.apache.maven.indexer</groupId>
        <artifactId>indexer-core</artifactId>
        <version>6.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>

    <!-- For ResourceFetcher implementation, if used -->
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-http-lightweight</artifactId>
        <version>2.3</version>
        <scope>compile</scope>
    </dependency>

    <!-- Runtime: DI, but using Plexus Shim as we use Wagon -->
    <dependency>
        <groupId>org.eclipse.sisu</groupId>
        <artifactId>org.eclipse.sisu.plexus</artifactId>
        <version>0.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.sonatype.sisu</groupId>
        <artifactId>sisu-guice</artifactId>
        <version>3.2.4</version>
    </dependency>

java 语:

public IndexToGavMappingConverter(File dataDir, String id, String url)
    throws PlexusContainerException, ComponentLookupException, IOException
{
    this.dataDir = dataDir;

    // Create Plexus container, the Maven default IoC container.
    final DefaultContainerConfiguration config = new DefaultContainerConfiguration();
    config.setClassPathScanning( PlexusConstants.SCANNING_INDEX );
    this.plexusContainer = new DefaultPlexusContainer(config);

    // Lookup the indexer components from plexus.
    this.indexer = plexusContainer.lookup( Indexer.class );
    this.indexUpdater = plexusContainer.lookup( IndexUpdater.class );
    // Lookup wagon used to remotely fetch index.
    this.httpWagon = plexusContainer.lookup( Wagon.class, "http" );

    // Files where local cache is (if any) and Lucene Index should be located
    this.centralLocalCache = new File( this.dataDir, id + "-cache" );
    this.centralIndexDir = new File( this.dataDir,   id + "-index" );

    // Creators we want to use (search for fields it defines).
    // See https://maven.apache.org/maven-indexer/indexer-core/apidocs/index.html?constant-values.html
    List<IndexCreator> indexers = new ArrayList();
    // https://maven.apache.org/maven-indexer/apidocs/org/apache/maven/index/creator/MinimalArtifactInfoIndexCreator.html
    indexers.add( plexusContainer.lookup( IndexCreator.class, "min" ) );
    // https://maven.apache.org/maven-indexer/apidocs/org/apache/maven/index/creator/JarFileContentsIndexCreator.html
    //indexers.add( plexusContainer.lookup( IndexCreator.class, "jarContent" ) );
    // https://maven.apache.org/maven-indexer/apidocs/org/apache/maven/index/creator/MavenPluginArtifactInfoIndexCreator.html
    //indexers.add( plexusContainer.lookup( IndexCreator.class, "maven-plugin" ) );

    // Create context for central repository index.
    this.centralContext = this.indexer.createIndexingContext(
            id + "Context", id, this.centralLocalCache, this.centralIndexDir,
            url, null, true, true, indexers );
}

    final IndexSearcher searcher = this.centralContext.acquireIndexSearcher();
    try
    {
        final IndexReader ir = searcher.getIndexReader();
        Bits liveDocs = MultiFields.getLiveDocs(ir);
        for ( int i = 0; i < ir.maxDoc(); i++ )
        {
            if ( liveDocs == null || liveDocs.get( i ) )
            {
                final Document doc = ir.document( i );
                final ArtifactInfo ai = IndexUtils.constructArtifactInfo( doc, this.centralContext );

                if (ai == null)
                    continue;
                if (ai.getSha1() == null)
                    continue;
                if (ai.getSha1().length() != 40)
                    continue;
                if ("javadoc".equals(ai.getClassifier()))
                    continue;
                if ("sources".equals(ai.getClassifier()))
                    continue;

                out.append(StringUtils.lowerCase(ai.getSha1())).append(' ');
                out.append(ai.getGroupId()).append(":");
                out.append(ai.getArtifactId()).append(":");
                out.append(ai.getVersion()).append(":");
                out.append(StringUtils.defaultString(ai.getClassifier()));
                out.append('\n');
            }
        }
    }
    finally
    {
        this.centralContext.releaseIndexSearcher( searcher );
    }

我们在Windup project - JBoss migration tool中使用此功能。

qaxu7uf2

qaxu7uf23#

legacy zip index是一个简单的lucene索引,我可以用Luke打开它,并编写一些简单的lucene代码来转储感兴趣的头(在本例中为“u”)

import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;

public class Dumper {
    public static void main(String[] args) throws Exception {
        IndexSearcher searcher = new IndexSearcher("c:/PROJECTS/Test/index");
        for (int i = 0; i < searcher.maxDoc(); i++) {
            Document doc = searcher.doc(i);
            String metadata = doc.get("u");
            if (metadata != null) {
                System.out.println(metadata);
            }
        }
    }
}

范例输出...

org.ioke|ioke-lang-lib|P-0.4.0-p11|NA
org.jboss.weld.archetypes|jboss-javaee6-webapp|1.0.1.CR2|sources|jar
org.jboss.weld.archetypes|jboss-javaee6-webapp|1.0.1.CR2|NA
org.nutz|nutz|1.b.37|javadoc|jar
org.nutz|nutz|1.b.37|sources|jar
org.nutz|nutz|1.b.37|NA
org.openengsb.wrapped|com.google.gdata|1.41.5.w1|NA
org.openengsb.wrapped|openengsb-wrapped-parent|6|NA

可能有更好的方法来实现这一点虽然...

f4t66c6m

f4t66c6m4#

对于记录,现在有一个工具可以提取maven索引并将其导出为文本文件:Maven index exporter。它以Docker映像的形式提供,不需要任何代码。
它基本上是下载所有的.gz索引文件,使用maven-indexer cli提取索引,并使用clue将它们导出到一个文本文件。它已经在Maven Central上进行了测试,并可以在许多其他Maven repositories上工作。

相关问题