Set<Artifact> getResolvedArtifactsFromUnresolvedDependencies(
      List<Dependency> unresolvedDependencies, boolean resolveTransitively)
      throws MojoExecutionException {
    final Set<Artifact> resolvedArtifacts = new HashSet<Artifact>();
    //    Artifact mojoArtifact = this.artifactFactory.createBuildArtifact(project.getGroupId(),
    // project.getArtifactId(), project.getVersion(), "pom");

    /*
     * Resolve each artifact.  This will get all transitive artifacts AND eliminate conflicts.
     */
    final Set<Artifact> unresolvedArtifacts;

    //    for (Dependency d : unresolvedDependencies)
    //      System.out.println("dependency: " + d.toString());

    try {
      unresolvedArtifacts =
          MavenMetadataSource.createArtifacts(
              this.artifactFactory, unresolvedDependencies, null, null, null);
      //      for (Artifact artifact : unresolvedArtifacts) {
      //        System.out.println("unresolved " + artifact.toString());
      //      }

      if (resolveTransitively) {
        ArtifactResolutionResult artifacts =
            artifactResolver.resolveTransitively(
                unresolvedArtifacts,
                project.getArtifact(),
                remoteRepositories,
                localRepository,
                artifactMetadataSource);
        resolvedArtifacts.addAll(artifacts.getArtifacts());
      } else {
        // resolve each artifact individually
        for (Artifact artifact : unresolvedArtifacts) {
          artifactResolver.resolve(artifact, remoteRepositories, localRepository);

          resolvedArtifacts.add(artifact);
        }
      }

    } catch (Exception e) {
      throw new MojoExecutionException("Unable to complete configuring the build settings", e);
    }

    for (Artifact artifact : resolvedArtifacts) {
      System.out.println("matched " + artifact.toString());
    }

    return resolvedArtifacts;
  }
Пример #2
0
 private Path extractIfRequired(final Path buildDir)
     throws MojoFailureException, MojoExecutionException {
   if (jbossHome != null) {
     // we do not need to download WildFly
     return Paths.get(jbossHome);
   }
   final String artifact =
       ArtifactNameSplitter.of(this.artifact)
           .setArtifactId(artifactId)
           .setClassifier(classifier)
           .setGroupId(groupId)
           .setPackaging(packaging)
           .setVersion(version)
           .asString();
   final Path result = artifactResolver.resolve(project, artifact).toPath();
   final Path target = buildDir.resolve(WILDFLY_DIR);
   // Delete the target if it exists
   if (Files.exists(target)) {
     try {
       Archives.deleteDirectory(target);
     } catch (IOException e) {
       throw new MojoFailureException("Could not delete target directory: " + target, e);
     }
   }
   try {
     Archives.unzip(result, target);
     final Iterator<Path> iterator = Files.newDirectoryStream(target).iterator();
     if (iterator.hasNext()) return iterator.next();
   } catch (IOException e) {
     throw new MojoFailureException("Artifact was not successfully extracted: " + result, e);
   }
   throw new MojoFailureException("Artifact was not successfully extracted: " + result);
 }
  @Override
  public ClassLoader getClassLoader(KieModule kmodule) {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    if (parent == null) {
      parent = ClassLoader.getSystemClassLoader();
    }
    if (parent == null) {
      parent = MavenClassLoaderResolver.class.getClassLoader();
    }

    InternalKieModule internalKModule = (InternalKieModule) kmodule;
    Collection<ReleaseId> jarDependencies = internalKModule.getJarDependencies();

    if (jarDependencies.isEmpty()) {
      return parent;
    }

    ArtifactResolver resolver = ArtifactResolver.getResolverFor(internalKModule.getPomModel());
    List<URL> urls = new ArrayList<URL>();
    List<ReleaseId> unresolvedDeps = new ArrayList<ReleaseId>();

    for (ReleaseId rid : jarDependencies) {
      try {
        Artifact artifact = resolver.resolveArtifact(rid);
        if (artifact != null) {
          File jar = artifact.getFile();
          urls.add(jar.toURI().toURL());
        } else {
          logger.error("Dependency artifact not found for: " + rid);
          unresolvedDeps.add(rid);
        }
      } catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }
    }

    internalKModule.setUnresolvedDependencies(unresolvedDeps);
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
  }