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

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

Introduction

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

Prototype

public boolean containsVersion(ArtifactVersion version) 

Source Link

Usage

From source file:org.bimserver.plugins.PluginManager.java

License:Open Source License

public PluginBundle loadFromPluginDir(PluginBundleVersionIdentifier pluginBundleVersionIdentifier,
        SPluginBundleVersion pluginBundleVersion, List<SPluginInformation> plugins,
        boolean strictDependencyChecking) throws Exception {
    Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
    if (!Files.exists(target)) {
        throw new PluginException(target.toString() + " not found");
    }//  w w  w  . j a  v  a 2  s. com

    SPluginBundle sPluginBundle = new SPluginBundle();

    MavenXpp3Reader mavenreader = new MavenXpp3Reader();

    try (JarFile jarFile = new JarFile(target.toFile())) {
        ZipEntry entry = jarFile.getEntry("META-INF/maven/" + pluginBundleVersion.getGroupId() + "/"
                + pluginBundleVersion.getArtifactId() + "/pom.xml");
        Model model = mavenreader.read(jarFile.getInputStream(entry));

        sPluginBundle.setOrganization(model.getOrganization().getName());
        sPluginBundle.setName(model.getName());

        DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());

        for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
            if (dependency.getGroupId().equals("org.opensourcebim")
                    && (dependency.getArtifactId().equals("shared")
                            || dependency.getArtifactId().equals("pluginbase"))) {
                // TODO Skip, we should also check the version though
            } else {
                PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(
                        dependency.getGroupId(), dependency.getArtifactId());
                if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
                    if (strictDependencyChecking) {
                        VersionRange versionRange = VersionRange.createFromVersion(dependency.getVersion());
                        String version = pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier)
                                .getPluginBundleVersion().getVersion();
                        ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
                        if (versionRange.containsVersion(artifactVersion)) {
                            // OK
                        } else {
                            throw new Exception("Required dependency " + pluginBundleIdentifier
                                    + " is installed, but it's version (" + version
                                    + ") does not comply to the required version (" + dependency.getVersion()
                                    + ")");
                        }
                    } else {
                        LOGGER.info("Skipping strict dependency checking for dependency "
                                + dependency.getArtifactId());
                    }
                } else {
                    if (pluginBundleIdentifier.getGroupId().equals("org.opensourcebim")) {
                        throw new Exception(
                                "Required dependency " + pluginBundleIdentifier + " is not installed");
                    } else {
                        MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(
                                model.getRepositories().get(0).getUrl(), dependency.getGroupId(),
                                dependency.getArtifactId());

                        try {
                            Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());

                            FileJarClassLoader jarClassLoader = new FileJarClassLoader(this,
                                    delegatingClassLoader, depJarFile);
                            jarClassLoaders.add(jarClassLoader);
                            delegatingClassLoader.add(jarClassLoader);
                        } catch (Exception e) {

                        }
                    }
                }
            }
        }

        return loadPlugin(pluginBundleVersionIdentifier, target, sPluginBundle, pluginBundleVersion, plugins,
                delegatingClassLoader);
    }
}

From source file:org.bimserver.plugins.PluginManager.java

License:Open Source License

public PluginBundle install(MavenPluginBundle mavenPluginBundle, List<SPluginInformation> plugins,
        boolean strictDependencyChecking) throws Exception {
    PluginBundleVersionIdentifier pluginBundleVersionIdentifier = mavenPluginBundle
            .getPluginVersionIdentifier();
    MavenXpp3Reader mavenreader = new MavenXpp3Reader();
    Model model = null;/*from w w  w  .j a va  2s.  c om*/
    try (InputStream pomInputStream = mavenPluginBundle.getPomInputStream()) {
        model = mavenreader.read(pomInputStream);
    }

    if (plugins == null) {
        try (JarInputStream jarInputStream = new JarInputStream(mavenPluginBundle.getJarInputStream())) {
            JarEntry nextJarEntry = jarInputStream.getNextJarEntry();
            while (nextJarEntry != null) {
                if (nextJarEntry.getName().equals("plugin/plugin.xml")) {
                    // Install all plugins
                    PluginDescriptor pluginDescriptor = getPluginDescriptor(
                            new FakeClosingInputStream(jarInputStream));
                    plugins = new ArrayList<>();
                    processPluginDescriptor(pluginDescriptor, plugins);
                    for (SPluginInformation info : plugins) {
                        info.setInstallForAllUsers(true);
                        info.setInstallForNewUsers(true);
                    }
                    break;
                }
                nextJarEntry = jarInputStream.getNextJarEntry();
            }
        }
    }

    DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());

    for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
        if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared")
                || dependency.getArtifactId().equals("pluginbase"))) {
            // TODO Skip, we should also check the version though
        } else {
            PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependency.getGroupId(),
                    dependency.getArtifactId());
            if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
                if (strictDependencyChecking) {
                    VersionRange versionRange = VersionRange.createFromVersion(dependency.getVersion());
                    //                  String version = pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
                    ArtifactVersion artifactVersion = new DefaultArtifactVersion(
                            mavenPluginBundle.getVersion());
                    if (versionRange.containsVersion(artifactVersion)) {
                        // OK
                    } else {
                        throw new Exception("Required dependency " + pluginBundleIdentifier
                                + " is installed, but it's version (" + mavenPluginBundle.getVersion()
                                + ") does not comply to the required version (" + dependency.getVersion()
                                + ")");
                    }
                } else {
                    LOGGER.info(
                            "Skipping strict dependency checking for dependency " + dependency.getArtifactId());
                }
            } else {
                try {
                    MavenPluginLocation mavenPluginLocation = mavenPluginRepository
                            .getPluginLocation(dependency.getGroupId(), dependency.getArtifactId());
                    Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());

                    FileJarClassLoader jarClassLoader = new FileJarClassLoader(this, delegatingClassLoader,
                            depJarFile);
                    jarClassLoaders.add(jarClassLoader);
                    delegatingClassLoader.add(jarClassLoader);
                } catch (Exception e) {
                    throw new Exception("Required dependency " + pluginBundleIdentifier + " is not installed");
                }
            }
        }
    }

    Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
    if (Files.exists(target)) {
        throw new PluginException("This plugin has already been installed " + target.getFileName().toString());
    }
    Files.copy(mavenPluginBundle.getJarInputStream(), target);

    return loadPlugin(pluginBundleVersionIdentifier, target, mavenPluginBundle.getPluginBundle(),
            mavenPluginBundle.getPluginBundleVersion(), plugins, delegatingClassLoader);
}

From source file:org.codehaus.mojo.setup.AbstractSetupMojo.java

License:Apache License

/**
 * During the initializingPhase we can check if the required Maven version is used.
 * /*  w ww  .  j a  va2s  .co  m*/
 * @param range the version range of Maven for which this goal is executable. 
 * @return true is version is within range, otherwise false.
 */
private boolean isRequiredMavenVersion(VersionRange range) {
    return range.containsVersion(rti.getApplicationVersion());
}

From source file:org.codehaus.mojo.versions.api.ArtifactVersions.java

License:Apache License

/**
 * Checks if the version is in the range (and ensures that the range respects the <code>-!</code> syntax
 * to rule out any qualifiers from range boundaries).
 *
 * @param version the version to check./*from   w w w  .  j a v  a2 s . c  om*/
 * @param range   the range to check.
 * @return <code>true</code> if and only if the version is in the range.
 * @since 1.3
 */
public static boolean isVersionInRange(ArtifactVersion version, VersionRange range) {
    if (!range.containsVersion(version)) {
        return false;
    }
    for (Restriction r : ((List<Restriction>) range.getRestrictions())) {
        if (r.containsVersion(version)) {
            // check for the -! syntax
            if (!r.isLowerBoundInclusive() && r.getLowerBound() != null) {
                String s = r.getLowerBound().toString();
                if (s.endsWith("-!") && version.toString().startsWith(s.substring(0, s.length() - 2))) {
                    return false;
                }
            }
            if (!r.isUpperBoundInclusive() && r.getUpperBound() != null) {
                String s = r.getUpperBound().toString();
                if (s.endsWith("-!") && version.toString().startsWith(s.substring(0, s.length() - 2))) {
                    return false;
                }
            }
        }
    }
    return true;
}

From source file:org.echocat.jomon.maven.boot.ArtifactFactory.java

License:Mozilla Public License

@Nullable
protected String selectLatestMatchingVersion(@Nonnull ArtifactRepositoryMetadata repositoryMetadata,
        @Nonnull Artifact artifact) {/*from   ww w  .  j  a  va 2 s.  c  om*/
    final Metadata metadata = repositoryMetadata.getMetadata();
    String latestMatchingVersion = null;
    final Versioning versioning = metadata.getVersioning();
    if (versioning != null) {
        final List<String> versions = versioning.getVersions();
        final ListIterator<String> i = versions.listIterator(versions.size());
        final VersionRange versionRange = artifact.getVersionRange();
        while (i.hasPrevious() && isEmpty(latestMatchingVersion)) {
            final String current = i.previous();
            if (versionRange.containsVersion(new DefaultArtifactVersion(current))) {
                latestMatchingVersion = current;
            }
        }
    }
    return latestMatchingVersion;
}

From source file:org.eclipse.m2e.core.internal.embedder.AbstractWorkspaceRuntime.java

License:Open Source License

protected IMavenProjectFacade getMavenDistribution() {
    try {//from  ww  w  .j a v  a2  s  . c  o m
        VersionRange range = VersionRange.createFromVersionSpec(getDistributionArtifactKey().getVersion());
        for (IMavenProjectFacade facade : projectManager.getProjects()) {
            ArtifactKey artifactKey = facade.getArtifactKey();
            if (getDistributionArtifactKey().getGroupId().equals(artifactKey.getGroupId()) //
                    && getDistributionArtifactKey().getArtifactId().equals(artifactKey.getArtifactId())//
                    && range.containsVersion(new DefaultArtifactVersion(artifactKey.getVersion()))) {
                return facade;
            }
        }
    } catch (InvalidVersionSpecificationException e) {
        // can't happen
    }
    return null;
}

From source file:org.eclipse.m2e.core.internal.embedder.MavenWorkspaceRuntime.java

License:Open Source License

private IMavenProjectFacade getMavenDistribution() {
    try {//from  w  ww  .j av a2s . co  m
        VersionRange range = VersionRange.createFromVersionSpec(MAVEN_DISTRIBUTION.getVersion());
        for (IMavenProjectFacade facade : projectManager.getProjects()) {
            ArtifactKey artifactKey = facade.getArtifactKey();
            if (MAVEN_DISTRIBUTION.getGroupId().equals(artifactKey.getGroupId()) //
                    && MAVEN_DISTRIBUTION.getArtifactId().equals(artifactKey.getArtifactId())//
                    && range.containsVersion(new DefaultArtifactVersion(artifactKey.getVersion()))) {
                return facade;
            }
        }
    } catch (InvalidVersionSpecificationException e) {
        // can't happen
    }
    return null;
}

From source file:org.eclipse.m2e.core.internal.project.registry.MavenRequiredCapability.java

License:Open Source License

public boolean isPotentialMatch(Capability capability) {
    if (capability instanceof MavenCapability && getVersionlessKey().equals(capability.getVersionlessKey())) {
        try {/*from   w  w w .  ja  va 2  s.c  o m*/
            // TODO may need to cache parsed version and versionRange for performance reasons
            ArtifactVersion version = new DefaultArtifactVersion(((MavenCapability) capability).getVersion());
            VersionRange range = VersionRange.createFromVersionSpec(versionRange);
            return range.containsVersion(version);
        } catch (InvalidVersionSpecificationException ex) {
            return true; // better safe than sorry
        }
    }
    return false;
}

From source file:org.eclipse.m2e.core.ui.internal.editing.LifecycleMappingOperation.java

License:Open Source License

public void process(Document document) {
    Element root = document.getDocumentElement();
    Element pluginExecutions; // add the new plugins here 

    //now find the lifecycle stuff if it's there.
    if (createAtTopLevel) {
        if (root == null) {
            // probably an empty document
            root = document.createElement("lifecycleMappingMetadata"); //$NON-NLS-1$
            document.appendChild(root);/*from w w w  .java 2  s  . c  o m*/
        }
        pluginExecutions = getChild(root, "pluginExecutions"); //$NON-NLS-1$
    } else {
        Element managedPlugins = getChild(root, BUILD, PLUGIN_MANAGEMENT, PLUGINS);
        Element lifecyclePlugin = findChild(managedPlugins, PLUGIN,
                childEquals(GROUP_ID, LIFECYCLE_PLUGIN_GROUPID),
                childEquals(ARTIFACT_ID, LIFECYCLE_PLUGIN_ARTIFACTID));

        if (lifecyclePlugin == null) {
            //not found, create
            lifecyclePlugin = PomHelper.createPlugin(managedPlugins, LIFECYCLE_PLUGIN_GROUPID,
                    LIFECYCLE_PLUGIN_ARTIFACTID, LIFECYCLE_PLUGIN_VERSION);

            //mkleint: a bit scared to have this text localized, with chinese/japanese locales, it could write garbage into the pom file..
            Comment comment = document.createComment(
                    "This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself."); //$NON-NLS-1$
            managedPlugins.insertBefore(comment, lifecyclePlugin);
            format(comment);
        }

        pluginExecutions = getChild(lifecyclePlugin, CONFIGURATION, "lifecycleMappingMetadata", //$NON-NLS-1$
                "pluginExecutions"); //$NON-NLS-1$
    }

    //now find the plugin execution for the plugin we have..
    Element execution = null;
    for (Element exec : findChilds(pluginExecutions, "pluginExecution")) { //$NON-NLS-1$
        Element filter = findChild(exec, "pluginExecutionFilter", //$NON-NLS-1$
                childEquals(GROUP_ID, groupId), childEquals(ARTIFACT_ID, artifactId));
        //the action needs to match the action we want..
        Element actionEl = findChild(findChild(exec, "action"), action.toString()); //$NON-NLS-1$
        if (filter != null && actionEl != null) {
            String versionRange = getTextValue(getChild(filter, "versionRange")); //$NON-NLS-1$
            if (versionRange != null) { //  paranoid null check
                //now we shall do some smart matching on the existing versionRange and our version..
                //so far the "smart" thing involves just overwriting the range.
                try {
                    VersionRange range = VersionRange.createFromVersionSpec(versionRange);
                    if (!range.containsVersion(new DefaultArtifactVersion(version))) {
                        Element rangeEl = findChild(filter, "versionRange"); //$NON-NLS-1$
                        setText(rangeEl, "[" + version + ",)"); //$NON-NLS-1$ //$NON-NLS-2$
                    }
                } catch (InvalidVersionSpecificationException e) {
                    log.error("Failed to parse version range:" + versionRange, e); //$NON-NLS-1$
                }
            }
            execution = exec;
            break;
        }
    }
    if (execution == null) {
        execution = createPluginExecution(document, pluginExecutions);
    }
    //now enter/update the goal(s)..
    Element goalsEl = getChild(execution, "pluginExecutionFilter", GOALS); //$NON-NLS-1$
    List<String> toAddGoals = new ArrayList<String>(Arrays.asList(goals));
    for (Element existingGoal : findChilds(goalsEl, GOAL)) {
        String glValue = getTextValue(existingGoal);
        if (glValue != null && toAddGoals.contains(glValue)) {
            toAddGoals.remove(glValue);
        }
    }
    if (toAddGoals.size() > 0) {
        for (String goal : toAddGoals) {
            format(createElementWithText(goalsEl, GOAL, goal));
        }
    }

}

From source file:org.eclipse.m2e.editor.xml.internal.lifecycle.LifecycleMappingOperation.java

License:Open Source License

public void process(Document document) {
    Element root = document.getDocumentElement();
    Element managedPlugins = getChild(root, BUILD, PLUGIN_MANAGEMENT, PLUGINS);
    //now find the lifecycle stuff if it's there.
    Element lifecyclePlugin = findChild(managedPlugins, PLUGIN, childEquals(GROUP_ID, LIFECYCLE_PLUGIN_GROUPID),
            childEquals(ARTIFACT_ID, LIFECYCLE_PLUGIN_ARTIFACTID));
    if (lifecyclePlugin == null) {
        //not found, create
        lifecyclePlugin = PomHelper.createPlugin(managedPlugins, LIFECYCLE_PLUGIN_GROUPID,
                LIFECYCLE_PLUGIN_ARTIFACTID, LIFECYCLE_PLUGIN_VERSION);
        Comment comment = document.createComment("TODO TEXT. This plugin's configuration is used in m2e only.");
        managedPlugins.insertBefore(comment, lifecyclePlugin);
        format(comment);/*from w  ww. j av a 2s.  c om*/
    }

    Element pluginExecutions = getChild(lifecyclePlugin, CONFIGURATION, "lifecycleMappingMetadata",
            "pluginExecutions");
    //now find the plugin execution for the plugin we have..
    Element execution = null;
    for (Element exec : findChilds(pluginExecutions, "pluginExecution")) {
        Element filter = findChild(exec, "pluginExecutionFilter", childEquals("groupId", groupId),
                childEquals("artifactId", artifactId));
        //the action needs to match the action we want..
        Element actionEl = findChild(findChild(exec, "action"), action.toString());
        if (filter != null && actionEl != null) {
            String versionRange = getTextValue(getChild(filter, "versionRange"));
            if (versionRange != null) { //  paranoid null check
                //now we shall do some smart matching on the existing versionRange and our version..
                //so far the "smart" thing involves just overwriting the range.
                try {
                    VersionRange range = VersionRange.createFromVersionSpec(versionRange);
                    if (!range.containsVersion(new DefaultArtifactVersion(version))) {
                        Element rangeEl = findChild(filter, "versionRange");
                        setText(rangeEl, "[" + version + ",)");
                    }
                } catch (InvalidVersionSpecificationException e) {
                    log.error("Failed to parse version range:" + versionRange, e);
                }
            }
            execution = exec;
            break;
        }
    }
    if (execution == null) {
        execution = createPluginExecution(document, pluginExecutions);
    }
    //now enter/update the goal(s)..
    Element goalsEl = getChild(execution, "pluginExecutionFilter", "goals");
    List<String> toAddGoals = new ArrayList<String>(Arrays.asList(goals));
    for (Element existingGoal : findChilds(goalsEl, "goal")) {
        String glValue = getTextValue(existingGoal);
        if (glValue != null && toAddGoals.contains(glValue)) {
            toAddGoals.remove(glValue);
        }
    }
    if (toAddGoals.size() > 0) {
        for (String goal : toAddGoals) {
            format(createElementWithText(goalsEl, "goal", goal));
        }
    }

}