Maven Plugin API:从Artifact获取MavenProject [英] Maven Plugin API: Get MavenProject from Artifact

查看:456
本文介绍了Maven Plugin API:从Artifact获取MavenProject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取有关项目中使用的所有依赖项(递归)的信息。看起来MavenProject类提供了我需要的所有信息。但我无法弄清楚如何将 Artifact 的实例转换为 MavenProject的实例

I am trying to extract information on all dependencies (recursively) used in my project. It seems like the MavenProject class provides all information that I require. But I can't figure out how to transform an instance of Artifact into an instance of MavenProject

/**
 * 
 *
 * @reqiresDependencyResolution
 *
 */
@Mojo(name = "license-overview", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
public class MyMojo extends AbstractMojo {


    /**
     * @parameter default-value="${project}"
     * @required
     * @readonly
     */
    MavenProject project;


    public void execute() throws MojoExecutionException {

        Set<Artifact> artifacts= project.getArtifacts();
        for (Artifact artifact : artifacts) {
            //Here I need to access the artifact's name, license, author, etc.
            System.out.println("*** "+artifact.getArtifactId()+"***");
        }

    }
}

如何访问位于我的依赖关系的pom内的信息,但不是通过 Artifact s getters导出的?

How to access the information that is located within the pom of my dependency, but is not exported via the Artifacts getters?

推荐答案

是的,这是可能的。

我们可以使用 ProjectBuilder API:

We can build a project in-memory with the ProjectBuilder API:


构建项目的内存描述。

Builds in-memory descriptions of projects.

通过调用 build(projectArtifact,request) 方法,我们感兴趣的工件和 ProjectBuildingRequest (它包含各种参数,如远程/本地存储库的位置等),这将在内存中构建一个 MavenProject

考虑以下MOJO,它将打印所有依赖项的名称:

Consider the following MOJO that will print the name of all the dependencies:

@Mojo(name = "foo", requiresDependencyResolution = ResolutionScope.RUNTIME)
public class MyMojo extends AbstractMojo {

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

    @Parameter(defaultValue = "${session}", readonly = true, required = true)
    private MavenSession session;

    @Component
    private ProjectBuilder projectBuilder;

    public void execute() throws MojoExecutionException, MojoFailureException {
        ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
        try {
            for (Artifact artifact : project.getArtifacts()) {
                buildingRequest.setProject(null);
                MavenProject mavenProject = projectBuilder.build(artifact, buildingRequest).getProject();
                System.out.println(mavenProject.getName());
            }
        } catch (ProjectBuildingException e) {
            throw new MojoExecutionException("Error while building project", e);
        }
    }

}

有这里有几个主要成分:

There are a couple of main ingredients here:


  • requiresDependencyResolution 告诉Maven我们要求在执行之前解析依赖项。在这种情况下,我将其指定为 RUNTIME ,以便解决所有编译和运行时依赖关系。您当然可以将其设置为 ResolutionScope 。你想要的。

  • 项目构建器注入了 @Component 注释。

  • A使用当前Maven会话的参数构建默认构建请求。我们只需要通过显式设置为 null 来覆盖当前项目,否则不会发生任何事情。

  • The requiresDependencyResolution tells Maven that we require the dependencies to be resolved before execution. In this case, I specified it to RUNTIME so that all compile and runtime dependencies are resolved. You can of course set that to the ResolutionScope. you want.
  • The project builder is injected with the @Component annotation.
  • A default building request is build with the parameter of the current Maven session. We just need to override the current project by explicitly setting it to null, otherwise nothing will happen.

当您有权访问 MavenProject 时,您可以打印所有关于它的信息,比如开发人员等。

When you have access to the MavenProject, you can then print all the information you want about it, like developers, etc.

如果要打印依赖项(直接和传递),还需要调用 setResolveDependencies(true) 在建筑物请求中,否则,它们将不会填充在构建的项目中。

If you want to print the dependencies (direct and transitive), you will also need to invoke setResolveDependencies(true) on the building request, otherwise, they won't be populated in the built project.

这篇关于Maven Plugin API:从Artifact获取MavenProject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆