Example usage for org.apache.maven.artifact.factory ArtifactFactory createDependencyArtifact

List of usage examples for org.apache.maven.artifact.factory ArtifactFactory createDependencyArtifact

Introduction

In this page you can find the example usage for org.apache.maven.artifact.factory ArtifactFactory createDependencyArtifact.

Prototype

Artifact createDependencyArtifact(String groupId, String artifactId, VersionRange versionRange, String type,
            String classifier, String scope);

Source Link

Usage

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.//from  w w w .  j  a va2  s.  co  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: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.j ava2 s .c o  m
            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);
}

From source file:org.codehaus.mojo.pluginsupport.dependency.DependencyHelper.java

License:Apache License

public static Map getManagedVersionMap(final MavenProject project, final ArtifactFactory factory)
        throws ProjectBuildingException {
    assert project != null;
    assert factory != null;

    DependencyManagement dependencyManagement = project.getDependencyManagement();
    Map managedVersionMap;// w  w w . j ava 2  s  .  c  om

    if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
        managedVersionMap = new HashMap();
        Iterator iter = dependencyManagement.getDependencies().iterator();

        while (iter.hasNext()) {
            Dependency d = (Dependency) iter.next();

            try {
                VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion());
                Artifact artifact = factory.createDependencyArtifact(d.getGroupId(), d.getArtifactId(),
                        versionRange, d.getType(), d.getClassifier(), d.getScope());
                managedVersionMap.put(d.getManagementKey(), artifact);
            } catch (InvalidVersionSpecificationException e) {
                throw new ProjectBuildingException(project.getId(), "Unable to parse version '" + d.getVersion()
                        + "' for dependency '" + d.getManagementKey() + "': " + e.getMessage(), e);
            }
        }
    } else {
        managedVersionMap = Collections.EMPTY_MAP;
    }

    return managedVersionMap;
}

From source file:org.universAAL.maven.treebuilder.DependencyTreeBuilder.java

License:Apache License

/**
 * Method builds dependency tree for a list of maven projects. All artifacts
 * in the tree are crosschecked against duplications and conflicts. In each
 * case of duplication, conflict, the case is resolved by omitting artifact
 * which is lower in the tree and keeping artifact which is higher in the
 * tree. If artifacts are on the same level then the one occuring first in
 * the tree is kept.//from  www.j a  v a 2  s.com
 * 
 * @param repository
 *            Maven repository.
 * @param factory
 *            Factory used for creating artifacts.
 * @param metadataSource
 *            ArtifactMetadataSource provided by maven.
 * @param projectDescs
 *            list of maven project descriptors. Each descriptor contains
 *            MavenProject, a list of project's remote repositories and a
 *            boolean indicator if the project needs to be resolved
 *            transitively or not.
 * @return a dependency tree as a list of rootnodes (instances of
 *         DependencyNode class) which contain their own subtrees. Each
 *         rootnode corresponds to one maven project provided as argument.
 *         The order of rootnodes list is the same as order of provided
 *         maven projects list.
 * @throws DependencyTreeBuilderException
 *             Notifies about a problem during building the dependency tree.
 * @throws ArtifactMetadataRetrievalException
 *             Signals problem with metadata retieval.
 * @throws InvalidVersionSpecificationException
 *             Informs about invalid version specifications.
 * @throws NoSuchFieldException
 *             Exception related to reflection.
 * @throws SecurityException
 *             Exception thrown by security manager.
 * @throws IllegalAccessException
 *             Illegal access during usage of java reflection.
 * @throws IllegalArgumentException
 *             Illegal argument was passed.
 */
public List<RootNode> buildDependencyTree(final ArtifactRepository repository, final ArtifactFactory factory,
        final ArtifactMetadataSource metadataSource, final MavenProjectDescriptor... projectDescs)
        throws DependencyTreeBuilderException, ArtifactMetadataRetrievalException,
        InvalidVersionSpecificationException, SecurityException, NoSuchFieldException, IllegalArgumentException,
        IllegalAccessException {
    ArtifactFilter filter = new ScopeArtifactFilter();
    DependencyTreeResolutionListener listener = new DependencyTreeResolutionListener(filter);
    Map resolvedArtifacts = new LinkedHashMap();
    for (MavenProjectDescriptor projectDesc : projectDescs) {
        MavenProject project = projectDesc.project;
        try {
            List<String> separatedGroupId = extractSeparatedGroupIds(project);

            // Here you can simply add to create a new list
            List remoteRepositories = projectDesc.remoteRepositories;

            // If artifact is marked in pom as a bundle then it is changed
            // to jar. Retaining bundle can impose problems when the
            // artifact is duplicated or conflicted with other artifact
            // specified as a dependency, because in the
            // dependency there is only jar type specified.
            Artifact originatingArtifact = project.getArtifact();
            this.stringifiedRoot = FilteringVisitorSupport.stringify(originatingArtifact);
            if ("bundle".equals(originatingArtifact.getType())) {
                Artifact changeArtifact = artifactFactory.createArtifact(originatingArtifact.getGroupId(),
                        originatingArtifact.getArtifactId(), originatingArtifact.getVersion(),
                        originatingArtifact.getScope(), "jar");
                originatingArtifact = changeArtifact;
            }
            ResolutionNode root = new ResolutionNode(originatingArtifact, remoteRepositories);

            // If the project is not supposed to be transitively resolved
            // then its dependencies are not added to the root. Moreover the
            // parameter is passed to the recurse method. Thanks to than
            // when transitive is false, resolving of runtime dependencies
            // is not performed.
            if (projectDesc.transitive) {
                Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();

                if (dependencyArtifacts == null) {
                    dependencyArtifacts = new LinkedHashSet();
                    List dependencies = project.getDependencies();
                    for (Object depObj : dependencies) {
                        Dependency dep = (Dependency) depObj;
                        if (dep.isOptional()) {
                            // filtering optional dependencies
                            continue;
                        }
                        Artifact dependencyArtifact;
                        VersionRange versionRange = VersionRange.createFromVersionSpec(dep.getVersion());
                        dependencyArtifact = factory.createDependencyArtifact(dep.getGroupId(),
                                dep.getArtifactId(), versionRange, dep.getType(), dep.getClassifier(),
                                dep.getScope());
                        if (dep.getExclusions() != null) {
                            if (!dep.getExclusions().isEmpty()) {
                                List<String> patterns = new ArrayList<String>();
                                for (Exclusion exclusion : dep.getExclusions()) {
                                    patterns.add(exclusion.getGroupId() + ":" + exclusion.getArtifactId());
                                }
                                dependencyArtifact.setDependencyFilter(new ExcludesArtifactFilter(patterns));
                            }
                        }
                        dependencyArtifacts.add(dependencyArtifact);
                    }
                } else {
                    // filtering optional dependencies
                    Set<Artifact> filteredArtifacts = new LinkedHashSet();
                    for (Artifact a : dependencyArtifacts) {
                        if (!a.isOptional()) {
                            filteredArtifacts.add(a);
                        }
                    }
                    dependencyArtifacts = filteredArtifacts;
                }

                for (Artifact depArtifact : dependencyArtifacts) {
                    if (depArtifact.getVersion() != null) {
                        if (!depArtifact.getVersion().equals(depArtifact.getBaseVersion())) {
                            if (depArtifact.isSnapshot()) {
                                depArtifact.setVersion(depArtifact.getBaseVersion());
                            }
                        }
                    }
                }

                root.addDependencies(dependencyArtifacts, remoteRepositories, filter);
            }

            // Information about managed versions is extracted from the
            // artifact's pom (but also from parent poms and settings.xml
            // file).
            ManagedVersionMap versionMap = getManagedVersionsMap(originatingArtifact,
                    project.getManagedVersionMap());

            recurse(originatingArtifact, root, resolvedArtifacts, versionMap, localRepository,
                    remoteRepositories, metadataSource, filter, listener, projectDesc.transitive,
                    new HashSet<String>(separatedGroupId));
        } catch (ArtifactResolutionException exception) {
            throw new DependencyTreeBuilderException("Cannot build project dependency tree", exception);
        }
    }
    return listener.getRootNodes();
}