Example usage for org.apache.maven.project.artifact MavenMetadataSource createArtifacts

List of usage examples for org.apache.maven.project.artifact MavenMetadataSource createArtifacts

Introduction

In this page you can find the example usage for org.apache.maven.project.artifact MavenMetadataSource createArtifacts.

Prototype

@Deprecated
    public static Set<Artifact> createArtifacts(ArtifactFactory artifactFactory, List<Dependency> dependencies,
            String inheritedScope, ArtifactFilter dependencyFilter, MavenProject project)
            throws InvalidDependencyVersionException 

Source Link

Usage

From source file:com.baidu.jprotobuf.mojo.PreCompileMojo.java

License:Apache License

/**
 * Resolve the executable dependencies for the specified project
 * /*from ww w  .  j  a  v  a2s . c  o  m*/
 * @param executablePomArtifact the project's POM
 * @return a set of Artifacts
 * @throws MojoExecutionException if a failure happens
 */
private Set<Artifact> resolveExecutableDependencies(Artifact executablePomArtifact)
        throws MojoExecutionException {

    Set<Artifact> executableDependencies;
    try {
        MavenProject executableProject = this.projectBuilder.buildFromRepository(executablePomArtifact,
                this.remoteRepositories, this.localRepository);

        // get all of the dependencies for the executable project
        List<Dependency> dependencies = executableProject.getDependencies();

        // make Artifacts of all the dependencies
        Set<Artifact> dependencyArtifacts = MavenMetadataSource.createArtifacts(this.artifactFactory,
                dependencies, null, null, null);

        // not forgetting the Artifact of the project itself
        dependencyArtifacts.add(executableProject.getArtifact());

        // resolve all dependencies transitively to obtain a comprehensive list of assemblies
        ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts,
                executablePomArtifact, Collections.emptyMap(), this.localRepository, this.remoteRepositories,
                metadataSource, null, Collections.emptyList());
        executableDependencies = result.getArtifacts();
    } catch (Exception ex) {
        throw new MojoExecutionException("Encountered problems resolving dependencies of the executable "
                + "in preparation for its execution.", ex);
    }

    return executableDependencies;
}

From source file:com.googlecode.japi.checker.maven.plugin.RuntimeDependencyResolver.java

License:Apache License

/**
 * Download (if necessary) a pom, and load it as a MavenProject, transitively resolving any
 * dependencies therein./*  w  w  w.j  a v  a  2 s . c  om*/
 * 
 * @param projectBuilder
 * @param groupId
 * @param artifactId
 * @param versionId
 * @return a Set of Artifacts representing the transitively resolved dependencies.
 * @throws MalformedURLException
 * @throws ProjectBuildingException
 * @throws InvalidDependencyVersionException
 * @throws ArtifactResolutionException
 * @throws ArtifactNotFoundException
 */
public Set<Artifact> transitivelyResolvePomDependencies(MavenProjectBuilder projectBuilder, String groupId,
        String artifactId, String versionId, boolean resolveProjectArtifact)
        throws MalformedURLException, ProjectBuildingException, InvalidDependencyVersionException,
        ArtifactResolutionException, ArtifactNotFoundException {

    Artifact pomArtifact = getPomArtifact(groupId, artifactId, versionId);
    MavenProject project = loadPomAsProject(projectBuilder, pomArtifact);
    @SuppressWarnings("rawtypes")
    List dependencies = project.getDependencies();

    @SuppressWarnings("unchecked")
    Set<Artifact> dependencyArtifacts = MavenMetadataSource.createArtifacts(artifactFactory, dependencies, null,
            null, null);
    dependencyArtifacts.add(project.getArtifact());

    @SuppressWarnings("rawtypes")
    List listeners = Collections.EMPTY_LIST;

    ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts, pomArtifact,
            Collections.EMPTY_MAP, localRepository, remoteRepositories, metadataSource, null, listeners);

    @SuppressWarnings("unchecked")
    Set<Artifact> artifacts = result.getArtifacts();

    LOGGER.fine("RESOLVED " + artifacts.size() + " ARTIFACTS");
    Iterator<Artifact> itor = artifacts.iterator();
    while (itor.hasNext()) {
        Artifact a = (Artifact) itor.next();
        LOGGER.fine(a.getFile().toURI().toURL().toString());
    }
    return artifacts;
}

From source file:com.ning.maven.plugins.dependencyversionscheck.AbstractDependencyVersionsMojo.java

License:Apache License

/**
 * Returns a Set of artifacts based off the given project. Artifacts can be filtered and optional dependencies can be excluded.
 *
 * It would be awesome if this method would also use the DependencyTreeBuilder which seems to yield better results (and is much closer to the actual compile tree in some cases)
 * than the artifactResolver. However, due to MNG-3236 the artifact filter is not applied when resolving dependencies and this method relies on the artifact filter to get
 * the scoping right. Well, maybe in maven 3.0 this will be better. Or different. Whatever comes first.
 *//*from www.  j  a v  a  2 s  .  co  m*/
private Set resolveDependenciesInItsOwnScope(final MavenProject project, final ArtifactFilter filter,
        final boolean includeOptional)
        throws InvalidDependencyVersionException, ArtifactResolutionException, ArtifactNotFoundException {
    Set dependencyArtifacts = MavenMetadataSource.createArtifacts(artifactFactory, project.getDependencies(),
            null, filter, null);

    ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts,
            project.getArtifact(), Collections.EMPTY_MAP, localRepository,
            project.getRemoteArtifactRepositories(), artifactMetadataSource,
            new ArtifactOptionalFilter(includeOptional));

    return result.getArtifacts();
}

From source file:com.taobao.hsf.jetty.RuntimeDependencyResolver.java

License:Apache License

/**
 * Download (if necessary) a pom, and load it as a MavenProject, transitively resolving any
 * dependencies therein./* w  w  w  .  j a v a2s . c  o m*/
 * 
 * @param projectBuilder
 * @param groupId
 * @param artifactId
 * @param versionId
 * @return a Set of Artifacts representing the transitively resolved dependencies.
 * 
 * @throws MalformedURLException
 * @throws ProjectBuildingException
 * @throws InvalidDependencyVersionException
 * @throws ArtifactResolutionException
 * @throws ArtifactNotFoundException
 */
public Set transitivelyResolvePomDependencies(MavenProjectBuilder projectBuilder, String groupId,
        String artifactId, String versionId, boolean resolveProjectArtifact)
        throws MalformedURLException, ProjectBuildingException, InvalidDependencyVersionException,
        ArtifactResolutionException, ArtifactNotFoundException {

    Artifact pomArtifact = getPomArtifact(groupId, artifactId, versionId);
    MavenProject project = loadPomAsProject(projectBuilder, pomArtifact);
    List dependencies = project.getDependencies();

    Set dependencyArtifacts = MavenMetadataSource.createArtifacts(artifactFactory, dependencies, null, null,
            null);
    dependencyArtifacts.add(project.getArtifact());

    List listeners = Collections.EMPTY_LIST;

    if (PluginLog.getLog().isDebugEnabled()) {
        listeners = new ArrayList();
        listeners.add(new RuntimeResolutionListener());
    }

    ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts, pomArtifact,
            Collections.EMPTY_MAP, localRepository, remoteRepositories, metadataSource, null, listeners);

    Set artifacts = result.getArtifacts();

    if (PluginLog.getLog().isDebugEnabled()) {
        PluginLog.getLog().debug("RESOLVED " + artifacts.size() + " ARTIFACTS");
        Iterator itor = artifacts.iterator();
        while (itor.hasNext()) {
            Artifact a = (Artifact) itor.next();
            PluginLog.getLog().debug(a.getFile().toURL().toString());
        }
    }
    return artifacts;
}

From source file:de.jiac.micro.mojo.ConfiguratorMojo.java

License:Open Source License

private Set transitivelyResolvePomDependencies(String groupId, String artifactId, String version)
        throws ProjectBuildingException, InvalidDependencyVersionException, ArtifactResolutionException,
        ArtifactNotFoundException {//from  ww  w .j  a v a 2 s. c o m
    //get the pom as an Artifact 
    Artifact pomArtifact = artifactFactory.createPluginArtifact(groupId, artifactId,
            VersionRange.createFromVersion(version));

    //load the pom as a MavenProject
    MavenProject tempProject = mavenProjectBuilder.buildFromRepository(pomArtifact, remoteRepositories,
            localRepository);

    //get all of the dependencies for the project
    List dependencies = tempProject.getDependencies();

    //make Artifacts of all the dependencies
    Set dependencyArtifacts = MavenMetadataSource.createArtifacts(artifactFactory, dependencies, null, null,
            null);

    //not forgetting the Artifact of the project itself
    dependencyArtifacts.add(tempProject.getArtifact());

    //resolve all dependencies transitively to obtain a comprehensive list of jars        
    ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts, pomArtifact,
            Collections.EMPTY_MAP, localRepository, remoteRepositories, metadataSource, null,
            Collections.EMPTY_LIST);

    return result.getArtifacts();
}

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

License:Apache License

/**
 * Main task execution.  Called by parent execute().
 *//*  w  w  w .j a va  2 s. c  om*/
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:me.springframework.di.maven.AbstractGeneratorMojo.java

License:Open Source License

/**
 * Produces the {@link JavaDocBuilder} used to analyze classes and source files.
 * //from  w  w  w  .  ja v a2s.  c o  m
 * @return The {@link JavaDocBuilder} used to analyze classes and source files.
 * @throws MojoExecutionException If we fail to produce the {@link JavaDocBuilder}. (Fail
 *             early.)
 */
@SuppressWarnings("unchecked")
private JavaDocBuilder createJavaDocBuilder() throws MojoExecutionException {
    JavaDocBuilder builder = new JavaDocBuilder();
    builder.addSourceTree(new File(project.getBuild().getSourceDirectory()));
    // Quick and dirty hack for adding Classloaders with the definitions of
    // classes the sources depend upon.
    try {
        Set<Artifact> artifacts = MavenMetadataSource.createArtifacts(artifactFactory,
                project.getDependencies(), Artifact.SCOPE_RUNTIME, new ArtifactFilter() {

                    public boolean include(Artifact artifact) {
                        return true;
                    }

                }, project);
        List<URL> urls = new ArrayList<URL>();
        for (Artifact artifact : artifacts) {
            if (getLog().isDebugEnabled()) {
                getLog().debug("Adding artifact " + artifact.getId());
            }
            if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
                urls.add(artifact.getFile().toURL());
            } else {
                urls.add(new File(localRepository.getBasedir(), localRepository.pathOf(artifact)).toURL());
            }
        }
        URL[] template = new URL[0];
        URLClassLoader loader = new URLClassLoader(urls.toArray(template));
        builder.getClassLibrary().addClassLoader(loader);
    } catch (InvalidDependencyVersionException idve) {
        idve.printStackTrace();
    } catch (MalformedURLException e) {
        throw new MojoExecutionException("Malformed artifact URLs.");
    }
    return builder;
}

From source file:net.hasor.maven.ExecJavaMojo.java

License:Apache License

/**
 * Resolve the executable dependencies for the specified project
 * /*from www .jav a2  s .c o  m*/
 * @param executablePomArtifact the project's POM
 * @return a set of Artifacts
 * @throws MojoExecutionException if a failure happens
 */
private Set<Artifact> resolveExecutableDependencies(Artifact executablePomArtifact)
        throws MojoExecutionException {
    Set<Artifact> executableDependencies;
    try {
        MavenProject executableProject = this.projectBuilder.buildFromRepository(executablePomArtifact,
                this.remoteRepositories, this.localRepository);
        // get all of the dependencies for the executable project
        List<Dependency> dependencies = executableProject.getDependencies();
        // make Artifacts of all the dependencies
        Set<Artifact> dependencyArtifacts = MavenMetadataSource.createArtifacts(this.artifactFactory,
                dependencies, null, null, null);
        // not forgetting the Artifact of the project itself
        dependencyArtifacts.add(executableProject.getArtifact());
        // resolve all dependencies transitively to obtain a comprehensive list of assemblies
        ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts,
                executablePomArtifact, Collections.emptyMap(), this.localRepository, this.remoteRepositories,
                metadataSource, null, Collections.emptyList());
        executableDependencies = result.getArtifacts();
    } catch (Exception ex) {
        throw new MojoExecutionException("Encountered problems resolving dependencies of the executable "
                + "in preparation for its execution.", ex);
    }
    return executableDependencies;
}

From source file:org.apache.camel.guice.maven.RunMojo.java

License:Apache License

private Set<Artifact> resolveExecutableDependencies(Artifact executablePomArtifact)
        throws MojoExecutionException {

    Set<Artifact> executableDependencies;
    try {//from  ww w .  j  av  a2 s . co  m
        MavenProject executableProject = this.projectBuilder.buildFromRepository(executablePomArtifact,
                this.remoteRepositories, this.localRepository);

        // get all of the dependencies for the executable project
        List<Artifact> dependencies = CastUtils.cast(executableProject.getDependencies());

        // make Artifacts of all the dependencies
        Set<Artifact> dependencyArtifacts = CastUtils.cast(
                MavenMetadataSource.createArtifacts(this.artifactFactory, dependencies, null, null, null));

        // not forgetting the Artifact of the project itself
        dependencyArtifacts.add(executableProject.getArtifact());

        // resolve all dependencies transitively to obtain a comprehensive
        // list of assemblies
        ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts,
                executablePomArtifact, Collections.emptyMap(), this.localRepository, this.remoteRepositories,
                metadataSource, null, Collections.emptyList());
        executableDependencies = CastUtils.cast(result.getArtifacts());

    } catch (Exception ex) {
        throw new MojoExecutionException("Encountered problems resolving dependencies of the executable "
                + "in preparation for its execution.", ex);
    }

    return executableDependencies;
}

From source file:org.codehaus.mojo.fitnesse.FitnesseRunnerMojo.java

License:Open Source License

/**
 * Create the transitive classpath./*from w  w  w.j  ava2 s . c o m*/
 * 
 * @return The dependent artifacts.
 * @throws MojoExecutionException If the classpath can't be found.
 */
public Set transitivelyResolvePomDependencies() throws MojoExecutionException {
    // make Artifacts of all the dependencies
    Set dependencyArtifacts;
    try {
        dependencyArtifacts = MavenMetadataSource.createArtifacts(artifactFactory, dependencies, null, null,
                null);
    } catch (InvalidDependencyVersionException e) {
        throw new MojoExecutionException("Invalid dependency", e);
    }

    // not forgetting the Artifact of the project itself
    dependencyArtifacts.add(project.getArtifact());

    List listeners = Collections.EMPTY_LIST;

    // resolve all dependencies transitively to obtain a comprehensive list
    // of jars
    ArtifactResolutionResult result;
    try {
        result = artifactResolver.resolveTransitively(dependencyArtifacts, project.getArtifact(),
                Collections.EMPTY_MAP, localRepository, remoteRepositories, metadataSource, null, listeners);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve Artifact.", e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Unable to resolve Artifact.", e);
    }

    return result.getArtifacts();
}