예제 #1
0
  @Override
  public DependencyMetadata resolveDependencyMetadata(
      Dependency query, final List<DependencyRepository> repositories) {
    try {
      if (Strings.isNullOrEmpty(query.getVersion())) {
        query = DependencyBuilder.create(query).setVersion("[,)");
      }

      RepositorySystem system = container.lookup(RepositorySystem.class);
      MavenRepositorySystemSession session = setupRepoSession(system);

      DefaultArtifact artifact = new DefaultArtifact(query.toCoordinates());

      ArtifactDescriptorRequest ar =
          new ArtifactDescriptorRequest(artifact, convertToMavenRepos(repositories), null);
      ArtifactDescriptorResult results = system.readArtifactDescriptor(session, ar);

      Artifact a = results.getArtifact();
      Dependency d =
          DependencyBuilder.create()
              .setArtifactId(a.getArtifactId())
              .setGroupId(a.getGroupId())
              .setVersion(a.getVersion());

      return new DependencyMetadataImpl(d, results);
    } catch (Exception e) {
      throw new ProjectModelException(
          "Unable to resolve any artifacts for query [" + query + "]", e);
    }
  }
예제 #2
0
  private VersionRangeResult getVersions(
      Dependency dep, final List<RemoteRepository> repositories) {
    try {
      String version = dep.getVersion();
      if (Strings.isNullOrEmpty(version)) {
        dep = DependencyBuilder.create(dep).setVersion("[,)");
      } else if (!version.matches("(\\(|\\[).*?(\\)|\\])")) {
        dep = DependencyBuilder.create(dep).setVersion("[" + version + "]");
      }

      RepositorySystem maven = container.lookup(RepositorySystem.class);
      MavenRepositorySystemSession session = setupRepoSession(maven);

      VersionRangeRequest rangeRequest =
          new VersionRangeRequest(new DefaultArtifact(dep.toCoordinates()), repositories, null);

      VersionRangeResult rangeResult = maven.resolveVersionRange(session, rangeRequest);
      return rangeResult;
    } catch (Exception e) {
      throw new ProjectModelException("Failed to look up versions for [" + dep + "]", e);
    }
  }
예제 #3
0
  @Override
  public List<DependencyResource> resolveDependencies(
      Dependency dep, final List<DependencyRepository> repositories) {
    List<DependencyResource> result = new ArrayList<DependencyResource>();

    try {
      if (Strings.isNullOrEmpty(dep.getVersion())) {
        dep = DependencyBuilder.create(dep).setVersion("[,)");
      }

      RepositorySystem system = container.lookup(RepositorySystem.class);
      MavenRepositorySystemSession session = setupRepoSession(system);

      DefaultArtifact artifact = new DefaultArtifact(dep.toCoordinates());
      CollectRequest collectRequest =
          new CollectRequest(
              new org.sonatype.aether.graph.Dependency(artifact, null),
              convertToMavenRepos(repositories));
      DependencyRequest request = new DependencyRequest(collectRequest, null);

      DependencyResult artifacts = system.resolveDependencies(session, request);

      for (ArtifactResult a : artifacts.getArtifactResults()) {
        File file = a.getArtifact().getFile();
        Dependency d =
            DependencyBuilder.create()
                .setArtifactId(a.getArtifact().getArtifactId())
                .setGroupId(a.getArtifact().getGroupId())
                .setVersion(a.getArtifact().getVersion());
        DependencyResource resource = new DependencyResource(factory, file, d);
        result.add(resource);
      }
      return result;
    } catch (Exception e) {
      throw new ProjectModelException("Unable to resolve an artifact", e);
    }
  }
예제 #4
0
  @Command("set-parent")
  public void setParent(
      @Option(
              name = "parentId",
              description =
                  "dependency identifier of parent, ex: \"org.jboss.seam.forge:forge-parent:1.0.0\"",
              required = false)
          final Dependency gav,
      @Option(
              name = "parentRelativePath",
              description =
                  "relative location from the current project to the parent project root folder",
              type = PromptType.FILE_PATH,
              required = false)
          final String relativePath,
      @Option(
              name = "parentProjectRoot",
              description = "absolute location of a project to use as this project's direct parent",
              required = false)
          final Resource<?> path,
      final PipeOut out) {
    MavenCoreFacet mvn = project.getFacet(MavenCoreFacet.class);
    Parent parent = null;
    if (gav != null) {
      Assert.notNull(
          gav.getArtifactId(), "ArtifactId must not be null [" + gav.toCoordinates() + "]");
      Assert.notNull(gav.getGroupId(), "GroupId must not be null [" + gav.toCoordinates() + "]");
      Assert.notNull(gav.getVersion(), "Version must not be null [" + gav.toCoordinates() + "]");

      parent = new Parent();
      parent.setArtifactId(gav.getArtifactId());
      parent.setGroupId(gav.getGroupId());
      parent.setVersion(gav.getVersion());

      if (relativePath != null) {
        parent.setRelativePath(relativePath);
      }

      Model pom = mvn.getPOM();
      pom.setParent(parent);
      mvn.setPOM(pom);
    } else if ((path != null) && factory.containsProject(path.reify(DirectoryResource.class))) {
      Project parentProject = factory.findProject(path.reify(DirectoryResource.class));
      MavenCoreFacet parentCore = parentProject.getFacet(MavenCoreFacet.class);

      parent = new Parent();
      parent.setArtifactId(parentCore.getMavenProject().getArtifactId());
      parent.setGroupId(parentCore.getMavenProject().getGroupId());
      parent.setVersion(parentCore.getMavenProject().getVersion());

      if (relativePath != null) {
        parent.setRelativePath(relativePath);
      }

      Model pom = mvn.getPOM();
      pom.setParent(parent);
      mvn.setPOM(pom);
    } else if (relativePath != null) {
      PathspecParser parser =
          new PathspecParser(resources, shell.getCurrentProject().getProjectRoot(), relativePath);
      List<Resource<?>> resolvedResources = parser.resolve();
      if (!resolvedResources.isEmpty()
          && factory.containsProject(resolvedResources.get(0).reify(DirectoryResource.class))) {
        Project parentProject =
            factory.findProject(resolvedResources.get(0).reify(DirectoryResource.class));
        MavenCoreFacet parentCore = parentProject.getFacet(MavenCoreFacet.class);

        parent = new Parent();
        parent.setArtifactId(parentCore.getMavenProject().getArtifactId());
        parent.setGroupId(parentCore.getMavenProject().getGroupId());
        parent.setVersion(parentCore.getMavenProject().getVersion());
        parent.setRelativePath(relativePath);

        Model pom = mvn.getPOM();
        pom.setParent(parent);
        mvn.setPOM(pom);
      } else {
        out.print(ShellColor.RED, "***ERROR***");
        out.println(" relative path did not resolve to a Project [" + relativePath + "]");
      }
    } else {
      out.print(ShellColor.RED, "***ERROR***");
      out.println(" you must specify a path to or dependency id of the parent project.");
    }

    if (parent != null) {
      String parentId =
          parent.getGroupId()
              + ":"
              + parent.getArtifactId()
              + ":"
              + parent.getVersion()
              + " ("
              + (parent.getRelativePath() == null ? " " : parent.getRelativePath() + ")");

      out.println("Set parent [ " + parentId + " ]");
    }
  }