如何以编程方式列出所有传递依赖项,包括使用 DependencyGraphBuilder 在 Maven 中覆盖的依赖项?答案 您所在的位置:网站首页 解析maven依赖项很慢 如何以编程方式列出所有传递依赖项,包括使用 DependencyGraphBuilder 在 Maven 中覆盖的依赖项?答案

如何以编程方式列出所有传递依赖项,包括使用 DependencyGraphBuilder 在 Maven 中覆盖的依赖项?答案

#如何以编程方式列出所有传递依赖项,包括使用 DependencyGraphBuilder 在 Maven 中覆盖的依赖项?答案| 来源: 网络整理| 查看: 265

在这里包括我的方法,因为附加步骤可能会成为您实际用例的一部分,尤其是。如果从事复合或多模块项目。

(Maven 3,我的运行时是 3.6;不直接依赖 Aether)

在我的情况下,我想从我的插件中解析特定工件 foo-runtime 的依赖关系树;然而,

某些依赖版本仅在其父级的 foo-parent POM 中可用(即在 foo-runtime 自己的 POM 中不存在)。 父 POM 还包含其他详细信息,例如排除 foo-runtime 的某些依赖项 - 通过 dependencyManagement。

所以我不得不:

显式加载父模型, 将子模型链接到它, 填写子模型缺失的版本号(还是不知道为什么Maven在链接父模型后没有自动解决这些),然后 为孩子运行依赖关系解析。

为了避免从头开始构建模型,我使用现有工件 foo-api 导出了 foo-runtime 的模型(在我的情况下,它始终保证存在于正在构建的 Maven 项目中)。所有这些工件都共享相同的groupId。

@Component public LifecycleDependencyResolver resolver; // ... // `artifacts` contains all artifacts of current/reactor `MavenProject` obtained via `project.getArtifacts()` private Set resolveRuntimeDeps(Set artifacts) throws MojoExecutionException { // foo-api will always be present; use it to derive coordinates for foo-runtime Artifact fooApi = artifacts.stream().filter(artifact -> "foo-api".equals(artifact.getArtifactId())) .findFirst().orElseThrow(() -> new MojoExecutionException("Unable to find foo-api")); Collection scopes = Arrays.asList("compile", "runtime"); MavenProject fooRoot = deriveProject(fooApi, "foo-parent"); Model fooRootPom = fooRoot.getModel(); MavenProject fooSrv = deriveProject(fooApi, "foo-runtime"); fooSrv.setParent(fooRoot); // some foo-runtime deps depend on versions declared on parent pom; merge them Map depMgt = fooRootPom.getDependencyManagement().getDependencies().stream() .collect(Collectors.toMap(dep -> dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getType(), this::toArtifact)); for (Dependency d : fooSrv.getDependencies()) { if (d.getVersion() == null) { Artifact managed = depMgt.get(d.getGroupId() + ":" + d.getArtifactId() + ":" + d.getType()); if (managed != null) { d.setVersion(managed.getVersion()); } } } try { resolver.resolveProjectDependencies(fooSrv, scopes, scopes, session, false, Collections.emptySet()); return fooSrv.getArtifacts(); } catch (LifecycleExecutionException e) { throw new MojoExecutionException("Error resolving foo-runtime dependencies", e); } } // load POM for another artifact based on foo-api JAR available in current project private MavenProject deriveProject(Artifact fooApi, String artifactId) throws MojoExecutionException { Model pom; String pomPath = fooApi.getFile().getAbsolutePath().replaceAll("foo-api", artifactId).replaceAll("\\.jar$", ".pom"); try (InputStream fooRootPomData = new FileInputStream(pomPath)) { pom = new MavenXpp3Reader().read(fooRootPomData); pom.setPomFile(new File(pomPath)); } catch (IOException | XmlPullParserException e) { throw new MojoExecutionException("Error loading " + artifactId + " metadata", e); } // set these params to avoid skips/errors during resolution MavenProject proj = new MavenProject(pom); proj.setArtifact(toArtifact(pom)); proj.setArtifactFilter(Objects::nonNull); proj.setRemoteArtifactRepositories(Collections.emptyList()); return proj; } private Artifact toArtifact(Model model) { return new DefaultArtifact( Optional.ofNullable(model.getGroupId()).orElseGet(() -> model.getParent().getGroupId()), model.getArtifactId(), Optional.ofNullable(model.getVersion()).orElseGet(() -> model.getParent().getVersion()), "compile", model.getPackaging(), null, project.getArtifact().getArtifactHandler()); } private Artifact toArtifact(Dependency dep) { return new DefaultArtifact(dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getScope(), dep.getType(), dep.getClassifier(), project.getArtifact().getArtifactHandler()); }

(我尝试了几乎所有其他建议的方法,但是所有这些方法最终都出现了一些错误。现在回想起来,我怀疑其中许多错误可能是由于我的叶子 POM 缺少版本一些工件的数字。似乎(可以接受)“模型丰富”阶段 - 传播父版本等 - 是由 Maven 流程中的一些早期组件执行的;并且调用者必须至少部分处理这个问题,当从头开始调用依赖解析器。)



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有