Example usage for org.apache.maven.artifact Artifact getVersionRange

List of usage examples for org.apache.maven.artifact Artifact getVersionRange

Introduction

In this page you can find the example usage for org.apache.maven.artifact Artifact getVersionRange.

Prototype

VersionRange getVersionRange();

Source Link

Usage

From source file:com.adviser.maven.GraphArtifactCollector.java

License:Apache License

private ArtifactVersion getArtifactVersion(ArtifactRepository localRepository, List remoteRepositories,
        ArtifactMetadataSource source, Artifact artifact)
        throws OverConstrainedVersionException, ArtifactMetadataRetrievalException {
    ArtifactVersion version;/* ww w . j  a v a  2s  . co m*/
    if (!artifact.isSelectedVersionKnown()) {
        List versions = artifact.getAvailableVersions();
        if (versions == null) {
            versions = source.retrieveAvailableVersions(artifact, localRepository, remoteRepositories);
            artifact.setAvailableVersions(versions);
        }

        VersionRange versionRange = artifact.getVersionRange();

        version = versionRange.matchVersion(versions);

        if (version == null) {
            if (versions.isEmpty()) {
                throw new OverConstrainedVersionException(
                        "No versions are present in the repository for the artifact with a range "
                                + versionRange,
                        artifact, remoteRepositories);
            } else {
                throw new OverConstrainedVersionException(
                        "Couldn't find a version in " + versions + " to match range " + versionRange, artifact,
                        remoteRepositories);
            }
        }
    } else {
        version = artifact.getSelectedVersion();
    }
    return version;
}

From source file:com.adviser.maven.GraphArtifactCollector.java

License:Apache License

private ResolutionNode checkPreviousNodes(ResolutionNode node, List listeners, List previousNodes)
        throws OverConstrainedVersionException {
    for (Iterator i = previousNodes.iterator(); i.hasNext();) {
        ResolutionNode previous = (ResolutionNode) i.next();
        if (previous.isActive()) {
            // Version mediation
            VersionRange previousRange = previous.getArtifact().getVersionRange();
            VersionRange currentRange = node.getArtifact().getVersionRange();
            // TODO: why do we force the version on it? what if they
            // don't match?
            if (previousRange == null) {
                // version was already resolved
                node.getArtifact().setVersion(previous.getArtifact().getVersion());
            } else if (currentRange == null) {
                // version was already resolved
                previous.getArtifact().setVersion(node.getArtifact().getVersion());
            } else {
                // TODO: shouldn't need to double up on this work, only
                // done for simplicity of handling recommended
                // version but the restriction is identical
                VersionRange newRange = previousRange.restrict(currentRange);
                // TODO: ick. this forces the OCE that should have come
                // from the previous call. It is still correct
                if (newRange.isSelectedVersionKnown(previous.getArtifact())) {
                    fireEvent(ResolutionListener.RESTRICT_RANGE, listeners, node, previous.getArtifact(),
                            newRange);//from   w ww  .ja  va 2s  . c  o  m
                }
                previous.getArtifact().setVersionRange(newRange);
                node.getArtifact().setVersionRange(currentRange.restrict(previousRange));

                // Select an appropriate available version from the (now
                // restricted) range
                // Note this version was selected before to get the
                // appropriate POM
                // But it was reset by the call to setVersionRange on
                // restricting the version
                ResolutionNode[] resetNodes = { previous, node };
                for (int j = 0; j < 2; j++) {
                    Artifact resetArtifact = resetNodes[j].getArtifact();
                    if (resetArtifact.getVersion() == null && resetArtifact.getVersionRange() != null
                            && resetArtifact.getAvailableVersions() != null) {

                        resetArtifact.selectVersion(resetArtifact.getVersionRange()
                                .matchVersion(resetArtifact.getAvailableVersions()).toString());
                        fireEvent(ResolutionListener.SELECT_VERSION_FROM_RANGE, listeners, resetNodes[j]);
                    }
                }
            }

            // Conflict Resolution
            // TODO: use as conflict resolver(s), chain

            // TODO: should this be part of mediation?
            // previous one is more dominant
            if (previous.getDepth() <= node.getDepth()) {
                checkScopeUpdate(node, previous, listeners);
            } else {
                checkScopeUpdate(previous, node, listeners);
            }

            if (previous.getDepth() <= node.getDepth()) {
                // previous was nearer
                fireEvent(ResolutionListener.OMIT_FOR_NEARER, listeners, node, previous.getArtifact());
                node.disable();
                node = previous;
            } else {
                fireEvent(ResolutionListener.OMIT_FOR_NEARER, listeners, previous, node.getArtifact());
                previous.disable();
            }
        }
    }
    return node;
}

From source file:com.adviser.maven.GraphArtifactCollector.java

License:Apache License

private void fireEvent(int event, List listeners, ResolutionNode node, Artifact replacement,
        VersionRange newRange) {//from   ww  w  .j a  va2 s .  c  om
    for (Iterator i = listeners.iterator(); i.hasNext();) {
        ResolutionListener listener = (ResolutionListener) i.next();

        switch (event) {
        case ResolutionListener.TEST_ARTIFACT:
            listener.testArtifact(node.getArtifact());
            break;
        case ResolutionListener.PROCESS_CHILDREN:
            listener.startProcessChildren(node.getArtifact());
            break;
        case ResolutionListener.FINISH_PROCESSING_CHILDREN:
            listener.endProcessChildren(node.getArtifact());
            break;
        case ResolutionListener.INCLUDE_ARTIFACT:
            listener.includeArtifact(node.getArtifact());
            break;
        case ResolutionListener.OMIT_FOR_NEARER:
            String version = node.getArtifact().getVersion();
            String replacementVersion = replacement.getVersion();
            if (version != null ? !version.equals(replacementVersion) : replacementVersion != null) {
                listener.omitForNearer(node.getArtifact(), replacement);
            }
            break;
        case ResolutionListener.OMIT_FOR_CYCLE:
            listener.omitForCycle(node.getArtifact());
            break;
        case ResolutionListener.UPDATE_SCOPE:
            listener.updateScope(node.getArtifact(), replacement.getScope());
            break;
        case ResolutionListener.UPDATE_SCOPE_CURRENT_POM:
            listener.updateScopeCurrentPom(node.getArtifact(), replacement.getScope());
            break;
        case ResolutionListener.MANAGE_ARTIFACT:
            listener.manageArtifact(node.getArtifact(), replacement);
            break;
        case ResolutionListener.SELECT_VERSION_FROM_RANGE:
            listener.selectVersionFromRange(node.getArtifact());
            break;
        case ResolutionListener.RESTRICT_RANGE:
            if (node.getArtifact().getVersionRange().hasRestrictions()
                    || replacement.getVersionRange().hasRestrictions()) {
                listener.restrictRange(node.getArtifact(), replacement, newRange);
            }
            break;
        default:
            throw new IllegalStateException("Unknown event: " + event);
        }
    }
}

From source file:com.github.batkinson.plugins.PackMojo.java

License:Apache License

@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException {

    getLog().debug("starting pack");

    Packer packer = Pack200.newPacker();
    Unpacker unpacker = null;//from   ww  w  .j  a  v  a2  s  .co m

    if (normalizeOnly)
        unpacker = Pack200.newUnpacker();

    String type = "jar.pack";
    if (compress)
        type = "jar.pack.gz";

    Map<String, String> packerProps = packer.properties();

    packerProps.put(Packer.EFFORT, effort);
    packerProps.put(Packer.SEGMENT_LIMIT, segmentLimit);
    packerProps.put(Packer.KEEP_FILE_ORDER, keepFileOrder);
    packerProps.put(Packer.MODIFICATION_TIME, modificationTime);
    packerProps.put(Packer.DEFLATE_HINT, deflateHint);

    if (stripCodeAttributes != null) {
        for (String attributeName : stripCodeAttributes) {
            getLog().debug("stripping " + attributeName);
            packerProps.put(Packer.CODE_ATTRIBUTE_PFX + attributeName, Packer.STRIP);
        }
    }

    if (failOnUnknownAttributes)
        packerProps.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);

    List<Artifact> artifactsToPack = new ArrayList<Artifact>();

    if (processMainArtifact)
        artifactsToPack.add(project.getArtifact());

    if (includeClassifiers != null && !includeClassifiers.isEmpty()) {
        for (Artifact secondaryArtifact : (List<Artifact>) project.getAttachedArtifacts())
            if ("jar".equals(secondaryArtifact.getType()) && secondaryArtifact.hasClassifier()
                    && includeClassifiers.contains(secondaryArtifact.getClassifier()))
                artifactsToPack.add(secondaryArtifact);
    }

    String action = normalizeOnly ? "normalizing" : "packing";

    getLog().info(action + " " + artifactsToPack.size() + " artifacts");

    for (Artifact artifactToPack : artifactsToPack) {

        File origFile = artifactToPack.getFile();
        File packFile = new File(origFile.getAbsolutePath() + ".pack");
        File zipFile = new File(packFile.getAbsolutePath() + ".gz");

        try {
            JarFile jar = new JarFile(origFile);
            FileOutputStream fos = new FileOutputStream(packFile);

            getLog().debug("packing " + origFile + " to " + packFile);
            packer.pack(jar, fos);

            getLog().debug("closing handles ...");
            jar.close();
            fos.close();

            if (normalizeOnly) {
                getLog().debug("unpacking " + packFile + " to " + origFile);
                JarOutputStream origJarStream = new JarOutputStream(new FileOutputStream(origFile));
                unpacker.unpack(packFile, origJarStream);

                getLog().debug("closing handles...");
                origJarStream.close();

                getLog().debug("unpacked file");
            } else {

                Artifact newArtifact = new DefaultArtifact(artifactToPack.getGroupId(),
                        artifactToPack.getArtifactId(), artifactToPack.getVersionRange(),
                        artifactToPack.getScope(), type, artifactToPack.getClassifier(),
                        new DefaultArtifactHandler(type));

                if (compress) {
                    getLog().debug("compressing " + packFile + " to " + zipFile);
                    GZIPOutputStream zipOut = new GZIPOutputStream(new FileOutputStream(zipFile));
                    IOUtil.copy(new FileInputStream(packFile), zipOut);
                    zipOut.close();
                    newArtifact.setFile(zipFile);
                } else {
                    newArtifact.setFile(packFile);
                }

                if (attachPackedArtifacts) {
                    getLog().debug("attaching " + newArtifact);
                    project.addAttachedArtifact(newArtifact);
                }
            }

            getLog().debug("finished " + action + " " + packFile);

        } catch (IOException e) {
            throw new MojoExecutionException("Failed to pack jar.", e);
        }
    }
}

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

License:Apache License

public AttachedNarArtifact(final Artifact parent, final String type, final String classifier) {
    super(parent.getGroupId(), parent.getArtifactId(), parent.getVersionRange(), parent.getScope(), type,
            classifier, null, parent.isOptional());
    setArtifactHandler(new Handler(classifier));
}

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

License:Apache License

public NarArtifact(final Artifact dependency, final NarInfo narInfo) {
    super(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersionRange(),
            dependency.getScope(), dependency.getType(), dependency.getClassifier(),
            dependency.getArtifactHandler(), dependency.isOptional());
    this.setFile(dependency.getFile());
    this.narInfo = narInfo;
}

From source file:com.github.mzachar.maven.plan.PlanMojo.java

License:Apache License

private Element createPlanXmlArtifact(Artifact dependenci) {
    PlanArtifactType type = null;//from ww  w .  j a  va2 s  .c  o  m
    if ("jar".equalsIgnoreCase(dependenci.getType())) {
        type = PlanArtifactType.BUNDLE;
    }
    if ("war".equalsIgnoreCase(dependenci.getType())) {
        type = PlanArtifactType.BUNDLE;
    }

    if ("par".equalsIgnoreCase(dependenci.getType())) {
        type = PlanArtifactType.PAR;
    }

    if ("plan".equalsIgnoreCase(dependenci.getType())) {
        type = PlanArtifactType.PLAN;
    }

    // we can't use this type in plan dependency
    if (type == null)
        return null;

    // try osgi manifest symbolic name and version
    Element bundleArtifact = createBundleArtifact(dependenci, type);
    if (bundleArtifact != null)
        return bundleArtifact;

    // fall back to maven groupId.artifactId and version
    String version = dependenci.getVersion();
    if (dependenci.getVersionRange() != null) {
        version = dependenci.getVersionRange().toString();
    }
    return createPlanXmlArtifact(type, getArtifactName(dependenci, type), version);
}

From source file:com.github.zhve.ideaplugin.ArtifactDependencyResolver.java

License:Apache License

private static Artifact toDependencyArtifact(ArtifactFactory artifactFactory, Artifact dependency,
        String inheritedScope) {// w w w .  j a  va  2s  .co m
    Artifact dependencyArtifact = artifactFactory.createDependencyArtifact(dependency.getGroupId(),
            dependency.getArtifactId(), dependency.getVersionRange(), dependency.getType(),
            dependency.getClassifier(), dependency.getScope(), inheritedScope, dependency.isOptional());
    if (dependencyArtifact != null)
        dependencyArtifact.setDependencyFilter(dependency.getDependencyFilter());
    return dependencyArtifact;
}

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

License:Apache License

/**
 * Return a version object for an Artifact.
 *///from w  w w .  ja  v  a2s  .com
private Version getVersion(Artifact artifact) throws OverConstrainedVersionException {
    Version version = null;

    if (artifact != null) {
        if ((artifact.getVersionRange() != null) && (artifact.getSelectedVersion() != null)) {
            version = new Version(artifact.getVersionRange().toString(),
                    artifact.getSelectedVersion().toString());
        } else {
            version = new Version(artifact.getVersion());
        }
    }
    return version;
}

From source file:com.ning.maven.plugins.duplicatefinder.DependencyWrapper.java

License:Apache License

public boolean matches(Artifact artifact) {
    ArtifactVersion version;/* ww w .  jav  a  2 s .c  o m*/

    try {
        if (artifact.getVersionRange() != null) {
            version = artifact.getSelectedVersion();
        } else {
            version = new DefaultArtifactVersion(artifact.getVersion());
        }
    } catch (OverConstrainedVersionException ex) {
        return false;
    }

    return StringUtils.equals(dependency.getGroupId(), artifact.getGroupId())
            && StringUtils.equals(dependency.getArtifactId(), artifact.getArtifactId())
            && StringUtils.equals(StringUtils.defaultIfEmpty(dependency.getType(), "jar"),
                    StringUtils.defaultIfEmpty(artifact.getType(), "jar"))
            && StringUtils.equals(dependency.getClassifier(), artifact.getClassifier())
            && (versionRange == null || versionRange.containsVersion(version)
                    || StringUtils.equals(artifact.getVersion(), dependency.getVersion()));
}