Search in sources :

Example 11 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 12 with VersionRange

use of org.apache.maven.artifact.versioning.VersionRange in project maven-plugins 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)

Example 13 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 14 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 15 with VersionRange

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

the class AbstractBundleListMojo method getArtifact.

/**
     * Get a resolved Artifact from the coordinates provided
     *
     * @return the artifact, which has been resolved.
     * @throws MojoExecutionException
     */
protected Artifact getArtifact(String groupId, String artifactId, String version, String type, String classifier) throws MojoExecutionException {
    Artifact artifact;
    VersionRange vr;
    try {
        vr = VersionRange.createFromVersionSpec(version);
    } catch (InvalidVersionSpecificationException e) {
        vr = VersionRange.createFromVersion(version);
    }
    if (StringUtils.isEmpty(classifier)) {
        artifact = factory.createDependencyArtifact(groupId, artifactId, vr, type, null, Artifact.SCOPE_COMPILE);
    } else {
        artifact = factory.createDependencyArtifact(groupId, artifactId, vr, type, classifier, Artifact.SCOPE_COMPILE);
    }
    // This code kicks in when the version specifier is a range.
    if (vr.getRecommendedVersion() == null) {
        try {
            List<ArtifactVersion> availVersions = metadataSource.retrieveAvailableVersions(artifact, local, remoteRepos);
            ArtifactVersion resolvedVersion = vr.matchVersion(availVersions);
            artifact.setVersion(resolvedVersion.toString());
        } catch (ArtifactMetadataRetrievalException e) {
            throw new MojoExecutionException("Unable to find version for artifact", e);
        }
    }
    try {
        resolver.resolve(artifact, remoteRepos, 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;
}
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) VersionRange(org.apache.maven.artifact.versioning.VersionRange) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

VersionRange (org.apache.maven.artifact.versioning.VersionRange)15 Artifact (org.apache.maven.artifact.Artifact)13 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)7 ArtifactHandler (org.apache.maven.artifact.handler.ArtifactHandler)7 File (java.io.File)6 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 DefaultArtifactHandlerStub (org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub)5 ArrayList (java.util.ArrayList)3 Dependency (org.apache.maven.model.Dependency)3 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)3 Random (java.util.Random)2 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)2 ArtifactMetadataRetrievalException (org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)2 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)2 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)2 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)2 ExcludesArtifactFilter (org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter)2 ArtifactVersion (org.apache.maven.artifact.versioning.ArtifactVersion)2 Exclusion (org.apache.maven.model.Exclusion)2