All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rempl-maven-plugin-1.1.3.src.main.java.com.rempl.maven.MavenResolver Maven / Gradle / Ivy

Go to download

Reverse Engineering Meta Programming Library (REMPL) that enables manipulations with source code meta constructs, like classes, methods, files, packages, etc. in runtime.

There is a newer version: 1.4
Show newest version
/**
 * Copyright (c) 2011, REMPL.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1) Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2) Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 * 3) Neither the name of the REMPL.com nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.rempl.maven;

// REMPL API from com.rempl:rempl-api
import com.rempl.aether.Resolver;

// Bridget to Aether from com.rempl:rempl-aether
import com.rempl.api.Artifact;
import com.rempl.api.LoadingException;
import com.ymock.util.Logger;

// basic classes, to return result
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.collection.DependencyCollectionException;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.ArtifactResolutionException;
import org.sonatype.aether.resolution.ArtifactResult;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.artifact.JavaScopes;
import org.sonatype.aether.util.filter.DependencyFilterUtils;

/**
 * Resolver of one artifact name into a list of JAR files.
 *
 * @author Yegor Bugayenko ([email protected])
 * @version $Id: MavenResolver.java 1742 2011-08-03 20:10:55Z [email protected] $
 */
public final class MavenResolver implements Resolver {

    /**
     * The entry point to Aether, i.e. the component
     * doing all the work.
     */
    private RepositorySystem system;

    /**
     * The current repository/network configuration of Maven.
     */
    private RepositorySystemSession session;

    /**
     * The project's remote repositories to use for the resolution
     * of plugins and their dependencies.
     */
    private List remotes;

    /**
     * Public ctor.
     * @param sys Repository system
     * @param ses Repository system session
     * @param reps Remote reps
     * @see AbstractRemplMojo#resolver()
     */
    public MavenResolver(final RepositorySystem sys,
        final RepositorySystemSession ses, final List reps) {
        this.system = sys;
        this.session = ses;
        this.remotes = reps;
        Logger.debug(
            this,
            "#MavenResolver(%s, %s, reps)",
            sys.getClass().getName(),
            ses.getClass().getName()
        );
    }

    /**
     * Resolves the Artifact from the remote repository
     * if nessessary.
     * @param artifact The artifact to find
     * @return Collection of JAR files
     * @throws LoadingException If something goes wrong
     */
    @Override
    public Collection resolve(final Artifact artifact)
        throws LoadingException {
        final Collection found = this.artifacts(artifact);
        final Collection jars = new ArrayList();
        for (ArtifactResult aresult : found) {
            Logger.debug(
                this,
                "%s resolved to %s",
                aresult.getArtifact(),
                aresult.getArtifact().getFile()
            );
            jars.add(aresult.getArtifact().getFile());
        }
        return jars;
    }

    /**
     * Return a list of found artifacts.
     * @param artifact The name of the artifact to find
     * @return The collection of artifacts found
     * @throws LoadingException If something goes wrong
     * @see #resolve(String)
     */
    private Collection artifacts(final Artifact artifact)
        throws LoadingException {
        try {
            return this.system.resolveDependencies(
                this.session,
                this.request(artifact),
                DependencyFilterUtils.classpathFilter(
                    JavaScopes.COMPILE, JavaScopes.RUNTIME)
            );
        } catch (DependencyCollectionException ex) {
            throw new LoadingException(ex);
        } catch (ArtifactResolutionException ex) {
            throw new LoadingException(ex);
        }
    }

    /**
     * Create dependency request.
     * @param artifact The artifact to find
     * @return The request
     * @see #artifacts(Artifact)
     */
    public CollectRequest request(final Artifact artifact) {
        final CollectRequest crq = new CollectRequest();
        crq.setRoot(
            new Dependency(
                new DefaultArtifact(artifact.toString()),
                JavaScopes.COMPILE
            )
        );
        for (RemoteRepository repo : this.remotes) {
            crq.addRepository(repo);
        }
        return crq;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy