Пример #1
0
  public List<Artifact> resolvePom(File pomFile) {
    if (pomFile == null) {
      throw new RuntimeException("pomFile is null");
    }

    MavenProject pom;
    try {
      PlexusContainer container = container();
      org.apache.maven.repository.RepositorySystem lrs =
          container.lookup(org.apache.maven.repository.RepositorySystem.class);
      ProjectBuilder projectBuilder = container.lookup(ProjectBuilder.class);
      ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
      request.setRepositorySession(repositorySystemSession);
      request.setProcessPlugins(false);
      request.setLocalRepository(lrs.createDefaultLocalRepository());
      request.setRemoteRepositories(
          Arrays.asList(new ArtifactRepository[] {lrs.createDefaultRemoteRepository()}.clone()));
      ProjectBuildingResult result = projectBuilder.build(pomFile, request);
      pom = result.getProject();
    } catch (Exception e) {
      if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
      }
      throw new RuntimeException("Error loading pom: " + pomFile.getAbsolutePath(), e);
    }

    Artifact rootArtifact =
        new DefaultArtifact(
            pom.getArtifact().getGroupId(),
            pom.getArtifact().getArtifactId(),
            pom.getArtifact().getClassifier(),
            pom.getArtifact().getType(),
            pom.getArtifact().getVersion(),
            null,
            new File(pom.getModel().getBuild().getOutputDirectory()));

    CollectRequest collectRequest = new CollectRequest();
    for (org.apache.maven.model.Dependency dependency : pom.getDependencies()) {
      collectRequest.addDependency(toAetherDependency(dependency));
    }
    for (RemoteRepository repository : pom.getRemoteProjectRepositories()) {
      collectRequest.addRepository(repository);
    }
    for (RemoteRepository repository : repositories) {
      collectRequest.addRepository(repository);
    }

    // Make sure we account for managed dependencies
    if (pom.getDependencyManagement() != null) {
      for (org.apache.maven.model.Dependency managedDependency :
          pom.getDependencyManagement().getDependencies()) {
        collectRequest.addManagedDependency(toAetherDependency(managedDependency));
      }
    }

    DependencyRequest dependencyRequest =
        new DependencyRequest(
            collectRequest, DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME));
    List<Artifact> artifacts = resolveArtifacts(dependencyRequest);
    return ImmutableList.<Artifact>builder().add(rootArtifact).addAll(artifacts).build();
  }