Search in sources :

Example 1 with DependencyCollectionException

use of org.eclipse.aether.collection.DependencyCollectionException in project byte-buddy by raphw.

the class ClassLoaderResolverTest method testCollectionFailure.

@Test(expected = MojoExecutionException.class)
public void testCollectionFailure() throws Exception {
    when(repositorySystem.collectDependencies(eq(repositorySystemSession), any(CollectRequest.class))).thenThrow(new DependencyCollectionException(new CollectResult(new CollectRequest())));
    classLoaderResolver.resolve(new MavenCoordinate(FOO, BAR, QUX));
}
Also used : DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) CollectResult(org.eclipse.aether.collection.CollectResult) CollectRequest(org.eclipse.aether.collection.CollectRequest) Test(org.junit.Test)

Example 2 with DependencyCollectionException

use of org.eclipse.aether.collection.DependencyCollectionException in project BIMserver by opensourceBIM.

the class PluginManager method loadDependencies.

private PublicFindClassClassLoader loadDependencies(Set<org.bimserver.plugins.Dependency> bimServerDependencies, Model model, PublicFindClassClassLoader previous) throws FileNotFoundException, IOException {
    List<org.apache.maven.model.Dependency> dependencies = model.getDependencies();
    Iterator<org.apache.maven.model.Dependency> it = dependencies.iterator();
    Path workspaceDir = Paths.get("..");
    bimServerDependencies.add(new org.bimserver.plugins.Dependency(workspaceDir.resolve("PluginBase/target/classes")));
    bimServerDependencies.add(new org.bimserver.plugins.Dependency(workspaceDir.resolve("Shared/target/classes")));
    while (it.hasNext()) {
        org.apache.maven.model.Dependency depend = it.next();
        try {
            if (depend.getGroupId().equals("org.opensourcebim") && (depend.getArtifactId().equals("shared") || depend.getArtifactId().equals("pluginbase"))) {
                // TODO we might want to check the version though
                continue;
            }
            if (depend.isOptional() || "test".equals(depend.getScope())) {
                continue;
            }
            Dependency dependency2 = new Dependency(new DefaultArtifact(depend.getGroupId() + ":" + depend.getArtifactId() + ":jar:" + depend.getVersion()), "compile");
            DelegatingClassLoader depDelLoader = new DelegatingClassLoader(previous);
            if (!dependency2.getArtifact().isSnapshot()) {
                if (dependency2.getArtifact().getFile() != null) {
                    bimServerDependencies.add(new org.bimserver.plugins.Dependency(dependency2.getArtifact().getFile().toPath()));
                    loadDependencies(dependency2.getArtifact().getFile().toPath(), depDelLoader);
                } else {
                    ArtifactRequest request = new ArtifactRequest();
                    request.setArtifact(dependency2.getArtifact());
                    request.setRepositories(mavenPluginRepository.getRepositories());
                    try {
                        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
                        if (resolveArtifact.getArtifact().getFile() != null) {
                            bimServerDependencies.add(new org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                            loadDependencies(resolveArtifact.getArtifact().getFile().toPath(), depDelLoader);
                        } else {
                        // TODO error?
                        }
                    } catch (ArtifactResolutionException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                // Snapshot projects linked in Eclipse
                ArtifactRequest request = new ArtifactRequest();
                if ((!"test".equals(dependency2.getScope()) && !dependency2.getArtifact().isSnapshot())) {
                    request.setArtifact(dependency2.getArtifact());
                    request.setRepositories(mavenPluginRepository.getLocalRepositories());
                    try {
                        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
                        if (resolveArtifact.getArtifact().getFile() != null) {
                            bimServerDependencies.add(new org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                            loadDependencies(resolveArtifact.getArtifact().getFile().toPath(), depDelLoader);
                        } else {
                        // TODO error?
                        }
                    } catch (Exception e) {
                        LOGGER.info(dependency2.getArtifact().toString());
                        e.printStackTrace();
                    }
                // bimServerDependencies.add(new
                // org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                }
            }
            ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
            descriptorRequest.setArtifact(dependency2.getArtifact());
            descriptorRequest.setRepositories(mavenPluginRepository.getRepositories());
            ArtifactDescriptorResult descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
            CollectRequest collectRequest = new CollectRequest();
            collectRequest.setRootArtifact(descriptorResult.getArtifact());
            collectRequest.setDependencies(descriptorResult.getDependencies());
            collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
            collectRequest.setRepositories(descriptorResult.getRepositories());
            DependencyNode node = mavenPluginRepository.getSystem().collectDependencies(mavenPluginRepository.getSession(), collectRequest).getRoot();
            DependencyRequest dependencyRequest = new DependencyRequest();
            dependencyRequest.setRoot(node);
            CollectResult collectResult = mavenPluginRepository.getSystem().collectDependencies(mavenPluginRepository.getSession(), collectRequest);
            PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
            // collectResult.getRoot().accept(new
            // ConsoleDependencyGraphDumper());
            collectResult.getRoot().accept(nlg);
            try {
                mavenPluginRepository.getSystem().resolveDependencies(mavenPluginRepository.getSession(), dependencyRequest);
            } catch (DependencyResolutionException e) {
            // Ignore
            }
            for (DependencyNode dependencyNode : nlg.getNodes()) {
                ArtifactRequest newRequest = new ArtifactRequest(dependencyNode);
                newRequest.setRepositories(mavenPluginRepository.getRepositories());
                ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), newRequest);
                Artifact artifact = resolveArtifact.getArtifact();
                Path jarFile = Paths.get(artifact.getFile().getAbsolutePath());
                loadDependencies(jarFile, depDelLoader);
                Artifact versionArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion());
                ArtifactRequest request = new ArtifactRequest();
                request.setArtifact(versionArtifact);
                request.setRepositories(mavenPluginRepository.getRepositories());
                // try {
                // ArtifactResult resolveArtifact =
                // mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(),
                // request);
                // File depPomFile =
                // resolveArtifact.getArtifact().getFile();
                // if (depPomFile != null) {
                // MavenXpp3Reader mavenreader = new MavenXpp3Reader();
                // Model depModel = null;
                // try (FileReader reader = new FileReader(depPomFile)) {
                // try {
                // depModel = mavenreader.read(reader);
                // } catch (XmlPullParserException e) {
                // e.printStackTrace();
                // }
                // }
                // previous = loadDependencies(bimServerDependencies,
                // depModel, previous);
                // } else {
                // LOGGER.info("Artifact not found " + versionArtifact);
                // }
                // } catch (ArtifactResolutionException e1) {
                // LOGGER.error(e1.getMessage());
                // }
                // EclipsePluginClassloader depLoader = new
                // EclipsePluginClassloader(depDelLoader, projectRoot);
                bimServerDependencies.add(new org.bimserver.plugins.Dependency(jarFile));
            }
            previous = depDelLoader;
        } catch (DependencyCollectionException e) {
            e.printStackTrace();
        } catch (ArtifactDescriptorException e2) {
            e2.printStackTrace();
        } catch (ArtifactResolutionException e) {
            e.printStackTrace();
        }
    }
    return previous;
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) Path(java.nio.file.Path) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) CollectResult(org.eclipse.aether.collection.CollectResult) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 3 with DependencyCollectionException

use of org.eclipse.aether.collection.DependencyCollectionException in project gate-core by GateNLP.

the class PackageGappTask method execute.

@Override
public void execute() throws BuildException {
    // process the hints
    for (MappingHint h : hintTasks) {
        h.perform();
    }
    // map to store the necessary file copy operations
    Map<URL, URL> fileCopyMap = new HashMap<URL, URL>();
    Map<URL, URL> dirCopyMap = new HashMap<URL, URL>();
    TreeMap<URL, URL> pluginCopyMap = new TreeMap<URL, URL>(PATH_COMPARATOR);
    log("Packaging gapp file " + src);
    // do the work
    GappModel gappModel = null;
    URL newFileURL = null;
    try {
        URL gateHomeURL = null;
        // convert gateHome to a URL, if it was provided
        if (gateHome != null) {
            gateHomeURL = gateHome.toURI().toURL();
        }
        URL resourcesHomeURL = null;
        // convert resourcesHome to a URL, if it was provided
        if (resourcesHome != null) {
            resourcesHomeURL = resourcesHome.toURI().toURL();
        }
        gappModel = new GappModel(src.toURI().toURL(), gateHomeURL, resourcesHomeURL);
        newFileURL = destFile.toURI().toURL();
        gappModel.setGappFileURL(newFileURL);
    } catch (MalformedURLException e) {
        throw new BuildException("Couldn't convert src or dest file to URL", e, getLocation());
    }
    // we use TreeSet for these sets so we will process the paths
    // higher up the directory tree before paths pointing to their
    // subdirectories.
    Set<URL> plugins = new TreeSet<URL>(PATH_COMPARATOR);
    plugins.addAll(gappModel.getPluginURLs());
    Set<URL> resources = new TreeSet<URL>(PATH_COMPARATOR);
    resources.addAll(gappModel.getResourceURLs());
    // process the extraresourcespath elements (if any)
    processExtraResourcesPaths(resources);
    // first look at the explicit mapping hints
    if (mappingHints != null && !mappingHints.isEmpty()) {
        Iterator<URL> resourcesIt = resources.iterator();
        while (resourcesIt.hasNext()) {
            URL resource = resourcesIt.next();
            for (URL hint : mappingHints.keySet()) {
                String hintString = hint.toExternalForm();
                if (resource.equals(hint) || (hintString.endsWith("/") && resource.toExternalForm().startsWith(hintString))) {
                    // found this resource under this hint
                    log("Found resource " + resource + " under mapping hint URL " + hint, Project.MSG_VERBOSE);
                    String hintTarget = mappingHints.get(hint);
                    URL newResourceURL = null;
                    if (hintTarget == null) {
                        // hint asks to map to an absolute URL
                        log("  Converting to absolute URL", Project.MSG_VERBOSE);
                        newResourceURL = resource;
                    } else {
                        // construct the new URL relative to the hint target
                        try {
                            URL mappedHint = new URL(newFileURL, hintTarget);
                            String resourceRelpath = PersistenceManager.getRelativePath(hint, resource);
                            newResourceURL = new URL(mappedHint, resourceRelpath);
                            fileCopyMap.put(resource, newResourceURL);
                            if (copyResourceDirs) {
                                dirCopyMap.put(new URL(resource, "."), new URL(newResourceURL, "."));
                            }
                        } catch (MalformedURLException e) {
                            throw new BuildException("Couldn't construct URL relative to " + hintTarget + " for " + resource, e, getLocation());
                        }
                        log("  Relocating to " + newResourceURL, Project.MSG_VERBOSE);
                    }
                    // do the relocation
                    gappModel.updatePathForURL(resource, newResourceURL, hintTarget != null);
                    // we've now dealt with this resource, so don't need to
                    // handle it later
                    resourcesIt.remove();
                    break;
                }
            }
        }
    }
    // Any resources that aren't covered by the hints, try and
    // resolve them relative to the plugins referenced by the
    // application.
    Iterator<URL> pluginsIt = plugins.iterator();
    while (pluginsIt.hasNext()) {
        URL pluginURL = pluginsIt.next();
        pluginsIt.remove();
        URL newPluginURL = null;
        String pluginName = pluginURL.getFile();
        log("Processing plugin " + pluginName, Project.MSG_VERBOSE);
        // first check whether this plugin is a subdirectory of another plugin
        // we have already processed
        SortedMap<URL, URL> possibleAncestors = pluginCopyMap.headMap(pluginURL);
        URL ancestorPlugin = null;
        if (!possibleAncestors.isEmpty())
            ancestorPlugin = possibleAncestors.lastKey();
        if (ancestorPlugin != null && pluginURL.toExternalForm().startsWith(ancestorPlugin.toExternalForm())) {
            // this plugin is under one we have already dealt with
            log("  Plugin is located under another plugin " + ancestorPlugin, Project.MSG_VERBOSE);
            String relPath = PersistenceManager.getRelativePath(ancestorPlugin, pluginURL);
            try {
                newPluginURL = new URL(pluginCopyMap.get(ancestorPlugin), relPath);
            } catch (MalformedURLException e) {
                throw new BuildException("Couldn't construct URL relative to plugins/" + " for " + pluginURL, e, getLocation());
            }
        } else {
            // normal case, this plugin is not a subdir of another plugin
            boolean addSlash = false;
            // we will map the plugin whose directory name is X to plugins/X
            if (pluginName.endsWith("/")) {
                addSlash = true;
                pluginName = pluginName.substring(pluginName.lastIndexOf('/', pluginName.length() - 2) + 1, pluginName.length() - 1);
            } else {
                pluginName = pluginName.substring(pluginName.lastIndexOf('/') + 1);
            }
            log("  Plugin name is " + pluginName, Project.MSG_VERBOSE);
            try {
                newPluginURL = new URL(newFileURL, "plugins/" + pluginName + (addSlash ? "/" : ""));
                // until we find one that is available.
                if (pluginCopyMap.containsValue(newPluginURL)) {
                    int index = 2;
                    do {
                        newPluginURL = new URL(newFileURL, "plugins/" + pluginName + "-" + (index++) + (addSlash ? "/" : ""));
                    } while (pluginCopyMap.containsValue(newPluginURL));
                }
            } catch (MalformedURLException e) {
                throw new BuildException("Couldn't construct URL relative to plugins/" + " for " + pluginURL, e, getLocation());
            }
        }
        log("  Relocating to " + newPluginURL, Project.MSG_VERBOSE);
        // deal with the plugin URL itself (in the urlList)
        gappModel.updatePathForURL(pluginURL, newPluginURL, true);
        pluginCopyMap.put(pluginURL, newPluginURL);
        // now look for resources located under that plugin
        String pluginUri = pluginURL.toExternalForm();
        if (!pluginUri.endsWith("/")) {
            pluginUri += "/";
        }
        Iterator<URL> resourcesIt = resources.iterator();
        while (resourcesIt.hasNext()) {
            URL resourceURL = resourcesIt.next();
            try {
                if (resourceURL.toExternalForm().startsWith(pluginUri)) {
                    // found a resource under this plugin, so relocate it to be
                    // under the re-located plugin dir
                    resourcesIt.remove();
                    String resourceRelpath = PersistenceManager.getRelativePath(pluginURL, resourceURL);
                    log("    Found resource " + resourceURL, Project.MSG_VERBOSE);
                    URL newResourceURL = null;
                    newResourceURL = new URL(newPluginURL, resourceRelpath);
                    log("    Relocating to " + newResourceURL, Project.MSG_VERBOSE);
                    gappModel.updatePathForURL(resourceURL, newResourceURL, true);
                    fileCopyMap.put(resourceURL, newResourceURL);
                    if (copyResourceDirs) {
                        dirCopyMap.put(new URL(resourceURL, "."), new URL(newResourceURL, "."));
                    }
                }
            } catch (MalformedURLException e) {
                throw new BuildException("Couldn't construct URL relative to " + newPluginURL + " for " + resourceURL, e, getLocation());
            }
        }
    }
    // anything left over, handle according to onUnresolved
    if (!resources.isEmpty()) {
        switch(onUnresolved) {
            case fail:
                // easy case - fail the build
                log("There were unresolved resources:", Project.MSG_ERR);
                for (URL res : resources) {
                    log(res.toExternalForm(), Project.MSG_ERR);
                }
                log("Either set onUnresolved=\"absolute|recover\" or add the " + "relevant mapping hints", Project.MSG_ERR);
                throw new BuildException("There were unresolved resources", getLocation());
            case absolute:
                // convert all unresolved resources to absolute URLs
                log("There were unresolved resources, which have been made absolute", Project.MSG_WARN);
                for (URL res : resources) {
                    gappModel.updatePathForURL(res, res, false);
                    log(res.toExternalForm(), Project.MSG_VERBOSE);
                }
                break;
            case recover:
                // the clever case - recover by putting all the unresolved
                // resources into subdirectories of an "application-resources"
                // directory under the output dir
                URL unresolvedResourcesDir = null;
                try {
                    unresolvedResourcesDir = new URL(newFileURL, "application-resources/");
                } catch (MalformedURLException e) {
                    throw new BuildException("Can't construct URL relative to " + newFileURL + " for application-resources", e, getLocation());
                }
                // map to track where under application-resources we should map
                // each directory that contains unresolved resources
                TreeMap<URL, URL> unresolvedResourcesSubDirs = new TreeMap<URL, URL>(PATH_COMPARATOR);
                log("There were unresolved resources, which have been gathered into " + unresolvedResourcesDir, Project.MSG_INFO);
                for (URL res : resources) {
                    URL resourceDir = null;
                    try {
                        resourceDir = new URL(res, ".");
                    } catch (MalformedURLException e) {
                        throw new BuildException("Can't construct URL to parent directory of " + res, e, getLocation());
                    }
                    URL targetDir = getUnresolvedResourcesTarget(unresolvedResourcesSubDirs, unresolvedResourcesDir, resourceDir);
                    String resName = res.getFile();
                    resName = resName.substring(resName.lastIndexOf('/') + 1);
                    URL newResourceURL = null;
                    try {
                        newResourceURL = new URL(targetDir, resName);
                    } catch (MalformedURLException e) {
                        throw new BuildException("Can't construct URL relative to " + unresolvedResourcesDir + " for " + resName, e, getLocation());
                    }
                    gappModel.updatePathForURL(res, newResourceURL, true);
                    fileCopyMap.put(res, newResourceURL);
                    if (copyResourceDirs) {
                        dirCopyMap.put(resourceDir, targetDir);
                    }
                }
                break;
            default:
                throw new BuildException("Unrecognised UnresolvedAction", getLocation());
        }
    }
    // write out the fixed GAPP file
    try {
        log("Writing modified gapp to " + destFile);
        gappModel.write();
    } catch (IOException e) {
        throw new BuildException("Error writing out modified GAPP file", e, getLocation());
    }
    // now copy the files that it references
    if (fileCopyMap.size() > 0) {
        log("Copying " + fileCopyMap.size() + " resources");
    }
    for (Map.Entry<URL, URL> resEntry : fileCopyMap.entrySet()) {
        File source = Files.fileFromURL(resEntry.getKey());
        File dest = Files.fileFromURL(resEntry.getValue());
        if (source.isDirectory()) {
            // source URL points to a directory, so create a corresponding
            // directory dest
            dest.mkdirs();
        } else {
            // source URL doesn't point to a directory, so
            // ensure parent directory exists
            dest.getParentFile().mkdirs();
            if (source.isFile()) {
                // source URL points to an existing file, copy it
                try {
                    log("Copying " + source + " to " + dest, Project.MSG_VERBOSE);
                    FileUtils.getFileUtils().copyFile(source, dest);
                } catch (IOException e) {
                    throw new BuildException("Error copying file " + source + " to " + dest, e, getLocation());
                }
            }
        }
    }
    // handle the plugins
    if (pluginCopyMap.size() > 0) {
        log("Copying " + pluginCopyMap.size() + " plugins");
        if (copyPlugins) {
            log("Also copying complete plugin contents", Project.MSG_VERBOSE);
        }
        copyDirectories(pluginCopyMap, !copyPlugins);
    }
    // handle extra directories
    if (dirCopyMap.size() > 0) {
        log("Copying " + dirCopyMap.size() + " resource directories");
        copyDirectories(dirCopyMap, false);
    }
    if (mavenCache != null) {
        List<GappModel.MavenPlugin> mavenPlugins = gappModel.getMavenPlugins();
        if (!mavenPlugins.isEmpty()) {
            log("Creating Maven cache at " + mavenCache.getAbsolutePath());
            SimpleMavenCache smc = new SimpleMavenCache(mavenCache);
            for (GappModel.MavenPlugin plugin : mavenPlugins) {
                log("  Cacheing " + plugin.group + ":" + plugin.artifact + ":" + plugin.version, Project.MSG_VERBOSE);
                Artifact pluginArtifact = new DefaultArtifact(plugin.group, plugin.artifact, "jar", plugin.version);
                try {
                    smc.cacheArtifact(pluginArtifact);
                } catch (DependencyCollectionException | DependencyResolutionException | ArtifactResolutionException | IOException | SettingsBuildingException | ModelBuildingException e) {
                    throw new BuildException("Error cacheing plugin " + plugin.group + ":" + plugin.artifact + ":" + plugin.version, e);
                }
                // and the matching creole.jar if there is one
                try {
                    smc.cacheArtifact(new SubArtifact(pluginArtifact, "creole", "jar"));
                } catch (DependencyCollectionException | DependencyResolutionException | ArtifactResolutionException | IOException | SettingsBuildingException | ModelBuildingException e) {
                    log("    Could not cache " + plugin.group + ":" + plugin.artifact + "-" + plugin.version + "-creole.jar -  cache will still work but will " + "print warnings at runtime");
                }
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleMavenCache(gate.util.maven.SimpleMavenCache) URL(java.net.URL) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) TreeSet(java.util.TreeSet) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) BuildException(org.apache.tools.ant.BuildException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 4 with DependencyCollectionException

use of org.eclipse.aether.collection.DependencyCollectionException in project sts4 by spring-projects.

the class MavenCore method readDependencyTree.

/**
 * Taken from M2E same named method from MavenModelManager
 *
 * @param repositorySystem
 * @param repositorySession
 * @param mavenProject
 * @param scope
 * @return
 */
private DependencyNode readDependencyTree(org.eclipse.aether.RepositorySystem repositorySystem, RepositorySystemSession repositorySession, MavenProject mavenProject, String scope) {
    DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(repositorySession);
    ConflictResolver transformer = new ConflictResolver(new NearestVersionSelector(), new JavaScopeSelector(), new SimpleOptionalitySelector(), new JavaScopeDeriver());
    session.setDependencyGraphTransformer(transformer);
    session.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, Boolean.toString(true));
    session.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
    ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
    CollectRequest request = new CollectRequest();
    // $NON-NLS-1$
    request.setRequestContext("project");
    request.setRepositories(mavenProject.getRemoteProjectRepositories());
    for (org.apache.maven.model.Dependency dependency : mavenProject.getDependencies()) {
        request.addDependency(RepositoryUtils.toDependency(dependency, stereotypes));
    }
    DependencyManagement depMngt = mavenProject.getDependencyManagement();
    if (depMngt != null) {
        for (org.apache.maven.model.Dependency dependency : depMngt.getDependencies()) {
            request.addManagedDependency(RepositoryUtils.toDependency(dependency, stereotypes));
        }
    }
    DependencyNode node;
    try {
        node = repositorySystem.collectDependencies(session, request).getRoot();
    } catch (DependencyCollectionException ex) {
        node = ex.getResult().getRoot();
    }
    Collection<String> scopes = new HashSet<String>();
    Collections.addAll(scopes, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST);
    if (Artifact.SCOPE_COMPILE.equals(scope)) {
        scopes.remove(Artifact.SCOPE_COMPILE);
        scopes.remove(Artifact.SCOPE_SYSTEM);
        scopes.remove(Artifact.SCOPE_PROVIDED);
    } else if (Artifact.SCOPE_RUNTIME.equals(scope)) {
        scopes.remove(Artifact.SCOPE_COMPILE);
        scopes.remove(Artifact.SCOPE_RUNTIME);
    } else if (Artifact.SCOPE_COMPILE_PLUS_RUNTIME.equals(scope)) {
        scopes.remove(Artifact.SCOPE_COMPILE);
        scopes.remove(Artifact.SCOPE_SYSTEM);
        scopes.remove(Artifact.SCOPE_PROVIDED);
        scopes.remove(Artifact.SCOPE_RUNTIME);
    } else {
        scopes.clear();
    }
    CloningDependencyVisitor cloner = new CloningDependencyVisitor();
    node.accept(new FilteringDependencyVisitor(cloner, new ScopeDependencyFilter(null, scopes)));
    node = cloner.getRootNode();
    return node;
}
Also used : DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) CloningDependencyVisitor(org.eclipse.aether.util.graph.visitor.CloningDependencyVisitor) FilteringDependencyVisitor(org.eclipse.aether.util.graph.visitor.FilteringDependencyVisitor) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) CollectRequest(org.eclipse.aether.collection.CollectRequest) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) DependencyNode(org.eclipse.aether.graph.DependencyNode) ScopeDependencyFilter(org.eclipse.aether.util.filter.ScopeDependencyFilter) DependencyManagement(org.apache.maven.model.DependencyManagement) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with DependencyCollectionException

use of org.eclipse.aether.collection.DependencyCollectionException in project byte-buddy by raphw.

the class ClassLoaderResolver method doResolve.

/**
     * Resolves a Maven coordinate to a class loader that can load all of the coordinates classes.
     *
     * @param mavenCoordinate The Maven coordinate to resolve.
     * @return A class loader that references all of the class loader's dependencies and which is a child of this class's class loader.
     * @throws MojoExecutionException If the user configuration results in an error.
     * @throws MojoFailureException   If the plugin application raises an error.
     */
private ClassLoader doResolve(MavenCoordinate mavenCoordinate) throws MojoExecutionException, MojoFailureException {
    List<URL> urls = new ArrayList<URL>();
    log.info("Resolving transformer dependency: " + mavenCoordinate);
    try {
        DependencyNode root = repositorySystem.collectDependencies(repositorySystemSession, new CollectRequest(new Dependency(mavenCoordinate.asArtifact(), "runtime"), remoteRepositories)).getRoot();
        repositorySystem.resolveDependencies(repositorySystemSession, new DependencyRequest().setRoot(root));
        PreorderNodeListGenerator preorderNodeListGenerator = new PreorderNodeListGenerator();
        root.accept(preorderNodeListGenerator);
        for (Artifact artifact : preorderNodeListGenerator.getArtifacts(false)) {
            urls.add(artifact.getFile().toURI().toURL());
        }
    } catch (DependencyCollectionException exception) {
        throw new MojoExecutionException("Could not collect dependencies for " + mavenCoordinate, exception);
    } catch (DependencyResolutionException exception) {
        throw new MojoExecutionException("Could not resolve dependencies for " + mavenCoordinate, exception);
    } catch (MalformedURLException exception) {
        throw new MojoFailureException("Could not resolve file as URL for " + mavenCoordinate, exception);
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), ByteBuddy.class.getClassLoader());
}
Also used : DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ByteBuddy(net.bytebuddy.ByteBuddy) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) URL(java.net.URL) Artifact(org.eclipse.aether.artifact.Artifact) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) URLClassLoader(java.net.URLClassLoader) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException)

Aggregations

DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)6 CollectRequest (org.eclipse.aether.collection.CollectRequest)5 DependencyNode (org.eclipse.aether.graph.DependencyNode)4 DependencyResolutionException (org.eclipse.aether.resolution.DependencyResolutionException)4 Artifact (org.eclipse.aether.artifact.Artifact)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 CollectResult (org.eclipse.aether.collection.CollectResult)2 Dependency (org.eclipse.aether.graph.Dependency)2 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)2 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)2 SimpleMavenCache (gate.util.maven.SimpleMavenCache)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 URLClassLoader (java.net.URLClassLoader)1 FileSystemNotFoundException (java.nio.file.FileSystemNotFoundException)1 Path (java.nio.file.Path)1