Search in sources :

Example 21 with VersionRange

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

the class AbstractBundlePluginTest method getMavenProjectStub.

protected MavenProjectStub getMavenProjectStub() {
    MavenProjectStub project = new MavenProjectStub();
    project.setGroupId("group");
    project.setArtifactId("project");
    project.setVersion("1.2.3.4");
    VersionRange versionRange = VersionRange.createFromVersion(project.getVersion());
    ArtifactHandler artifactHandler = new DefaultArtifactHandler("pom");
    Artifact artifact = new DefaultArtifact(project.getGroupId(), project.getArtifactId(), versionRange, null, "pom", null, artifactHandler);
    artifact.setResolved(true);
    project.setArtifact(artifact);
    ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration();
    ArtifactRepositoryLayout layout = new LegacyRepositoryLayout();
    ArtifactRepository artifactRepository = new DefaultArtifactRepository("scratch", new File(getBasedir(), "target" + File.separatorChar + "scratch").toURI().toString(), layout);
    projectBuilderConfiguration.setLocalRepository(artifactRepository);
    project.setProjectBuilderConfiguration(projectBuilderConfiguration);
    return project;
}
Also used : ArtifactRepositoryLayout(org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout) DefaultProjectBuilderConfiguration(org.apache.maven.project.DefaultProjectBuilderConfiguration) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) LegacyRepositoryLayout(org.apache.maven.artifact.repository.layout.LegacyRepositoryLayout) VersionRange(org.apache.maven.artifact.versioning.VersionRange) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) Artifact(org.apache.maven.artifact.Artifact) ProjectBuilderConfiguration(org.apache.maven.project.ProjectBuilderConfiguration) DefaultProjectBuilderConfiguration(org.apache.maven.project.DefaultProjectBuilderConfiguration) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) MavenProjectStub(org.apache.maven.plugin.testing.stubs.MavenProjectStub) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 22 with VersionRange

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

the class AbstractBaselinePlugin method getPreviousArtifact.

private Artifact getPreviousArtifact() throws MojoFailureException, MojoExecutionException {
    // Find the previous version JAR and resolve it, and it's dependencies
    final VersionRange range;
    try {
        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") List<ArtifactVersion> // type is konwn
            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;
}
Also used : ArtifactMetadataRetrievalException(org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) OverConstrainedVersionException(org.apache.maven.artifact.versioning.OverConstrainedVersionException) VersionRange(org.apache.maven.artifact.versioning.VersionRange) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) Artifact(org.apache.maven.artifact.Artifact)

Example 23 with VersionRange

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

the class PluginManager method install.

public PluginBundle install(MavenPluginBundle mavenPluginBundle, List<SPluginInformation> plugins, boolean strictDependencyChecking) throws Exception {
    PluginBundleVersionIdentifier pluginBundleVersionIdentifier = mavenPluginBundle.getPluginVersionIdentifier();
    MavenXpp3Reader mavenreader = new MavenXpp3Reader();
    Model model = null;
    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);
}
Also used : Path(java.nio.file.Path) JarInputStream(java.util.jar.JarInputStream) FakeClosingInputStream(org.bimserver.utils.FakeClosingInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) SPluginInformation(org.bimserver.interfaces.objects.SPluginInformation) PluginException(org.bimserver.shared.exceptions.PluginException) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) VersionRange(org.apache.maven.artifact.versioning.VersionRange) JarEntry(java.util.jar.JarEntry) 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) FakeClosingInputStream(org.bimserver.utils.FakeClosingInputStream)

Example 24 with VersionRange

use of org.apache.maven.artifact.versioning.VersionRange in project maven-dependency-plugin 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 25 with VersionRange

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

the class TestDependencyUtil method testFileNameClassifier.

public void testFileNameClassifier() throws MojoExecutionException {
    ArtifactHandler ah = new DefaultArtifactHandlerStub("jar", "sources");
    VersionRange vr = VersionRange.createFromVersion("1.1-SNAPSHOT");
    Artifact artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "jar", "sources", ah, false);
    String name = DependencyUtil.getFormattedFileName(artifact, false);
    String expectedResult = "two-1.1-SNAPSHOT-sources.jar";
    assertEquals(expectedResult, name);
    name = DependencyUtil.getFormattedFileName(artifact, true);
    expectedResult = "two-sources.jar";
    assertEquals(expectedResult, name);
    name = DependencyUtil.getFormattedFileName(artifact, false, false, false, true);
    expectedResult = "two-1.1-SNAPSHOT.jar";
    assertEquals(expectedResult, name);
    ah = new DefaultArtifactHandlerStub("war", null);
    artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
    name = DependencyUtil.getFormattedFileName(artifact, true);
    expectedResult = "two.war";
    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) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) Artifact(org.apache.maven.artifact.Artifact) 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