我正在编写一个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
通过:注入烟雾
@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
中。