Search in sources :

Example 1 with VersionRange

use of org.apache.maven.artifact.versioning.VersionRange in project karaf by apache.

the class MojoSupport method createManagedVersionMap.

protected Map createManagedVersionMap(String projectId, DependencyManagement dependencyManagement) throws ProjectBuildingException {
    Map map;
    if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
        map = new HashMap();
        for (Dependency d : dependencyManagement.getDependencies()) {
            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;
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) HashMap(java.util.HashMap) VersionRange(org.apache.maven.artifact.versioning.VersionRange) Dependency(org.apache.maven.model.Dependency) HashMap(java.util.HashMap) Map(java.util.Map) Artifact(org.apache.maven.artifact.Artifact)

Example 2 with VersionRange

use of org.apache.maven.artifact.versioning.VersionRange in project camel by apache.

the class RunMojo method getAllDependencies.

// generic method to retrieve all the transitive dependencies
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;
        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 (Exclusion exclusion : dependency.getExclusions()) {
            exclusions.add(exclusion.getGroupId() + ":" + exclusion.getArtifactId());
        }
        ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);
        art.setDependencyFilter(newFilter);
        artifacts.add(art);
    }
    return artifacts;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) VersionRange(org.apache.maven.artifact.versioning.VersionRange) Dependency(org.apache.maven.model.Dependency) ExecutableDependency(org.codehaus.mojo.exec.ExecutableDependency) Artifact(org.apache.maven.artifact.Artifact) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) Exclusion(org.apache.maven.model.Exclusion) File(java.io.File)

Example 3 with VersionRange

use of org.apache.maven.artifact.versioning.VersionRange in project BIMserver by opensourceBIM.

the class PluginManager method loadFromPluginDir.

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");
    }
    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 (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
                    } else {
                        MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(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);
    }
}
Also used : Path(java.nio.file.Path) PluginException(org.bimserver.shared.exceptions.PluginException) ZipEntry(java.util.zip.ZipEntry) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) VersionRange(org.apache.maven.artifact.versioning.VersionRange) JarFile(java.util.jar.JarFile) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) FileJarClassLoader(org.bimserver.plugins.classloaders.FileJarClassLoader) Model(org.apache.maven.model.Model)

Example 4 with VersionRange

use of org.apache.maven.artifact.versioning.VersionRange in project BIMserver by opensourceBIM.

the class PluginBundleDatabaseAction method processMavenPluginLocation.

public SPluginBundle processMavenPluginLocation(MavenPluginLocation mavenPluginLocation, boolean strictVersionChecking, ArtifactVersion bimserverVersion) {
    SPluginBundle pluginBundle = new SPluginBundle();
    boolean usefulBundle = false;
    for (PluginVersion pluginVersion : mavenPluginLocation.getAllVersions()) {
        if (pluginVersion instanceof MavenPluginVersion) {
            SPluginBundleVersion sPluginBundleVersion = new SPluginBundleVersion();
            boolean useful = true;
            MavenPluginVersion mavenPluginVersion = (MavenPluginVersion) pluginVersion;
            for (MavenDependency mavenDependency : mavenPluginVersion.getDependencies()) {
                if (mavenDependency.getArtifact().getGroupId().equals("org.opensourcebim")) {
                    String artifactId = mavenDependency.getArtifact().getArtifactId();
                    // for the plugin, it's version has to be ok
                    if (artifactId.equals("shared") || artifactId.equals("pluginbase")) {
                        VersionRange versionRange = VersionRange.createFromVersion(mavenDependency.getArtifact().getVersion());
                        if (bimserverVersion != null && versionRange.containsVersion(bimserverVersion)) {
                        } else {
                            sPluginBundleVersion.setMismatch(true);
                            if (strictVersionChecking) {
                                useful = false;
                                LOGGER.info("Skipping version " + mavenPluginVersion.getArtifact().getVersion() + " or artifact " + mavenPluginVersion.getArtifact().getArtifactId());
                            }
                        }
                    }
                }
            }
            if (useful) {
                usefulBundle = true;
                sPluginBundleVersion.setName(mavenPluginVersion.getModel().getName());
                sPluginBundleVersion.setOrganization(mavenPluginVersion.getModel().getOrganization().getName());
                sPluginBundleVersion.setArtifactId(mavenPluginLocation.getArtifactId());
                sPluginBundleVersion.setGroupId(mavenPluginLocation.getGroupId());
                try {
                    sPluginBundleVersion.setRepository(mavenPluginLocation.getRepository(mavenPluginVersion.getVersion().toString()));
                } catch (ArtifactResolutionException e) {
                    LOGGER.error("", e);
                }
                sPluginBundleVersion.setType(SPluginBundleType.MAVEN);
                sPluginBundleVersion.setVersion(mavenPluginVersion.getVersion().toString());
                sPluginBundleVersion.setDescription(mavenPluginVersion.getModel().getDescription());
                pluginBundle.setName(mavenPluginVersion.getModel().getName());
                pluginBundle.setOrganization(mavenPluginVersion.getModel().getOrganization().getName());
                pluginBundle.setLatestVersion(sPluginBundleVersion);
                pluginBundle.getAvailableVersions().add(sPluginBundleVersion);
                try {
                    sPluginBundleVersion.setIcon(mavenPluginLocation.getVersionIcon(mavenPluginVersion.getVersion().toString()));
                } catch (ArtifactResolutionException e) {
                // This is not important
                } catch (IOException e) {
                    LOGGER.error("", e);
                }
                try {
                    GregorianCalendar date = mavenPluginLocation.getVersionDate(mavenPluginVersion.getVersion().toString());
                    if (date != null) {
                        sPluginBundleVersion.setDate(date.getTime());
                    }
                // byte[] bytes = Files.readAllBytes(date);
                // Properties properties = new Properties();
                // properties.load(new ByteArrayInputStream(bytes));
                // String buildDateString = properties.getProperty("build.date");
                // 
                // DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
                // try {
                // } catch (ParseException e) {
                // LOGGER.error("Invalid date format for plugin " + mavenPluginVersion.getModel().getName() + ": '" + buildDateString + "'");
                // }
                } catch (ArtifactResolutionException e) {
                // Not a problem
                } catch (Exception e) {
                    LOGGER.error("", e);
                }
            }
        }
    }
    if (usefulBundle) {
        return pluginBundle;
    }
    return null;
}
Also used : SPluginBundleVersion(org.bimserver.interfaces.objects.SPluginBundleVersion) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenPluginVersion(org.bimserver.plugins.MavenPluginVersion) PluginVersion(org.bimserver.plugins.PluginVersion) MavenPluginVersion(org.bimserver.plugins.MavenPluginVersion) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) GregorianCalendar(java.util.GregorianCalendar) VersionRange(org.apache.maven.artifact.versioning.VersionRange) IOException(java.io.IOException) IOException(java.io.IOException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenDependency(org.bimserver.plugins.MavenDependency)

Example 5 with VersionRange

use of org.apache.maven.artifact.versioning.VersionRange in project felix by apache.

the class BundleAllPlugin method resolveArtifact.

private Artifact resolveArtifact(Artifact artifact) throws MojoExecutionException, ArtifactNotFoundException {
    VersionRange versionRange;
    if (artifact.getVersion() != null) {
        versionRange = VersionRange.createFromVersion(artifact.getVersion());
    } else {
        versionRange = artifact.getVersionRange();
    }
    /*
         * there's a bug with ArtifactFactory#createDependencyArtifact(String, String, VersionRange,
         * String, String, String) that ignores the scope parameter, that's why we use the one with
         * the extra null parameter
         */
    Artifact resolvedArtifact = m_factory.createDependencyArtifact(artifact.getGroupId(), artifact.getArtifactId(), versionRange, artifact.getType(), artifact.getClassifier(), artifact.getScope(), null);
    try {
        m_artifactResolver.resolve(resolvedArtifact, remoteRepositories, localRepository);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Error resolving artifact " + resolvedArtifact, e);
    }
    return resolvedArtifact;
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VersionRange(org.apache.maven.artifact.versioning.VersionRange) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

VersionRange (org.apache.maven.artifact.versioning.VersionRange)31 Artifact (org.apache.maven.artifact.Artifact)24 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)17 ArtifactHandler (org.apache.maven.artifact.handler.ArtifactHandler)17 File (java.io.File)13 DefaultArtifactHandlerStub (org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub)10 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)7 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)6 ArtifactVersion (org.apache.maven.artifact.versioning.ArtifactVersion)5 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)4 IOException (java.io.IOException)3 Map (java.util.Map)3 ArtifactMetadataRetrievalException (org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)3 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)3 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)3 Dependency (org.apache.maven.model.Dependency)3 Model (org.apache.maven.model.Model)3