Example usage for org.apache.maven.artifact.resolver ArtifactResolver resolve

List of usage examples for org.apache.maven.artifact.resolver ArtifactResolver resolve

Introduction

In this page you can find the example usage for org.apache.maven.artifact.resolver ArtifactResolver resolve.

Prototype

@Deprecated
    void resolve(Artifact artifact, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository)
            throws ArtifactResolutionException, ArtifactNotFoundException;

Source Link

Usage

From source file:com.alibaba.citrus.maven.eclipse.base.ide.IdeUtils.java

License:Apache License

/**
 * Wrapper around {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 *
 * @param artifactResolver see {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 * @param artifact         see {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 * @param remoteRepos      see {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 * @param localRepository  see {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 * @param log              Logger//from w  ww. j  a v a2 s  . c om
 * @return the artifact, resolved if possible.
 */
public static Artifact resolveArtifact(ArtifactResolver artifactResolver, Artifact artifact, List remoteRepos,
        ArtifactRepository localRepository, Log log)

{
    try {
        artifactResolver.resolve(artifact, remoteRepos, localRepository);
    } catch (ArtifactNotFoundException e) {
        // ignore, the jar has not been found

        /*
         * This method gets called with no remote repositories to avoid remote trips (which would ideally be
         * realized by means of a per-request offline flag), the set of available remote repos can however affect
         * the resolution from the local repo as well, in particular in Maven 3. So double check whether the local
         * file is really not present.
         */
        if (artifact.getFile() != null && artifact.getFile().isFile()) {
            artifact.setResolved(true);
        }
    } catch (ArtifactResolutionException e) {
        String message = Messages.getString("IdeUtils.errorresolving",
                new Object[] { artifact.getClassifier(), artifact.getId(), e.getMessage() });

        log.warn(message);
    }

    return artifact;
}

From source file:com.github.maven_nar.NarManager.java

License:Apache License

public final void downloadAttachedNars(final List/* <NarArtifacts> */ narArtifacts,
        final List remoteRepositories, final ArtifactResolver resolver, final String classifier)
        throws MojoExecutionException, MojoFailureException {
    // FIXME this may not be the right way to do this.... -U ignored and
    // also SNAPSHOT not used
    final List dependencies = getAttachedNarDependencies(narArtifacts, classifier);

    this.log.debug("Download called with classifier: " + classifier + " for NarDependencies {");
    for (final Iterator i = dependencies.iterator(); i.hasNext();) {
        this.log.debug("  - " + i.next());
    }/*  www . j a v a 2s  .c om*/
    this.log.debug("}");

    for (final Iterator i = dependencies.iterator(); i.hasNext();) {
        final Artifact dependency = (Artifact) i.next();
        try {
            this.log.debug("Resolving " + dependency);
            resolver.resolve(dependency, remoteRepositories, this.repository);
        } catch (final ArtifactNotFoundException e) {
            final String message = "nar not found " + dependency.getId();
            throw new MojoExecutionException(message, e);
        } catch (final ArtifactResolutionException e) {
            final String message = "nar cannot resolve " + dependency.getId();
            throw new MojoExecutionException(message, e);
        }
    }
}

From source file:com.redhat.rcm.maven.plugin.buildmetadata.util.ReportUtils.java

License:Apache License

/**
 * Returns a file reference to the default skin useful for rendering
 * standalone run reports./* www . j a v a  2 s  .c o  m*/
 * <p>
 * Stolen from the changes plugin.
 * </p>
 *
 * @param project the project of the plugin that calls this method.
 * @param localRepository a reference to the local repository to reference to.
 * @param resolver to resolve the skin artifact.
 * @param factory to resolve dependencies.
 * @return a file reference to the default skin.
 * @throws MojoExecutionException if the skin artifact cannot be resolved.
 */
public static File getSkinArtifactFile(final MavenProject project, final ArtifactRepository localRepository,
        final ArtifactResolver resolver, final ArtifactFactory factory) throws MojoExecutionException {
    final Skin skin = Skin.getDefaultSkin();
    final String version = determineVersion(skin);
    try {
        final VersionRange versionSpec = VersionRange.createFromVersionSpec(version);
        final Artifact artifact = factory.createDependencyArtifact(skin.getGroupId(), skin.getArtifactId(),
                versionSpec, "jar", null, null);
        resolver.resolve(artifact, project.getRemoteArtifactRepositories(), localRepository);

        return artifact.getFile();
    } catch (final InvalidVersionSpecificationException e) {
        throw new MojoExecutionException("The skin version '" + version + "' is not valid: " + e.getMessage(),
                e);
    } catch (final ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to find skin", e);
    } catch (final ArtifactNotFoundException e) {
        throw new MojoExecutionException("The skin does not exist: " + e.getMessage(), e);
    }
}

From source file:fr.imag.adele.cadse.platform.DependenciesTask.java

License:Apache License

/**
 * Main task execution.  Called by parent execute().
 *//*from   w w w  .  j  a  v  a2  s . c o m*/
protected void doExecute() {

    if (useScope != null && scopes != null) {
        throw new BuildException("You cannot specify both useScope and scopes in the dependencies task.");
    }

    if (getPom() != null && !this.dependencies.isEmpty()) {
        throw new BuildException("You cannot specify both dependencies and a pom in the dependencies task");
    }

    // Try to load dependency refs from an exsiting Ant cache file
    if (isCacheDependencyRefs()) {
        if (getDependencyRefsBuildFile() == null) {
            setDependencyRefsBuildFile(DEFAULT_ANT_BUILD_FILE);
        }
        if (checkCachedDependencies()) {
            log("Dependency refs loaded from file: " + getDependencyRefsBuildFile(), Project.MSG_VERBOSE);
            return;
        }
    }

    ArtifactRepository localRepo = createLocalArtifactRepository();
    log("Using local repository: " + localRepo.getBasedir(), Project.MSG_VERBOSE);

    // Look up required resources from the plexus container
    ArtifactResolver resolver = (ArtifactResolver) lookup(ArtifactResolver.ROLE);
    ArtifactFactory artifactFactory = (ArtifactFactory) lookup(ArtifactFactory.ROLE);
    MavenMetadataSource metadataSource = (MavenMetadataSource) lookup(ArtifactMetadataSource.ROLE);

    Pom pom = initializePom(localRepo);
    if (pom != null) {
        dependencies = pom.getDependencies();
    } else {
        // we have to have some sort of Pom object in order to satisfy the requirements for building the
        // originating Artifact below...
        pom = createDummyPom(localRepo);
    }

    if (dependencies.isEmpty()) {
        log("There were no dependencies specified", Project.MSG_WARN);
    }

    log("Resolving dependencies...", Project.MSG_VERBOSE);

    Set artifacts;

    List remoteArtifactRepositories = createRemoteArtifactRepositories(pom.getRepositories());

    try {
        artifacts = MavenMetadataSource.createArtifacts(artifactFactory, dependencies, null, null, null);

        Artifact pomArtifact = artifactFactory.createBuildArtifact(pom.getGroupId(), pom.getArtifactId(),
                pom.getVersion(), pom.getPackaging());

        List listeners = Collections.singletonList(new AntResolutionListener(getProject()));

        ArtifactFilter filter = null;
        if (useScope != null) {
            filter = new ScopeArtifactFilter(useScope);
        }
        if (scopes != null) {
            filter = new SpecificScopesArtifactFilter(scopes);
        }
        if (type != null) {
            ArtifactFilter typeArtifactFilter = new TypesArtifactFilter(type);
            if (filter != null) {
                AndArtifactFilter andFilter = new AndArtifactFilter();
                andFilter.add(filter);
                andFilter.add(typeArtifactFilter);
                filter = andFilter;
            } else {
                filter = typeArtifactFilter;
            }
        }
        for (Object o : artifacts) {
            resolver.resolve((Artifact) o, remoteArtifactRepositories, localRepo);
        }

    } catch (ArtifactResolutionException e) {
        throw new BuildException("Unable to resolve artifact: " + e.getMessage(), e);
    } catch (ArtifactNotFoundException e) {
        throw new BuildException("Dependency not found: " + e.getMessage(), e);
    } catch (InvalidDependencyVersionException e) {
        throw new BuildException("Invalid dependency version: " + e.getMessage(), e);
    }

    FileSet dependencyFileSet = createFileSet();

    FileSet sourcesFileSet = createFileSet();

    FileSet javadocsFileSet = createFileSet();

    Path dependencyPath = new Path(getProject());

    Set versions = new HashSet();

    for (Object o : artifacts) {
        Artifact artifact = (Artifact) o;

        addArtifactToResult(localRepo, artifact, dependencyFileSet, dependencyPath);

        versions.add(artifact.getVersion());

        if (sourcesFilesetId != null) {
            resolveSource(artifactFactory, resolver, remoteArtifactRepositories, localRepo, artifact, "sources",
                    sourcesFileSet);
        }

        if (javadocFilesetId != null) {
            resolveSource(artifactFactory, resolver, remoteArtifactRepositories, localRepo, artifact, "javadoc",
                    javadocsFileSet);
        }

    }

    defineFilesetReference(filesetId, dependencyFileSet);

    defineFilesetReference(sourcesFilesetId, sourcesFileSet);

    defineFilesetReference(javadocFilesetId, javadocsFileSet);

    if (pathId != null) {
        getProject().addReference(pathId, dependencyPath);
    }

    if (versionsId != null) {
        String versionsValue = StringUtils.join(versions.iterator(), File.pathSeparator);
        getProject().setNewProperty(versionsId, versionsValue);
    }
}

From source file:fr.imag.adele.cadse.platform.DependenciesTask.java

License:Apache License

private void resolveSource(ArtifactFactory artifactFactory, ArtifactResolver resolver,
        List remoteArtifactRepositories, ArtifactRepository localRepo, Artifact artifact, String classifier,
        FileSet sourcesFileSet) {//from  w w w . j a v  a  2 s  .c  o m
    Artifact sourceArtifact = artifactFactory.createArtifactWithClassifier(artifact.getGroupId(),
            artifact.getArtifactId(), artifact.getVersion(), "java-source", classifier);
    try {
        resolver.resolve(sourceArtifact, remoteArtifactRepositories, localRepo);

        addArtifactToResult(localRepo, sourceArtifact, sourcesFileSet);
    } catch (ArtifactResolutionException e) {
        throw new BuildException("Unable to resolve artifact: " + e.getMessage(), e);
    } catch (ArtifactNotFoundException e) {
        // no source available: no problem, it's optional
    }
}

From source file:org.apache.myfaces.trinidadbuild.plugin.jdeveloper.IdeUtils.java

License:Apache License

/**
 * Wrapper around {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 *
 * @param artifactResolver see {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 * @param artifact see {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 * @param remoteRepos see {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 * @param localRepository see {@link ArtifactResolver#resolve(Artifact, List, ArtifactRepository)}
 * @param log Logger/*from ww  w.  j a v a2  s  .c om*/
 * @return the artifact, resolved if possible.
 */
public static Artifact resolveArtifact(ArtifactResolver artifactResolver, Artifact artifact, List remoteRepos,
        ArtifactRepository localRepository, Log log)

{
    try {
        artifactResolver.resolve(artifact, remoteRepos, localRepository);
    } catch (ArtifactNotFoundException e) {
        // ignore, the jar has not been found
    } catch (ArtifactResolutionException e) {
        String message = Messages.getString("IdeUtils.errorresolving",
                new Object[] { artifact.getClassifier(), artifact.getId(), e.getMessage() });

        log.warn(message);
    }

    return artifact;
}

From source file:org.apache.sling.maven.slingstart.ModelUtils.java

License:Apache License

/**
 * Get a resolved Artifact from the coordinates provided
 *
 * @return the artifact, which has been resolved.
 * @throws MojoExecutionException/* w  ww.  j  a v a2 s .c om*/
 */
public static Artifact getArtifact(final MavenProject project, final MavenSession session,
        final ArtifactHandlerManager artifactHandlerManager, final ArtifactResolver resolver,
        final String groupId, final String artifactId, final String version, final String type,
        final String classifier) throws MojoExecutionException {
    final Set<Artifact> artifacts = project.getDependencyArtifacts();
    for (final Artifact artifact : artifacts) {
        if (artifact.getGroupId().equals(groupId) && artifact.getArtifactId().equals(artifactId)
                && artifact.getVersion().equals(version) && artifact.getType().equals(type)
                && ((classifier == null && artifact.getClassifier() == null)
                        || (classifier != null && classifier.equals(artifact.getClassifier())))) {
            return artifact;
        }
    }
    final Artifact prjArtifact = new DefaultArtifact(groupId, artifactId,
            VersionRange.createFromVersion(version), Artifact.SCOPE_PROVIDED, type, classifier,
            artifactHandlerManager.getArtifactHandler(type));
    try {
        resolver.resolve(prjArtifact, project.getRemoteArtifactRepositories(), session.getLocalRepository());
    } catch (final ArtifactResolutionException e) {
        throw new MojoExecutionException(
                "Unable to get artifact for " + groupId + ":" + artifactId + ":" + version, e);
    } catch (final ArtifactNotFoundException e) {
        throw new MojoExecutionException(
                "Unable to get artifact for " + groupId + ":" + artifactId + ":" + version, e);
    }
    return prjArtifact;
}

From source file:org.codehaus.cargo.maven2.configuration.ArtifactInstaller.java

License:Apache License

/**
 * Resolves the dependency and return the artifact file.
 * @param artifactFactory The artifact factory is used to create valid Maven {@link Artifact}
 * objects./*  ww w.j a  v a 2  s  .  c om*/
 * @param artifactResolver The artifact resolver is used to dynamically resolve
 * {@link Artifact} objects. It will automatically download whatever needed.
 * @param localRepository The local Maven repository. This is used by the artifact resolver to
 * download resolved artifacts and put them in the local repository so that they won't have to
 * be fetched again next time the plugin is executed.
 * @param repositories The remote Maven repositories used by the artifact resolver to look for
 * artifacts.
 * @return Resolved dependency.
 * @throws ArtifactResolutionException If artifact resolution fails.
 * @throws ArtifactNotFoundException If artifact not found.
 */
public File resolve(ArtifactFactory artifactFactory, ArtifactResolver artifactResolver,
        ArtifactRepository localRepository, List<ArtifactRepository> repositories)
        throws ArtifactResolutionException, ArtifactNotFoundException {
    Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type,
            classifier);
    artifactResolver.resolve(artifact, repositories, localRepository);
    return artifact.getFile();
}

From source file:org.codehaus.mojo.animal_sniffer.enforcer.CheckSignatureRule.java

License:Open Source License

public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
    try {//from w ww .j av a 2  s  .  co m
        List classpathElements = (List) helper.evaluate("${project.compileClasspathElements}");

        File outputDirectory = new File((String) helper.evaluate("${project.build.outputDirectory}"));

        ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class);

        MavenProject project = (MavenProject) helper.evaluate("${project}");

        ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}");

        ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class);

        helper.getLog().info("Checking unresolved references to " + signature);

        org.apache.maven.artifact.Artifact a = signature.createArtifact(artifactFactory);

        resolver.resolve(a, project.getRemoteArtifactRepositories(), localRepository);
        // just check code from this module

        final Set ignoredPackages = buildPackageList(outputDirectory, classpathElements);

        if (ignores != null) {
            for (int i = 0; i < ignores.length; i++) {
                String ignore = ignores[i];
                if (ignore == null) {
                    continue;
                }
                ignoredPackages.add(ignore.replace('.', '/'));
            }
        }

        final SignatureChecker signatureChecker = new SignatureChecker(new FileInputStream(a.getFile()),
                ignoredPackages, new MavenLogger(helper.getLog()));
        signatureChecker.process(outputDirectory);

        if (signatureChecker.isSignatureBroken()) {
            throw new EnforcerRuleException(
                    "Signature errors found. Verify them and put @IgnoreJRERequirement on them.");
        }
    } catch (IOException e) {
        throw new EnforcerRuleException("Failed to check signatures", e);
    } catch (AbstractArtifactResolutionException e) {
        throw new EnforcerRuleException("Failed to obtain signature: " + signature, e);
    } catch (ComponentLookupException e) {
        throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e);
    } catch (ExpressionEvaluationException e) {
        throw new EnforcerRuleException("Unable to lookup an expression " + e.getLocalizedMessage(), e);
    }
}

From source file:org.codehaus.mojo.nbm.AbstractNbmMojo.java

License:Apache License

protected final ArtifactResult turnJarToNbmFile(Artifact art, ArtifactFactory artifactFactory,
        ArtifactResolver artifactResolver, MavenProject project, ArtifactRepository localRepository)
        throws MojoExecutionException {
    if ("jar".equals(art.getType()) || "nbm".equals(art.getType())) {
        //TODO, it would be nice to have a check to see if the
        // "to-be-created" module nbm artifact is actually already in the
        // list of dependencies (as "nbm-file") or not..
        // that would be a timesaver
        ExamineManifest mnf = new ExamineManifest(getLog());
        File jar = art.getFile();
        if (!jar.isFile()) {
            //MNBMODULE-210 with recent CoS changes in netbeans (7.4) jar will be file as we link open projects in the build
            // via WorkspaceReader. That's fine here, as all we need is to know if project is osgi or nbm module.
            // the nbm file has to be in local repository though.
            String path = localRepository.pathOf(art);
            File jar2 = new File(localRepository.getBasedir(), path.replace("/", File.separator));
            File manifest = new File(jar, "META-INF/MANIFEST.MF");

            if (!jar2.isFile() || !manifest.isFile()) {
                getLog().warn("MNBMODULE-131: need to at least run install phase on " + jar2);
                return new ArtifactResult(null, null);
            }/*w w  w. jav a 2  s  .  c om*/
            mnf.setManifestFile(manifest);
        } else {
            mnf.setJarFile(jar);
        }
        mnf.checkFile();
        if (mnf.isNetBeansModule()) {
            Artifact nbmArt = artifactFactory.createDependencyArtifact(art.getGroupId(), art.getArtifactId(),
                    art.getVersionRange(), "nbm-file", art.getClassifier(), art.getScope());
            try {
                artifactResolver.resolve(nbmArt, project.getRemoteArtifactRepositories(), localRepository);
            }

            catch (ArtifactResolutionException ex) {
                //shall be check before actually resolving from repos?
                checkReactor(art, nbmArt);
                if (!nbmArt.isResolved()) {
                    throw new MojoExecutionException("Failed to retrieve the nbm file from repository", ex);
                }
            } catch (ArtifactNotFoundException ex) {
                //shall be check before actually resolving from repos?
                checkReactor(art, nbmArt);
                if (!nbmArt.isResolved()) {
                    throw new MojoExecutionException("Failed to retrieve the nbm file from repository", ex);
                }
            }
            return new ArtifactResult(nbmArt, mnf);
        }
        if (mnf.isOsgiBundle()) {
            return new ArtifactResult(null, mnf);
        }
    }
    return new ArtifactResult(null, null);
}