Example usage for org.apache.maven.artifact.versioning VersionRange createFromVersionSpec

List of usage examples for org.apache.maven.artifact.versioning VersionRange createFromVersionSpec

Introduction

In this page you can find the example usage for org.apache.maven.artifact.versioning VersionRange createFromVersionSpec.

Prototype

public static VersionRange createFromVersionSpec(String spec) throws InvalidVersionSpecificationException 

Source Link

Document

Create a version range from a string representation

Some spec examples are:
  • 1.0 Version 1.0
  • [1.0,2.0) Versions 1.0 (included) to 2.0 (not included)
  • [1.0,2.0] Versions 1.0 to 2.0 (both included)
  • [1.5,) Versions 1.5 and higher
  • (,1.0],[1.2,) Versions up to 1.0 (included) and 1.2 or higher

Usage

From source file:net.oneandone.pommes.model.Database.java

License:Apache License

public static Document document(String origin, MavenProject mavenProject)
        throws InvalidVersionSpecificationException {
    Document doc;/*from ww  w .ja v a 2  s .com*/
    MavenProject parent;
    Pom parPom;

    doc = document(origin, Pom.forProject(origin, mavenProject));
    for (Dependency dependency : mavenProject.getDependencies()) {
        GAV dep = GAV.forDependency(dependency);

        // index groupId:artifactId for non-version searches
        doc.add(new StringField(DEP_GA, dep.toGaString(), Field.Store.YES));
        // index groupId:artifactId:version for exact-version searches
        doc.add(new StringField(DEP_GAV, dep.toGavString(), Field.Store.YES));
        // index groupId:artifactId for searches that need to evaluate the range
        VersionRange vr = VersionRange.createFromVersionSpec(dependency.getVersion());
        if (vr.hasRestrictions()) {
            doc.add(new StringField(DEP_GA_RANGE, dep.toGaString(), Field.Store.YES));
        }
    }

    // parent
    parent = mavenProject.getParent();
    if (parent != null) {
        parPom = Pom.forProject(origin, parent);
        doc.add(new StringField(PAR_GA, parPom.coordinates.toGaString(), Field.Store.YES));
        doc.add(new StringField(PAR_GAV, parPom.coordinates.toGavString(), Field.Store.YES));
    }
    return doc;
}

From source file:npanday.executable.impl.MatchPolicyFactory.java

License:Apache License

public static ExecutableMatchPolicy createExecutableVersionPolicy(final String requiredExecutableVersion) {
    return new ExecutableMatchPolicy() {
        public boolean match(ExecutableCapability executableCapability) {
            // if not specified, all versions are valid
            if (isNullOrEmpty(requiredExecutableVersion))
                return true;

            String offeredExecutableVersion = executableCapability.getExecutableVersion();

            // if not specified, it is valid for all versions!
            if (isNullOrEmpty(offeredExecutableVersion))
                return true;

            String required = requiredExecutableVersion.toLowerCase().trim();
            offeredExecutableVersion = offeredExecutableVersion.toLowerCase().trim();
            try {
                VersionRange range = VersionRange.createFromVersionSpec(offeredExecutableVersion);
                return range.containsVersion(new DefaultArtifactVersion(required));
            } catch (InvalidVersionSpecificationException e) {
                // fallback to just matching version if not a valid range
                return required.equals(offeredExecutableVersion);
            }/*from   ww w  . java 2s. c o  m*/
        }

        public String toString() {
            return "ExecutableMatchPolicy[executableVersion: '" + requiredExecutableVersion + "']";
        }
    };
}

From source file:org.apache.aries.plugin.esa.stubs.EsaMavenProjectStub9.java

License:Apache License

public Set getArtifacts() {
    try {/*from  w  ww. j  av a2 s . com*/
        Set artifacts = new HashSet();

        artifacts.add(createArtifact("org.apache.maven.test", "maven-artifact01", "1.0-SNAPSHOT", false));
        Artifact artifact02 = createArtifact("org.apache.maven.test", "maven-artifact02", "1.0-SNAPSHOT",
                false);
        artifact02.setVersionRange(VersionRange.createFromVersionSpec("[1.3, 2.5)"));
        artifacts.add(artifact02);
        artifacts.add(createArtifact("org.apache.maven.test", "maven-artifact03", "1.1-SNAPSHOT", false));
        artifacts.add(createArtifact("org.apache.maven.test", "maven-artifact04", "1.2-SNAPSHOT", "esa", true));
        return artifacts;
    } catch (InvalidVersionSpecificationException e) {
        throw new RuntimeException(e);
    }
}

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

License:Apache License

private Collection<Artifact> getAllDependencies() throws MojoExecutionException {
    List<Artifact> artifacts = new ArrayList<Artifact>();

    for (Iterator<?> dependencies = project.getDependencies().iterator(); dependencies.hasNext();) {
        Dependency dependency = (Dependency) dependencies.next();

        String groupId = dependency.getGroupId();
        String artifactId = dependency.getArtifactId();

        VersionRange versionRange;//from w w  w. ja v  a 2 s .  com
        try {
            versionRange = VersionRange.createFromVersionSpec(dependency.getVersion());
        } catch (InvalidVersionSpecificationException e) {
            throw new MojoExecutionException("unable to parse version", e);
        }

        String type = dependency.getType();
        if (type == null) {
            type = "jar";
        }
        String classifier = dependency.getClassifier();
        boolean optional = dependency.isOptional();
        String scope = dependency.getScope();
        if (scope == null) {
            scope = Artifact.SCOPE_COMPILE;
        }

        Artifact art = this.artifactFactory.createDependencyArtifact(groupId, artifactId, versionRange, type,
                classifier, scope, null, optional);

        if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
            art.setFile(new File(dependency.getSystemPath()));
        }

        List<String> exclusions = new ArrayList<String>();
        for (Iterator<?> j = dependency.getExclusions().iterator(); j.hasNext();) {
            Exclusion e = (Exclusion) j.next();
            exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
        }

        ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

        art.setDependencyFilter(newFilter);

        artifacts.add(art);
    }

    return artifacts;
}

From source file:org.apache.camel.maven.ConvertersMojo.java

License:Apache License

private File getSkinArtifactFile(DecorationModel decoration) throws MojoFailureException {

    Skin skin = decoration.getSkin();/* w ww .  j av a2  s .  c  o m*/
    if (skin == null) {
        skin = Skin.getDefaultSkin();
    }

    String version = skin.getVersion();
    Artifact artifact;
    try {
        if (version == null) {
            version = Artifact.RELEASE_VERSION;
        }

        VersionRange versionSpec = VersionRange.createFromVersionSpec(version);
        artifact = artifactFactory.createDependencyArtifact(skin.getGroupId(), skin.getArtifactId(),
                versionSpec, "jar", null, null);

        artifactResolver.resolve(artifact, remoteArtifactRepositories, localRepository);
        return artifact.getFile();
    } catch (InvalidVersionSpecificationException e) {
        throw new MojoFailureException("The skin version '" + version + "' is not valid: " + e.getMessage());
    } catch (ArtifactResolutionException e) {
        throw new MojoFailureException("Unable to fink skin: " + e.getMessage());
    } catch (ArtifactNotFoundException e) {
        throw new MojoFailureException("The skin does not exist: " + e.getMessage());
    }
}

From source file:org.apache.felix.bundleplugin.baseline.AbstractBaselinePlugin.java

License:Apache License

private Artifact getPreviousArtifact() throws MojoFailureException, MojoExecutionException {
    // Find the previous version JAR and resolve it, and it's dependencies
    final VersionRange range;
    try {//from   ww w.  ja  v  a 2s.  c  om
        range = VersionRange.createFromVersionSpec(comparisonVersion);
    } catch (InvalidVersionSpecificationException e) {
        throw new MojoFailureException("Invalid comparison version: " + e.getMessage());
    }

    final Artifact previousArtifact;
    try {
        previousArtifact = factory.createDependencyArtifact(comparisonGroupId, comparisonArtifactId, range,
                comparisonPackaging, comparisonClassifier, Artifact.SCOPE_COMPILE);

        if (!previousArtifact.getVersionRange().isSelectedVersionKnown(previousArtifact)) {
            getLog().debug("Searching for versions in range: " + previousArtifact.getVersionRange());
            @SuppressWarnings("unchecked")
            // type is konwn
            List<ArtifactVersion> availableVersions = metadataSource.retrieveAvailableVersions(previousArtifact,
                    session.getLocalRepository(), project.getRemoteArtifactRepositories());
            filterSnapshots(availableVersions);
            ArtifactVersion version = range.matchVersion(availableVersions);
            if (version != null) {
                previousArtifact.selectVersion(version.toString());
            }
        }
    } catch (OverConstrainedVersionException ocve) {
        throw new MojoFailureException("Invalid comparison version: " + ocve.getMessage());
    } catch (ArtifactMetadataRetrievalException amre) {
        throw new MojoExecutionException("Error determining previous version: " + amre.getMessage(), amre);
    }

    if (previousArtifact.getVersion() == null) {
        getLog().info("Unable to find a previous version of the project in the repository");
        return null;
    }

    try {
        resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(),
                session.getLocalRepository());
    } catch (ArtifactResolutionException are) {
        throw new MojoExecutionException(
                "Artifact " + previousArtifact + " cannot be resolved : " + are.getMessage(), are);
    } catch (ArtifactNotFoundException anfe) {
        throw new MojoExecutionException(
                "Artifact " + previousArtifact + " does not exist on local/remote repositories", anfe);
    }

    return previousArtifact;
}

From source file:org.apache.felix.karaf.tooling.features.GenerateFeaturesFileMojo.java

License:Apache License

private void prepare() throws ArtifactResolutionException, ArtifactNotFoundException, IOException,
        InvalidVersionSpecificationException {
    if (translation != null) {
        InputStream stream = null;
        try {/*  ww  w  . j  a v a 2s  .  c o m*/
            stream = new BufferedInputStream(new FileInputStream(translation));
            Properties file = new Properties();
            file.load(stream);
            ArrayList<String> stringNames = getStringNames(file);
            for (String key : stringNames) {
                String[] elements = key.split("/");
                translations.get(String.format("%s/%s", elements[0], elements[1]))
                        .put(VersionRange.createFromVersionSpec(elements[2]), file.getProperty(key));
            }
            getLog().info("Loaded " + translations.size() + " bundle name translation rules from "
                    + translation.getAbsolutePath());
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }

    Artifact kernel = factory.createArtifact("org.apache.felix.karaf", "apache-felix-karaf", karafVersion,
            Artifact.SCOPE_PROVIDED, "pom");
    resolver.resolve(kernel, remoteRepos, localRepo);
    getLog().info("-- List of bundles provided by Karaf " + karafVersion + " --");
    for (Artifact artifact : getDependencies(kernel)) {
        getLog().info(" " + artifact);
        provided.add(artifact);
    }
    getLog().info("-- <end of list>  --");
}

From source file:org.apache.felix.karaf.tooling.features.MojoSupport.java

License:Apache License

protected Map createManagedVersionMap(String projectId, DependencyManagement dependencyManagement)
        throws ProjectBuildingException {
    Map map;//from   w w w .j a v a  2 s.  c  o m
    if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
        map = new HashMap();
        for (Iterator i = dependencyManagement.getDependencies().iterator(); i.hasNext();) {
            Dependency d = (Dependency) i.next();

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

From source file:org.apache.rat.mp.RatReportMojo.java

License:Apache License

/**
 * Returns the skins artifact file.// ww w. ja  va2  s .  co m
 *
 * @return Artifact file
 * @throws MojoFailureException   An error in the plugin configuration was detected.
 * @throws MojoExecutionException An error occurred while searching for the artifact file.
 */
private File getSkinArtifactFile() throws MojoFailureException, MojoExecutionException {
    final Skin skin = Skin.getDefaultSkin();

    String version = skin.getVersion();
    final Artifact artifact;
    try {
        if (version == null) {
            version = Artifact.RELEASE_VERSION;
        }
        VersionRange versionSpec = VersionRange.createFromVersionSpec(version);
        artifact = factory.createDependencyArtifact(skin.getGroupId(), skin.getArtifactId(), versionSpec, "jar",
                null, null);

        resolver.resolve(artifact, getProject().getRemoteArtifactRepositories(), localRepository);
    } catch (InvalidVersionSpecificationException e) {
        throw new MojoFailureException("The skin version '" + version + "' is not valid: " + e.getMessage());
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to find skin", e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoFailureException("The skin does not exist: " + e.getMessage());
    }

    return artifact.getFile();
}

From source file:org.apache.sling.maven.projectsupport.AbstractBundleListMojo.java

License:Apache License

/**
 * Get a resolved Artifact from the coordinates provided
 *
 * @return the artifact, which has been resolved.
 * @throws MojoExecutionException//w  w w  .j  a  va2s  . co m
 */
protected Artifact getArtifact(String groupId, String artifactId, String version, String type,
        String classifier) throws MojoExecutionException {
    Artifact artifact;
    VersionRange vr;

    try {
        vr = VersionRange.createFromVersionSpec(version);
    } catch (InvalidVersionSpecificationException e) {
        vr = VersionRange.createFromVersion(version);
    }

    if (StringUtils.isEmpty(classifier)) {
        artifact = factory.createDependencyArtifact(groupId, artifactId, vr, type, null,
                Artifact.SCOPE_COMPILE);
    } else {
        artifact = factory.createDependencyArtifact(groupId, artifactId, vr, type, classifier,
                Artifact.SCOPE_COMPILE);
    }

    // This code kicks in when the version specifier is a range.
    if (vr.getRecommendedVersion() == null) {
        try {
            List<ArtifactVersion> availVersions = metadataSource.retrieveAvailableVersions(artifact, local,
                    remoteRepos);
            ArtifactVersion resolvedVersion = vr.matchVersion(availVersions);
            artifact.setVersion(resolvedVersion.toString());
        } catch (ArtifactMetadataRetrievalException e) {
            throw new MojoExecutionException("Unable to find version for artifact", e);
        }

    }

    try {
        resolver.resolve(artifact, remoteRepos, local);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve artifact.", e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Unable to find artifact.", e);
    }
    return artifact;
}