java 如何标识生成插件中依赖项的依赖项文件位置

ss2ws0br  于 2022-12-28  发布在  Java
关注(0)|答案(1)|浏览(99)

我正在尝试写一个自定义的maven插件。插件将做的一部分是检查的其他依赖关系的m2文件夹中的JAR文件列在它旁边的<plugin>标记。我需要确切的JAR文件/位置。
示例:

<build>
        <plugins>
            <plugin>
                <groupId>myPluginGroup</groupId>
                <artifactId>myPluginArtifact</artifactId>
                <version>1.0.0-SNAPSHOT</version>

                <dependencies>
                    <!-- find me -->
                    <dependency>
                        <groupId>example</groupId>
                        <artifactId>example</artifactId>
                        <version>1.0.0</version>
                    </dependency>
                </dependencies>
....
            </plugin>
        </plugins>
    </build>

因此,我希望能够扫描并找到"example.example",并将其指向"C:\用户\用户名. m2\存储库\示例\示例\1. 0. 0\示例-1. 0. 0. jar"。
我可以找到插件依赖关系:

final List<Plugin> plugins = project.getBuild().getPlugins();

for (Plugin plugin : plugins) {
    final List<Dependency> dependencyArtifacts = plugin.getDependencies();
    for (Dependency dependency : dependencyArtifacts) {
        ...
    }
}

这并没有给我JAR的位置,但它确实提供了构建插件依赖项的列表,这是我所需要的一部分。
我可以通过以下方式获得一些位置:

DependencyNode rootNode = treeBuilder.buildDependencyTree(project, localRepository,
    artifactFactory, artifactMetadataSource, artifactFilter, artifactCollector);

CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();

rootNode.accept(visitor);

List<DependencyNode> nodes = visitor.getNodes();
for (DependencyNode dependencyNode : nodes) {
    ...
}

但它不提供插件,即使它提供了什么,它不提供所有的m2位置。
有没有一种方法可以在构建插件中标识依赖项的依赖文件位置?

x0fgdtte

x0fgdtte1#

您可以使用Maven Resolver API来解析工件,在您的情况下,代码可能如下所示:
(无回路示例-仅解析器的最终用途)

public class MyMojo {

    @Component
    private RepositorySystem repoSystem;

    @Parameter( defaultValue = "${repositorySystemSession}", readonly = true )
    private RepositorySystemSession repoSession;

    @Parameter(defaultValue = "${project}", readonly = true)
    protected MavenProject project;

    void resolveOneArtifact() throws ArtifactResolutionException {
        Dependency dependency = project.getBuild().getPlugins().get(0).getDependencies().get(0);

        ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(RepositoryUtils.toDependency(dependency, repoSession.getArtifactTypeRegistry()).getArtifact());
        request.setRepositories(project.getRemotePluginRepositories());

        ArtifactResult artifactResult = repoSystem.resolveArtifact(repoSession, request);
        System.out.println(artifactResult.getArtifact().getFile());
    }

    void resolveArtifactWithTransitiveDependencies() throws DependencyResolutionException {
        Dependency dependency = project.getBuild().getPlugins().get(0).getDependencies().get(0);

        CollectRequest collectRequest = new CollectRequest();
        collectRequest.setRoot(RepositoryUtils.toDependency(dependency, repoSession.getArtifactTypeRegistry()));
        collectRequest.setRepositories(project.getRemotePluginRepositories());

        DependencyRequest request = new DependencyRequest();
        request.setCollectRequest(collectRequest);

        DependencyResult dependencyResult = repoSystem.resolveDependencies(repoSession, request);
        ArtifactResult artifactResult = dependencyResult.getArtifactResults().get(0);

        System.out.println(artifactResult.getArtifact().getFile());
    }
}

更多示例请查看代码:https://github.com/apache/maven-resolver/tree/master/maven-resolver-demos

相关问题