【问题标题】:Extracting licenses of dependencies from maven plugin从 maven 插件中提取依赖项的许可证
【发布时间】:2021-06-07 09:37:24
【问题描述】:

我正在编写一个打印所有项目依赖项的许可证的 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 本身相同的存储库。

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

【问题讨论】:

  • 不幸的是,license-maven-plugin 没有提供所有依赖项及其许可证的机器可读列表。解析生成的报告对我来说似乎太不稳定了。
  • license:download-licenses 创建一个包含依赖项及其许可证的 XML 文件。你还想要什么?
  • 你是对的。也许它甚至比用 Java 做的更好。

标签: java maven maven-plugin


【解决方案1】:

我刚刚找到了解决办法:

@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

【讨论】:

    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 2018-02-27
    相关资源
    最近更新 更多