Search in sources :

Example 26 with VersionRange

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

the class PluginBundleManager method loadDependencies.

private void loadDependencies(String pluginBundleVersion, boolean strictDependencyChecking, Model model, DelegatingClassLoader delegatingClassLoader) throws DependencyCollectionException, InvalidVersionSpecificationException, Exception {
    if (model.getRepositories() != null) {
        for (Repository repository : model.getRepositories()) {
            mavenPluginRepository.addRepository(repository.getId(), "default", repository.getUrl());
        }
    }
    List<Dependency> dependenciesToResolve = new ArrayList<>();
    for (org.apache.maven.model.Dependency mvnDependency : model.getDependencies()) {
        String scope = mvnDependency.getScope();
        if (scope != null && (scope.contentEquals("test"))) {
            // Skip
            continue;
        }
        Dependency d = new Dependency(new DefaultArtifact(mvnDependency.getGroupId(), mvnDependency.getArtifactId(), mvnDependency.getType(), mvnDependency.getVersion()), scope);
        Set<Exclusion> exclusions = new HashSet<>();
        d.setExclusions(exclusions);
        exclusions.add(new Exclusion("org.opensourcebim", "pluginbase", null, "jar"));
        exclusions.add(new Exclusion("org.opensourcebim", "shared", null, "jar"));
        exclusions.add(new Exclusion("org.opensourcebim", "ifcplugins", null, "jar"));
        if (!isDependencyProvided(mvnDependency))
            dependenciesToResolve.add(d);
    }
    CollectRequest collectRequest = new CollectRequest(dependenciesToResolve, null, null);
    collectRequest.setRepositories(mavenPluginRepository.getRepositoriesAsList());
    CollectResult collectDependencies = mavenPluginRepository.getSystem().collectDependencies(mavenPluginRepository.getSession(), collectRequest);
    PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
    DependencyNode rootDep = collectDependencies.getRoot();
    rootDep.accept(nlg);
    for (Dependency dependency : nlg.getDependencies(true)) {
        if (dependency.getScope().contentEquals("test")) {
            continue;
        }
        // LOGGER.info(dependency.getArtifact().getGroupId() + "." + dependency.getArtifact().getArtifactId());
        Artifact dependencyArtifact = dependency.getArtifact();
        PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependencyArtifact.getGroupId(), dependencyArtifact.getArtifactId());
        if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
            if (strictDependencyChecking) {
                String version = dependencyArtifact.getVersion();
                if (!version.contains("[") && !version.contains("(")) {
                    version = "[" + version + "]";
                }
                VersionRange versionRange = VersionRange.createFromVersionSpec(version);
                // String version =
                // pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
                ArtifactVersion artifactVersion = new DefaultArtifactVersion(pluginBundleVersion);
                if (versionRange.containsVersion(artifactVersion)) {
                // OK
                } else {
                    throw new Exception("Required dependency " + pluginBundleIdentifier + " is installed, but it's version (" + pluginBundleVersion + ") does not comply to the required version (" + dependencyArtifact.getVersion() + ")");
                }
            } else {
                LOGGER.info("Skipping strict dependency checking for dependency " + dependencyArtifact.getArtifactId());
            }
        } else {
            try {
                if (dependencyArtifact.getGroupId().contentEquals("com.sun.xml.ws")) {
                    continue;
                }
                MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(dependencyArtifact.getGroupId(), dependencyArtifact.getArtifactId());
                if (dependencyArtifact.getExtension().contentEquals("jar")) {
                    Path depJarFile = mavenPluginLocation.getVersionJar(dependencyArtifact.getVersion());
                    FileJarClassLoader jarClassLoader = new FileJarClassLoader(pluginManager, delegatingClassLoader, depJarFile);
                    jarClassLoaders.add(jarClassLoader);
                    delegatingClassLoader.add(jarClassLoader);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new Exception("Required dependency " + pluginBundleIdentifier + " is not installed");
            }
        }
    }
}
Also used : VersionRange(org.apache.maven.artifact.versioning.VersionRange) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) FileJarClassLoader(org.bimserver.plugins.classloaders.FileJarClassLoader) Exclusion(org.eclipse.aether.graph.Exclusion) DependencyNode(org.eclipse.aether.graph.DependencyNode) Path(java.nio.file.Path) CollectResult(org.eclipse.aether.collection.CollectResult) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) PluginException(org.bimserver.shared.exceptions.PluginException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) IOException(java.io.IOException) Repository(org.apache.maven.model.Repository) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 27 with VersionRange

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

the class DependencyManagementRenderer method getDependencyRow.

@SuppressWarnings("unchecked")
private String[] getDependencyRow(Dependency dependency, boolean hasClassifier) {
    Artifact artifact = artifactFactory.createProjectArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
    StringBuilder licensesBuffer = new StringBuilder();
    String url = null;
    try {
        VersionRange range = VersionRange.createFromVersionSpec(dependency.getVersion());
        if (range.getRecommendedVersion() == null) {
            // MPIR-216: no direct version but version range: need to choose one precise version
            log.debug("Resolving range for DependencyManagement on " + artifact.getId());
            List<ArtifactVersion> versions = artifactMetadataSource.retrieveAvailableVersions(artifact, localRepository, remoteRepositories);
            // only use versions from range
            for (Iterator<ArtifactVersion> iter = versions.iterator(); iter.hasNext(); ) {
                if (!range.containsVersion(iter.next())) {
                    iter.remove();
                }
            }
            // select latest, assuming pom information will be the most accurate
            if (versions.size() > 0) {
                ArtifactVersion maxArtifactVersion = Collections.max(versions);
                artifact.setVersion(maxArtifactVersion.toString());
                log.debug("DependencyManagement resolved: " + artifact.getId());
            }
        }
        url = ProjectInfoReportUtils.getArtifactUrl(artifactFactory, artifact, mavenProjectBuilder, remoteRepositories, localRepository);
        MavenProject artifactProject = repoUtils.getMavenProjectFromRepository(artifact);
        List<License> licenses = artifactProject.getLicenses();
        for (License license : licenses) {
            String licenseCell = ProjectInfoReportUtils.getArtifactIdCell(license.getName(), license.getUrl());
            if (licensesBuffer.length() > 0) {
                licensesBuffer.append(", ");
            }
            licensesBuffer.append(licenseCell);
        }
    } catch (InvalidVersionSpecificationException e) {
        log.warn("Unable to parse version for " + artifact.getId(), e);
    } catch (ArtifactMetadataRetrievalException e) {
        log.warn("Unable to retrieve versions for " + artifact.getId() + " from repository.", e);
    } catch (ProjectBuildingException e) {
        log.warn("Unable to create Maven project for " + artifact.getId() + " from repository.", e);
    }
    String artifactIdCell = ProjectInfoReportUtils.getArtifactIdCell(artifact.getArtifactId(), url);
    if (hasClassifier) {
        return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(), dependency.getClassifier(), dependency.getType(), licensesBuffer.toString() };
    }
    return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(), dependency.getType(), licensesBuffer.toString() };
}
Also used : ArtifactMetadataRetrievalException(org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) License(org.apache.maven.model.License) VersionRange(org.apache.maven.artifact.versioning.VersionRange) Artifact(org.apache.maven.artifact.Artifact) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) MavenProject(org.apache.maven.project.MavenProject)

Example 28 with VersionRange

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

the class TestSourcesMarkerFileHandler method setUp.

protected void setUp() throws Exception {
    super.setUp();
    ArtifactHandler ah = new DefaultArtifactHandler();
    VersionRange vr = VersionRange.createFromVersion("1.1");
    Artifact artifact = new DefaultArtifact("test", "1", vr, Artifact.SCOPE_COMPILE, "jar", "", ah, false);
    artifacts.add(artifact);
    artifact = new DefaultArtifact("test", "2", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
    artifacts.add(artifact);
    artifact = new DefaultArtifact("test", "3", vr, Artifact.SCOPE_TEST, "sources", "", ah, false);
    artifacts.add(artifact);
    artifact = new DefaultArtifact("test", "4", vr, Artifact.SCOPE_RUNTIME, "zip", "", ah, false);
    artifacts.add(artifact);
    // pick random output location
    Random a = new Random();
    outputFolder = new File("target/markers" + a.nextLong() + "/");
    outputFolder.delete();
    assertFalse(outputFolder.exists());
}
Also used : DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) Random(java.util.Random) VersionRange(org.apache.maven.artifact.versioning.VersionRange) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 29 with VersionRange

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

the class TestDependencyUtil method testFileNameClassifierWithFile.

public void testFileNameClassifierWithFile() throws MojoExecutionException {
    // specifically testing the default operation that getFormattedFileName
    // returns
    // the actual name of the file if available unless remove version is
    // set.
    ArtifactHandler ah = new DefaultArtifactHandlerStub("war", "sources");
    VersionRange vr = VersionRange.createFromVersion("1.1-SNAPSHOT");
    Artifact artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", "sources", ah, false);
    File file = new File("/target", "test-file-name.jar");
    artifact.setFile(file);
    String name = DependencyUtil.getFormattedFileName(artifact, false);
    String expectedResult = "two-1.1-SNAPSHOT-sources.war";
    assertEquals(expectedResult, name);
    name = DependencyUtil.getFormattedFileName(artifact, false, false, false, true);
    expectedResult = "two-1.1-SNAPSHOT.war";
    assertEquals(expectedResult, name);
    name = DependencyUtil.getFormattedFileName(artifact, true);
    expectedResult = "two-sources.war";
    assertEquals(expectedResult, name);
    artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
    name = DependencyUtil.getFormattedFileName(artifact, true);
    expectedResult = "two.war";
    assertEquals(expectedResult, name);
    // test that we pickup the correct extension in the file name if set.
    ah = new DefaultArtifactHandlerStub("jar", null);
    artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
    name = DependencyUtil.getFormattedFileName(artifact, true);
    expectedResult = "two.jar";
    assertEquals(expectedResult, name);
}
Also used : ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) DefaultArtifactHandlerStub(org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub) VersionRange(org.apache.maven.artifact.versioning.VersionRange) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 30 with VersionRange

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

the class TestDependencyUtil method setUp.

protected void setUp() throws Exception {
    super.setUp();
    ArtifactHandler ah = new DefaultArtifactHandlerStub("jar", null);
    VersionRange vr = VersionRange.createFromVersion("1.1");
    release = new DefaultArtifact("test", "one", vr, Artifact.SCOPE_COMPILE, "jar", "sources", ah, false);
    artifacts.add(release);
    ah = new DefaultArtifactHandlerStub("war", null);
    vr = VersionRange.createFromVersion("1.1-SNAPSHOT");
    snap = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", null, ah, false);
    artifacts.add(snap);
    ah = new DefaultArtifactHandlerStub("war", null);
    vr = VersionRange.createFromVersion("1.1-SNAPSHOT");
    snapResolvedVersion = new DefaultArtifact("test", "three", vr, Artifact.SCOPE_PROVIDED, "war", null, ah, false);
    snapResolvedVersion.setResolvedVersion("1.1-20121003.035531-117");
    artifacts.add(snapResolvedVersion);
    ah = new DefaultArtifactHandlerStub("war", null);
    vr = VersionRange.createFromVersion("1.1-SNAPSHOT");
    sources = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "sources", "sources", ah, false);
    // pick random output location
    Random a = new Random();
    outputFolder = new File("target/copy" + a.nextLong() + "/");
    outputFolder.delete();
    assertFalse(outputFolder.exists());
}
Also used : ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) Random(java.util.Random) DefaultArtifactHandlerStub(org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub) VersionRange(org.apache.maven.artifact.versioning.VersionRange) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Aggregations

VersionRange (org.apache.maven.artifact.versioning.VersionRange)33 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 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)9 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)7 ArtifactVersion (org.apache.maven.artifact.versioning.ArtifactVersion)7 IOException (java.io.IOException)5 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)5 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)5 FileNotFoundException (java.io.FileNotFoundException)4 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 JAXBException (javax.xml.bind.JAXBException)4 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)4 Model (org.apache.maven.model.Model)4