【问题标题】:How can I scan a maven repository?如何扫描 Maven 存储库?
【发布时间】:2011-07-10 17:09:12
【问题描述】:

我正在为 Eclipse 开发一个代码共享插件(用于一个学士论文项目)。

目前我正在尝试扫描一个 maven 存储库并生成一个包列表。

我可以使用 maven.model 类下载和解析 pom.xml,但我不知道哪些 maven 类负责解析archetype-catalog.xml

有非 maven 解析器吗?

我可以只扫描整个存储库树的 pom.xml 文件吗?

编辑: 我找到了 nexus-indexer,但我不知道如何使用它 :(

【问题讨论】:

    标签: java maven parsing maven-indexer


    【解决方案1】:

    花了很长时间,但我终于找到了一个可行的例子

    PlexusContainer plexus =  new DefaultPlexusContainer();
    
    NexusIndexer n = (NexusIndexer) plexus.lookup(NexusIndexer.class);
    IndexUpdater iu = (IndexUpdater) plexus.lookup(IndexUpdater.class);
    
    // DefaultNexusIndexer n = new DefaultNexusIndexer();
    List<IndexCreator> indexCreators = new ArrayList<IndexCreator>();
    // IndexingContext c = n.addIndexingContext("test", "test",new File( "/home/tomas/Desktop/test"),new File( "/home/tomas/Desktop/index"), "http://repository.jboss.org/", null);
    Directory tempIndexDirectory = new RAMDirectory();
    
    // IndexCreator min = new MinimalArtifactInfoIndexCreator();
    // MavenPluginArtifactInfoIndexCreator mavenPlugin = new MavenPluginArtifactInfoIndexCreator();
    // MavenArchetypeArtifactInfoIndexCreator mavenArchetype = new MavenArchetypeArtifactInfoIndexCreator();
    // JarFileContentsIndexCreator jar = new JarFileContentsIndexCreator();
    // IndexCreator min = plexus.lookup(IndexCreator.class, MinimalArtifactInfoIndexCreator.ID);
    IndexCreator mavenPlugin = plexus.lookup( IndexCreator.class, MavenPluginArtifactInfoIndexCreator.ID );
    IndexCreator mavenArchetype = plexus.lookup( IndexCreator.class, MavenArchetypeArtifactInfoIndexCreator.ID );
    IndexCreator jar = plexus.lookup( IndexCreator.class, JarFileContentsIndexCreator.ID );
    indexCreators.add(min);
    indexCreators.add(mavenPlugin);
    indexCreators.add(mavenArchetype);
    indexCreators.add(jar);
    
    IndexingContext c = n.addIndexingContext(
        "temp", "test", new File("/home/tomas/Desktop/mavenTest"), 
        tempIndexDirectory, "http://repository.jboss.org/maven2/",
        null, indexCreators
    );
    
    IndexUpdateRequest ur=new IndexUpdateRequest(c);
    ur.setForceFullUpdate(true);
    iu.fetchAndUpdateIndex(ur);
    
    // for (String s : c.getAllGroups()) {
    //     System.out.println(s);
    // }
    
    BooleanQuery q = new BooleanQuery();
    q.add(n.constructQuery(ArtifactInfo.GROUP_ID, "*"), Occur.SHOULD);
    
    FlatSearchRequest request = new FlatSearchRequest(q);
    FlatSearchResponse response = n.searchFlat(request);
    
    for (ArtifactInfo a : response.getResults()) {
        String bUrl=url+a`enter code here`.groupId+"/"+a.artifactId+"/"+a.version+"/";
        String fileName=a.artifactId+"-"+a.version;
        System.out.println(bUrl+fileName+"."+a.packaging);
    }
    

    【讨论】:

      【解决方案2】:

      为了将来参考,nexus-indexer 一直是donated to the Apache Foundation,现在称为maven-indexer。有一个github mirror,该项目也列在Apache Git page

      【讨论】:

        【解决方案3】:

        扫描我的本地存储库以查找软件包将非常巧妙。在共享任何内容之前,我需要考虑大量重复的包。从昨天开始,我正在为我的存储库构建版本 1.1.1-SNAPSHOT,而今天我正在构建版本 1.1.2-SNAPSHOT,两个版本的目录将共享相同的包。

        现在,如果您想显示项目中使用了哪些工件,您可以使用依赖插件。在 maven 项目开始时我最喜欢的两个命令是:

        mvn dependency:tree
        

        mvn dependency:resolve
        

        【讨论】:

        • 感谢您的回复,但我需要扫描远程存储库并将结果保存到数据库,以便稍后进行搜索
        • 最终结果是远程存储库的类层次结构?
        • 最终结果应该是所有人工制品的列表。
        猜你喜欢
        • 1970-01-01
        • 2019-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多