Search in sources :

Example 11 with Artifact

use of org.eclipse.aether.artifact.Artifact in project druid by druid-io.

the class PullDependencies method downloadExtension.

/**
   * Download the extension given its maven coordinate
   *
   * @param versionedArtifact The maven artifact of the extension
   * @param toLocation        The location where this extension will be downloaded to
   */
private void downloadExtension(Artifact versionedArtifact, File toLocation) {
    final CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(versionedArtifact, JavaScopes.RUNTIME));
    final DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, DependencyFilterUtils.andFilter(DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME), new DependencyFilter() {

        @Override
        public boolean accept(DependencyNode node, List<DependencyNode> parents) {
            String scope = node.getDependency().getScope();
            if (scope != null) {
                scope = scope.toLowerCase();
                if (scope.equals("provided")) {
                    return false;
                }
                if (scope.equals("test")) {
                    return false;
                }
                if (scope.equals("system")) {
                    return false;
                }
            }
            if (accept(node.getArtifact())) {
                return false;
            }
            for (DependencyNode parent : parents) {
                if (accept(parent.getArtifact())) {
                    return false;
                }
            }
            return true;
        }

        private boolean accept(final Artifact artifact) {
            return exclusions.contains(artifact.getGroupId());
        }
    }));
    try {
        log.info("Start downloading extension [%s]", versionedArtifact);
        final List<Artifact> artifacts = aether.resolveArtifacts(dependencyRequest);
        for (Artifact artifact : artifacts) {
            if (!exclusions.contains(artifact.getGroupId())) {
                log.info("Adding file [%s] at [%s]", artifact.getFile().getName(), toLocation.getAbsolutePath());
                FileUtils.copyFileToDirectory(artifact.getFile(), toLocation);
            } else {
                log.debug("Skipped Artifact[%s]", artifact);
            }
        }
    } catch (Exception e) {
        log.error(e, "Unable to resolve artifacts for [%s].", dependencyRequest);
        throw Throwables.propagate(e);
    }
    log.info("Finish downloading extension [%s]", versionedArtifact);
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 12 with Artifact

use of org.eclipse.aether.artifact.Artifact in project druid by druid-io.

the class PullDependencies method run.

@Override
public void run() {
    if (aether == null) {
        aether = getAetherClient();
    }
    final File extensionsDir = new File(extensionsConfig.getDirectory());
    final File hadoopDependenciesDir = new File(extensionsConfig.getHadoopDependenciesDir());
    try {
        if (clean) {
            FileUtils.deleteDirectory(extensionsDir);
            FileUtils.deleteDirectory(hadoopDependenciesDir);
        }
        FileUtils.forceMkdir(extensionsDir);
        FileUtils.forceMkdir(hadoopDependenciesDir);
    } catch (IOException e) {
        log.error(e, "Unable to clear or create extension directory at [%s]", extensionsDir);
        throw Throwables.propagate(e);
    }
    log.info("Start pull-deps with local repository [%s] and remote repositories [%s]", localRepository, remoteRepositories);
    try {
        log.info("Start downloading dependencies for extension coordinates: [%s]", coordinates);
        for (String coordinate : coordinates) {
            coordinate = coordinate.trim();
            final Artifact versionedArtifact = getArtifact(coordinate);
            File currExtensionDir = new File(extensionsDir, versionedArtifact.getArtifactId());
            createExtensionDirectory(coordinate, currExtensionDir);
            downloadExtension(versionedArtifact, currExtensionDir);
        }
        log.info("Finish downloading dependencies for extension coordinates: [%s]", coordinates);
        if (!noDefaultHadoop && hadoopCoordinates.isEmpty()) {
            hadoopCoordinates.addAll(TaskConfig.DEFAULT_DEFAULT_HADOOP_COORDINATES);
        }
        log.info("Start downloading dependencies for hadoop extension coordinates: [%s]", hadoopCoordinates);
        for (final String hadoopCoordinate : hadoopCoordinates) {
            final Artifact versionedArtifact = getArtifact(hadoopCoordinate);
            File currExtensionDir = new File(hadoopDependenciesDir, versionedArtifact.getArtifactId());
            createExtensionDirectory(hadoopCoordinate, currExtensionDir);
            // add a version folder for hadoop dependency directory
            currExtensionDir = new File(currExtensionDir, versionedArtifact.getVersion());
            createExtensionDirectory(hadoopCoordinate, currExtensionDir);
            downloadExtension(versionedArtifact, currExtensionDir);
        }
        log.info("Finish downloading dependencies for hadoop extension coordinates: [%s]", hadoopCoordinates);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 13 with Artifact

use of org.eclipse.aether.artifact.Artifact in project druid by druid-io.

the class PullDependenciesTest method setUp.

@Before
public void setUp() throws Exception {
    localRepo = temporaryFolder.newFolder();
    extensionToJars = new HashMap<>();
    extensionToJars.put(extension_A, ImmutableList.of("a.jar", "b.jar", "c.jar"));
    extensionToJars.put(extension_B, ImmutableList.of("d.jar", "e.jar"));
    extensionToJars.put(hadoop_client_2_3_0, ImmutableList.of("f.jar", "g.jar"));
    extensionToJars.put(hadoop_client_2_4_0, ImmutableList.of("h.jar", "i.jar"));
    rootExtensionsDir = new File(temporaryFolder.getRoot(), "extensions");
    rootHadoopDependenciesDir = new File(temporaryFolder.getRoot(), "druid_hadoop_dependencies");
    pullDependencies = new PullDependencies(new DefaultTeslaAether() {

        @Override
        public List<Artifact> resolveArtifacts(DependencyRequest request) throws DependencyResolutionException {
            return getArtifactsForExtension(request.getCollectRequest().getRoot().getArtifact());
        }
    }, new ExtensionsConfig() {

        @Override
        public String getDirectory() {
            return rootExtensionsDir.getAbsolutePath();
        }

        @Override
        public String getHadoopDependenciesDir() {
            return rootHadoopDependenciesDir.getAbsolutePath();
        }
    });
    pullDependencies.coordinates = ImmutableList.of(EXTENSION_A_COORDINATE, EXTENSION_B_COORDINATE);
    pullDependencies.hadoopCoordinates = ImmutableList.of(HADOOP_CLIENT_2_3_0_COORDINATE, HADOOP_CLIENT_2_4_0_COORDINATE);
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DefaultTeslaAether(io.tesla.aether.internal.DefaultTeslaAether) ExtensionsConfig(io.druid.guice.ExtensionsConfig) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) Before(org.junit.Before)

Example 14 with Artifact

use of org.eclipse.aether.artifact.Artifact in project druid by druid-io.

the class PullDependenciesTest method getArtifactsForExtension.

private List<Artifact> getArtifactsForExtension(Artifact artifact) {
    final List<String> jarNames = extensionToJars.get(artifact);
    final List<Artifact> artifacts = Lists.newArrayList();
    for (String jarName : jarNames) {
        final File jarFile = new File(localRepo, jarName);
        try {
            jarFile.createNewFile();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
        artifacts.add(new DefaultArtifact(null, jarName, null, "jar", "1.0", null, jarFile));
    }
    return artifacts;
}
Also used : IOException(java.io.IOException) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 15 with Artifact

use of org.eclipse.aether.artifact.Artifact in project bnd by bndtools.

the class AetherRepository method get.

@Override
public File get(String bsn, Version version, Map<String, String> properties, DownloadListener... listeners) throws Exception {
    init();
    // Use the index by preference
    if (indexedRepo != null)
        return indexedRepo.get(ConversionUtils.maybeMavenCoordsToBsn(bsn), version, properties, listeners);
    File file = null;
    boolean getSource = false;
    try {
        // Setup the Aether repo session and request
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
        session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, localRepo));
        if (bsn.endsWith(".source")) {
            String originalBsn = properties.get("bsn");
            if (originalBsn != null) {
                bsn = originalBsn;
                getSource = true;
            }
        }
        String[] coords = ConversionUtils.getGroupAndArtifactForBsn(bsn);
        MavenVersion mvnVersion = new MavenVersion(version);
        String versionStr = null;
        if ("exact".equals(properties.get("strategy")) || getSource) {
            versionStr = properties.get("version");
        } else {
            versionStr = mvnVersion.toString();
        }
        Artifact artifact = null;
        if (getSource) {
            artifact = new DefaultArtifact(coords[0], coords[1], "sources", "jar", versionStr);
        } else {
            artifact = new DefaultArtifact(coords[0], coords[1], "jar", versionStr);
        }
        ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(artifact);
        request.setRepositories(Collections.singletonList(remoteRepo));
        // Log the transfer
        session.setTransferListener(new AbstractTransferListener() {

            @Override
            public void transferStarted(TransferEvent event) throws TransferCancelledException {
                System.err.println(event);
            }

            @Override
            public void transferSucceeded(TransferEvent event) {
                System.err.println(event);
            }

            @Override
            public void transferFailed(TransferEvent event) {
                System.err.println(event);
            }
        });
        try {
            // Resolve the version
            ArtifactResult artifactResult = repoSystem.resolveArtifact(session, request);
            artifact = artifactResult.getArtifact();
            file = artifact.getFile();
        } catch (ArtifactResolutionException ex) {
        // could not download artifact, simply return null
        }
        return file;
    } finally {
        for (DownloadListener dl : listeners) {
            if (file != null)
                dl.success(file);
            else
                dl.failure(null, "Download failed");
        }
    }
}
Also used : AbstractTransferListener(org.eclipse.aether.transfer.AbstractTransferListener) TransferCancelledException(org.eclipse.aether.transfer.TransferCancelledException) TransferEvent(org.eclipse.aether.transfer.TransferEvent) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenVersion(aQute.bnd.version.MavenVersion) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Aggregations

Artifact (org.eclipse.aether.artifact.Artifact)41 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)30 File (java.io.File)11 Dependency (org.eclipse.aether.graph.Dependency)9 SubArtifact (org.eclipse.aether.util.artifact.SubArtifact)9 IOException (java.io.IOException)8 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)8 ArrayList (java.util.ArrayList)7 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)7 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)7 Path (java.nio.file.Path)6 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)6 CollectRequest (org.eclipse.aether.collection.CollectRequest)6 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)5 STGroupString (org.stringtemplate.v4.STGroupString)5 Jar (aQute.bnd.osgi.Jar)4 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 List (java.util.List)4 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)4