Exemplo n.º 1
0
 /**
  * Resolves an artifact given its Maven coordinates.
  *
  * @param coordinates Maven artifact coordinates
  * @return resolved artifact file
  */
 public File resolveArtifact(final String coordinates) throws RuntimeException {
   try {
     Artifact artifact = artifactResolver.resolveArtifact(request().artifact(coordinates));
     if (artifact == null || artifact.getFile() == null || !artifact.getFile().exists()) {
       throw new RuntimeException(String.format("Artifact %s could not be resolved", coordinates));
     }
     return artifact.getFile();
   } catch (ArtifactResolutionException e) {
     throw new RuntimeException(e.getMessage(), e);
   }
 }
Exemplo n.º 2
0
 private void indexAtifacts() {
   Collection<DependencyDescriptor> deps = getArtifactResolver().getAllDependecies();
   for (DependencyDescriptor dep : deps) {
     if (!dep.isFixedVersion()) {
       Artifact artifact = getArtifactResolver().resolveArtifact(dep.getReleaseId());
       log.debug(artifact + " resolved to  " + artifact.getFile());
       if (isKJar(artifact.getFile())) {
         usedDependencies.put(dep.getReleaseId(), new DependencyDescriptor(artifact));
       }
     }
   }
 }
Exemplo n.º 3
0
 private void addDependencies(
     InternalKieModule kieModule,
     ArtifactResolver resolver,
     List<DependencyDescriptor> dependencies) {
   for (DependencyDescriptor dep : dependencies) {
     Artifact depArtifact = resolver.resolveArtifact(dep.getReleaseId());
     if (depArtifact != null && isKJar(depArtifact.getFile())) {
       ReleaseId depReleaseId = new DependencyDescriptor(depArtifact).getReleaseId();
       ZipKieModule zipKieModule = createZipKieModule(depReleaseId, depArtifact.getFile());
       if (zipKieModule != null) {
         kieModule.addKieDependency(zipKieModule);
       }
     }
   }
 }
 public boolean isMarkerOlder(Artifact artifact1) throws MojoExecutionException {
   File marker = getMarkerFile();
   if (marker.exists()) {
     return artifact1.getFile().lastModified() > marker.lastModified();
   } else {
     // if the marker doesn't exist, we want to copy so assume it is
     // infinately older
     return true;
   }
 }
  private Set<Artifact> processTransientDependencies(Dependency dependency, boolean sharedLibraries)
      throws MojoExecutionException {
    try {
      final Set<Artifact> artifacts = new LinkedHashSet<Artifact>();

      final CollectRequest collectRequest = new CollectRequest();

      collectRequest.setRoot(dependency);
      collectRequest.setRepositories(projectRepos);
      final DependencyNode node =
          repoSystem.collectDependencies(repoSession, collectRequest).getRoot();

      final DependencyRequest dependencyRequest =
          new DependencyRequest(
              node,
              new AndDependencyFilter(
                  new ScopeDependencyFilter(
                      Arrays.asList("compile", "runtime"), Arrays.asList("test")),
                  // Also exclude any optional dependencies
                  new DependencyFilter() {
                    @Override
                    public boolean accept(
                        DependencyNode dependencyNode, List<DependencyNode> dependencyNodes) {
                      return !dependencyNode.getDependency().isOptional();
                    }
                  }));

      repoSystem.resolveDependencies(repoSession, dependencyRequest);

      PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
      node.accept(nlg);

      final List<Dependency> dependencies = nlg.getDependencies(false);

      for (Dependency dep : dependencies) {
        final org.sonatype.aether.artifact.Artifact depAetherArtifact = dep.getArtifact();
        if (isNativeLibrary(sharedLibraries, depAetherArtifact.getExtension())) {
          final Artifact mavenArtifact =
              artifactFactory.createDependencyArtifact(
                  depAetherArtifact.getGroupId(),
                  depAetherArtifact.getArtifactId(),
                  VersionRange.createFromVersion(depAetherArtifact.getVersion()),
                  depAetherArtifact.getExtension(),
                  depAetherArtifact.getClassifier(),
                  dep.getScope());
          mavenArtifact.setFile(depAetherArtifact.getFile());
          artifacts.add(mavenArtifact);
        }
      }

      return artifacts;
    } catch (Exception e) {
      throw new MojoExecutionException("Error while processing transient dependencies", e);
    }
  }
Exemplo n.º 6
0
 /**
  * Fetch all files found (JAR, ZIP, directories, etc).
  *
  * @return Set of files
  * @throws DependencyResolutionException If can't resolve
  */
 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 private Set<File> fetch() throws DependencyResolutionException {
   final Set<File> files = new LinkedHashSet<File>(0);
   for (String path : this.elements()) {
     files.add(new File(path));
   }
   for (Artifact artifact : this.artifacts()) {
     files.add(artifact.getFile());
   }
   return files;
 }
Exemplo n.º 7
0
 private void updateKieModule(DependencyDescriptor oldDependency, Artifact artifact) {
   ReleaseId newReleaseId = new DependencyDescriptor(artifact).getReleaseId();
   ZipKieModule kieModule = createZipKieModule(newReleaseId, artifact.getFile());
   if (kieModule != null) {
     ResultsImpl messages = build(kieModule);
     if (messages.filterMessages(Message.Level.ERROR).isEmpty()) {
       ((InternalKieContainer) kieContainer)
           .updateDependencyToVersion(oldDependency.getArtifactReleaseId(), newReleaseId);
       oldDependency.setArtifactVersion(artifact.getVersion());
     }
   }
 }
Exemplo n.º 8
0
 private InternalKieModule buildArtifact(Artifact artifact, ArtifactResolver resolver) {
   DependencyDescriptor dependencyDescriptor = new DependencyDescriptor(artifact);
   ReleaseId releaseId = dependencyDescriptor.getReleaseId();
   if (releaseId.isSnapshot()) {
     ((ReleaseIdImpl) releaseId).setSnapshotVersion(artifact.getVersion());
   }
   ZipKieModule kieModule = createZipKieModule(releaseId, artifact.getFile());
   if (kieModule != null) {
     addDependencies(
         kieModule, resolver, resolver.getArtifactDependecies(dependencyDescriptor.toString()));
     build(kieModule);
   }
   return kieModule;
 }
  /**
   * Resolve maven URI into maven project.
   *
   * <p>Provides only model and artifact.
   */
  public static MavenProject newProject(String mavenURI) throws Exception {

    final Artifact artifact = newArtifact(mavenURI);

    final File input = artifact.getFile();

    final ModelReader reader = new DefaultModelReader();

    final Model model = reader.read(input, null);

    final MavenProject project = new MavenProject(model);

    project.setArtifact(RepositoryUtils.toArtifact(artifact));

    return project;
  }
  /**
   * Creates a new class realm with the specified parent and imports.
   *
   * @param baseRealmId The base id to use for the new realm, must not be {@code null}.
   * @param type The type of the class realm, must not be {@code null}.
   * @param parent The parent realm for the new realm, may be {@code null} to use the Maven core
   *     realm.
   * @param imports The packages/types to import from the parent realm, may be {@code null}.
   * @param artifacts The artifacts to add to the realm, may be {@code null}. Unresolved artifacts
   *     (i.e. with a missing file) will automatically be excluded from the realm.
   * @return The created class realm, never {@code null}.
   */
  private ClassRealm createRealm(
      String baseRealmId,
      RealmType type,
      ClassLoader parent,
      List<String> imports,
      boolean importXpp3Dom,
      List<Artifact> artifacts) {
    Set<String> artifactIds = new LinkedHashSet<String>();

    List<ClassRealmConstituent> constituents = new ArrayList<ClassRealmConstituent>();

    if (artifacts != null) {
      for (Artifact artifact : artifacts) {
        artifactIds.add(getId(artifact));
        if (artifact.getFile() != null) {
          constituents.add(new ArtifactClassRealmConstituent(artifact));
        }
      }
    }

    if (imports != null) {
      imports = new ArrayList<String>(imports);
    } else {
      imports = new ArrayList<String>();
    }

    ClassRealm classRealm = newRealm(baseRealmId);

    if (parent != null) {
      classRealm.setParentClassLoader(parent);
    } else {
      classRealm.setParentRealm(getMavenRealm());
    }

    List<ClassRealmManagerDelegate> delegates = getDelegates();
    if (!delegates.isEmpty()) {
      ClassRealmRequest request = new DefaultClassRealmRequest(type, parent, imports, constituents);

      for (ClassRealmManagerDelegate delegate : delegates) {
        delegate.setupRealm(classRealm, request);
      }
    }

    if (importXpp3Dom) {
      importXpp3Dom(classRealm);
    }

    if (!imports.isEmpty()) {
      ClassLoader importedRealm = classRealm.getParentClassLoader();

      if (logger.isDebugEnabled()) {
        logger.debug("Importing packages into class realm " + classRealm.getId());
      }

      for (String imp : imports) {
        if (logger.isDebugEnabled()) {
          logger.debug("  Imported: " + imp);
        }

        classRealm.importFrom(importedRealm, imp);
      }
    }

    Set<String> includedIds = populateRealm(classRealm, constituents);

    if (logger.isDebugEnabled()) {
      artifactIds.removeAll(includedIds);

      for (String id : artifactIds) {
        logger.debug("  Excluded: " + id);
      }
    }

    return classRealm;
  }
 public File getLocation() {
   return artifact.getFile();
 }