从 Maven 插件中提取依赖项的许可证

Extracting licenses of dependencies from maven plugin

我正在编写一个 maven 插件,打印所有项目依赖项的许可证。 为此,我写了一个方法,使用 maven 来获取每个依赖项的模型。

public Model readModel(final String artifactId, final String groupId, final String version)
        throws ProjectBuildingException {
    final var artifact = this.repositorySystem.createProjectArtifact(groupId, artifactId, version);
    final ProjectBuildingResult build = this.mavenProjectBuilder.build(artifact,
            this.session.getProjectBuildingRequest());
    return build.getProject().getModel();
}

稍后在我的代码中,我从模型中选择许可证。

repositorySystem 通过以下方式注入 mojo:

@Component
RepositorySystem repositorySystem;

该代码的问题在于,它仅适用于 Maven Central 上可用的依赖项。 对于其他依赖项,它失败了:

Error resolving project artifact: Failure to find com.exasol:exasol-jdbc:pom:7.0.4 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced for project com.exasol:exasol-jdbc:pom:7.0.4

有什么我遗漏的吗?我希望这个 repositorySystem 使用与在 pom.xml.

中配置的 maven 本身相同的存储库

这是解决问题的正确方法吗? (我对依赖注入也不满意,但没有它我找不到解决这个问题的方法)

我刚刚找到了解决方案:

@Override
    public Model readModel(final String artifactId, final String groupId, final String version)
            throws ProjectBuildingException {
    final Artifact artifactDescription = this.repositorySystem.createProjectArtifact(groupId, artifactId, version);
    final ProjectBuildingRequest projectBuildingRequest = this.session.getProjectBuildingRequest();
    projectBuildingRequest.setRemoteRepositories(this.mvnProject.getRemoteArtifactRepositories());
    final ProjectBuildingResult build = this.mavenProjectBuilder.build(artifactDescription, projectBuildingRequest);
    return build.getProject().getModel();
}

诀窍是删除 this.repositorySystem.createProjectArtifact,它最终证明是开销,并将项目的存储库添加到 ProjectBuildingResult