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

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

Introduction

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

Prototype

public static VersionRange createFromVersion(String version) 

Source Link

Usage

From source file:org.apache.tomcat.maven.plugin.tomcat6.AbstractRunMojo.java

License:Apache License

/**
 * Resolves the Artifact from the remote repository if necessary. If no version is specified, it will be retrieved
 * from the dependency list or from the DependencyManagement section of the pom.
 *
 * @param additionalWebapp containing information about artifact from plugin configuration.
 * @return Artifact object representing the specified file.
 * @throws MojoExecutionException with a message if the version can't be found in DependencyManagement.
 *//*from w  ww.j a va2 s  .  c o m*/
protected Artifact getArtifact(AbstractWebapp additionalWebapp) throws MojoExecutionException {

    Artifact artifact;
    VersionRange vr;
    try {
        vr = VersionRange.createFromVersionSpec(additionalWebapp.getVersion());
    } catch (InvalidVersionSpecificationException e) {
        getLog().warn("fail to create versionRange from version: " + additionalWebapp.getVersion(), e);
        vr = VersionRange.createFromVersion(additionalWebapp.getVersion());
    }

    if (StringUtils.isEmpty(additionalWebapp.getClassifier())) {
        artifact = artifactFactory.createDependencyArtifact(additionalWebapp.getGroupId(),
                additionalWebapp.getArtifactId(), vr, additionalWebapp.getType(), null, Artifact.SCOPE_COMPILE);
    } else {
        artifact = artifactFactory.createDependencyArtifact(additionalWebapp.getGroupId(),
                additionalWebapp.getArtifactId(), vr, additionalWebapp.getType(),
                additionalWebapp.getClassifier(), Artifact.SCOPE_COMPILE);
    }

    try {
        artifactResolver.resolve(artifact, project.getRemoteArtifactRepositories(), this.artifactRepository);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve artifact.", e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Unable to find artifact.", e);
    }

    return artifact;
}

From source file:org.apache.tomcat.maven.plugin.tomcat7.run.AbstractRunMojo.java

License:Apache License

/**
 * Resolves the Artifact from the remote repository if necessary. If no version is specified, it will be retrieved
 * from the dependency list or from the DependencyManagement section of the pom.
 *
 * @param additionalWebapp containing information about artifact from plugin configuration.
 * @return Artifact object representing the specified file.
 * @throws MojoExecutionException with a message if the version can't be found in DependencyManagement.
 *//*ww w . java  2s  .  c  o m*/
protected Artifact getArtifact(AbstractWebapp additionalWebapp) throws MojoExecutionException {

    Artifact artifact;
    VersionRange vr;
    try {
        vr = VersionRange.createFromVersionSpec(additionalWebapp.getVersion());
    } catch (InvalidVersionSpecificationException e) {
        getLog().warn("fail to create versionRange from version: " + additionalWebapp.getVersion(), e);
        vr = VersionRange.createFromVersion(additionalWebapp.getVersion());
    }

    if (StringUtils.isEmpty(additionalWebapp.getClassifier())) {
        artifact = factory.createDependencyArtifact(additionalWebapp.getGroupId(),
                additionalWebapp.getArtifactId(), vr, additionalWebapp.getType(), null, Artifact.SCOPE_COMPILE);
    } else {
        artifact = factory.createDependencyArtifact(additionalWebapp.getGroupId(),
                additionalWebapp.getArtifactId(), vr, additionalWebapp.getType(),
                additionalWebapp.getClassifier(), Artifact.SCOPE_COMPILE);
    }

    try {
        resolver.resolve(artifact, project.getRemoteArtifactRepositories(), this.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;
}

From source file:org.apache.tuscany.maven.plugin.surefire.OSGiSurefirePlugin.java

License:Apache License

private void addProvider(OSGiSurefireBooter surefireBooter, String provider, String version,
        Artifact filteredArtifact) throws ArtifactNotFoundException, ArtifactResolutionException {
    Artifact providerArtifact = artifactFactory.createDependencyArtifact("org.apache.maven.surefire", provider,
            VersionRange.createFromVersion(version), "jar", null, Artifact.SCOPE_TEST);
    ArtifactResolutionResult result = resolveArtifact(filteredArtifact, providerArtifact);

    for (Iterator i = result.getArtifacts().iterator(); i.hasNext();) {
        Artifact artifact = (Artifact) i.next();

        String key = ArtifactUtils.versionlessKey(artifact);
        if ("junit:junit".equals(key) || "jnuit:junit-dep".equals(key)) {
            // Skip junit as it will be pulled from the test case dependencies
            continue;
        }// w  w  w. ja  va2s.  c o m
        getLog().debug("Adding to surefire test classpath: " + artifact.getFile().getAbsolutePath());

        surefireBooter.addSurefireClassPathUrl(artifact.getFile().getAbsolutePath());
    }
}

From source file:org.apache.tuscany.maven.plugin.surefire.OSGiSurefirePlugin.java

License:Apache License

protected Artifact getArtifact(String groupId, String artifactId) throws MojoExecutionException {
    Artifact artifact;/*  w ww  .  j a  v a  2 s  .c om*/
    VersionRange vr;
    try {
        vr = VersionRange.createFromVersionSpec(project.getVersion());
    } catch (InvalidVersionSpecificationException e1) {
        vr = VersionRange.createFromVersion(project.getVersion());
    }
    artifact = artifactFactory.createDependencyArtifact(groupId, artifactId, vr, "jar", null,
            Artifact.SCOPE_TEST);

    try {
        artifactResolver.resolve(artifact, remoteRepositories, localRepository);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve artifact.", e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("Unable to find artifact.", e);
    }

    return artifact;
}

From source file:org.arakhne.maven.AbstractArakhneMojo.java

License:Open Source License

/**
 * Create an artifact from the given values.
 * /*  www  .ja  v a 2s  . co m*/
 * @param groupId
 * @param artifactId
 * @param version
 * @param scope
 * @param type
 * @return the artifact
 */
public final Artifact createArtifact(String groupId, String artifactId, String version, String scope,
        String type) {
    VersionRange versionRange = null;
    if (version != null) {
        versionRange = VersionRange.createFromVersion(version);
    }
    String desiredScope = scope;

    if (Artifact.SCOPE_TEST.equals(desiredScope)) {
        desiredScope = Artifact.SCOPE_TEST;
    }

    if (Artifact.SCOPE_PROVIDED.equals(desiredScope)) {
        desiredScope = Artifact.SCOPE_PROVIDED;
    }

    if (Artifact.SCOPE_SYSTEM.equals(desiredScope)) {
        // system scopes come through unchanged...
        desiredScope = Artifact.SCOPE_SYSTEM;
    }

    ArtifactHandler handler = getArtifactHandlerManager().getArtifactHandler(type);

    return new org.apache.maven.artifact.DefaultArtifact(groupId, artifactId, versionRange, desiredScope, type,
            null, // classifier
            handler, false); // optional
}

From source file:org.basepom.mojo.duplicatefinder.artifact.ArtifactFileResolver.java

License:Apache License

@VisibleForTesting
static DefaultArtifact canonicalizeArtifact(final Artifact artifact) {
    final VersionRange versionRange = artifact.getVersionRange() == null
            ? VersionRange.createFromVersion(artifact.getVersion())
            : artifact.getVersionRange();
    String type = MoreObjects.firstNonNull(artifact.getType(), "jar");
    String classifier = artifact.getClassifier();

    if ("test-jar".equals(type) && (classifier == null || "tests".equals(classifier))) {
        type = "jar";
        classifier = "tests";
    }/*  w w w  . j a va 2  s. c o m*/

    final DefaultArtifact canonicalizedArtifact = new DefaultArtifact(artifact.getGroupId(),
            artifact.getArtifactId(), versionRange, artifact.getScope(), type, classifier,
            artifact.getArtifactHandler(), artifact.isOptional());

    return canonicalizedArtifact;
}

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");
    }//from   ww w  .ja v a 2s . c  om

    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;// w ww. j  a  v  a 2 s.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.buildforce.build.maven.plugins.RemoveDependenciesFromLocalRepositoryMojo.java

License:Apache License

private Artifact convertDependencyToArtifact(Dependency dependency) {
    return new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(),
            VersionRange.createFromVersion(dependency.getVersion()), dependency.getScope(),
            dependency.getType(), dependency.getClassifier(),
            new DefaultArtifactHandler(project.getArtifact().getType()));
}

From source file:org.carlspring.maven.commons.util.ArtifactUtils.java

License:Apache License

public static Artifact convertPathToArtifact(String path) {
    String groupId = "";
    String version;/* w w  w .  j av  a 2  s . c  om*/
    String scope = "compile";
    String classifier = "";
    String type = path.substring(path.lastIndexOf(".") + 1, path.length());

    String[] groupIdElements = path.split("/");
    for (int i = 0; i < (groupIdElements.length - 3); i++) {
        if (groupId.length() == 0) {
            groupId += groupIdElements[i];
        } else {
            groupId += "." + groupIdElements[i];
        }
    }

    String[] split = path.substring(path.lastIndexOf("/") + 1, path.length() - 4).split("-");

    /* Parse the artifactId */
    StringBuilder artifactId = new StringBuilder();
    int i = 0;
    for (; i < split.length; i++) {
        String token = split[i];
        try {
            Integer.parseInt(token.substring(0, 1));
            break;
        } catch (NumberFormatException e) {
            // This is okay, as we still haven't reached the version.
        }

        if (artifactId.length() > 0) {
            artifactId.append("-");
        }

        artifactId.append(token);
    }

    /* Parse the artifactId */

    /* Parse the version */

    version = split[i];

    // If the version starts with a number, all is fine.
    Integer.parseInt(version.substring(0, 1));

    // TODO: Not checking for number format exception

    i++;

    // Check if the version is a SNAPSHOT and append it, if it is.
    if ((i < split.length) && split[i].equals("SNAPSHOT")) {
        version += "-" + split[i];
        i++;
    }

    /* Parse the version */

    /* Parse the classifier, if any */
    if (i == (split.length - 1)) {
        classifier = split[i];
    }
    /* Parse the classifier, if any */

    return new DefaultArtifact(groupId, artifactId.toString(), VersionRange.createFromVersion(version), scope,
            type, classifier, new DefaultArtifactHandler(type));
}