Search in sources :

Example 6 with VersionRange

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

the class SchemaDistMojo method getArtifact.

protected Artifact getArtifact(ArtifactItem artifactItem) throws MojoExecutionException, InvalidVersionSpecificationException {
    Artifact artifact;
    VersionRange vr = VersionRange.createFromVersionSpec(artifactItem.getVersion());
    if (StringUtils.isEmpty(artifactItem.getClassifier())) {
        artifact = factory.createDependencyArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, artifactItem.getType(), null, Artifact.SCOPE_COMPILE);
    } else {
        artifact = factory.createDependencyArtifact(artifactItem.getGroupId(), artifactItem.getArtifactId(), vr, artifactItem.getType(), artifactItem.getClassifier(), Artifact.SCOPE_COMPILE);
    }
    try {
        resolver.resolve(artifact, remoteRepos, local);
    } catch (ArtifactResolutionException | ArtifactNotFoundException e) {
        throw new MojoExecutionException("Error resolving artifact " + artifact, e);
    }
    return artifact;
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) 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)

Example 7 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) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) 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 8 with VersionRange

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

the class ProcessRemoteResourcesMojo method getProjects.

@SuppressWarnings("unchecked")
protected List<MavenProject> getProjects() throws MojoExecutionException {
    List<MavenProject> projects = new ArrayList<MavenProject>();
    // add filters in well known order, least specific to most specific
    FilterArtifacts filter = new FilterArtifacts();
    Set<Artifact> artifacts = resolveProjectArtifacts();
    if (this.excludeTransitive) {
        Set<Artifact> depArtifacts;
        if (runOnlyAtExecutionRoot) {
            depArtifacts = aggregateProjectDependencyArtifacts();
        } else {
            depArtifacts = project.getDependencyArtifacts();
        }
        filter.addFilter(new ProjectTransitivityFilter(depArtifacts, true));
    }
    filter.addFilter(new ScopeFilter(this.includeScope, this.excludeScope));
    filter.addFilter(new GroupIdFilter(this.includeGroupIds, this.excludeGroupIds));
    filter.addFilter(new ArtifactIdFilter(this.includeArtifactIds, this.excludeArtifactIds));
    // perform filtering
    try {
        artifacts = filter.filter(artifacts);
    } catch (ArtifactFilterException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    for (Artifact artifact : artifacts) {
        try {
            List<ArtifactRepository> remoteRepo = remoteArtifactRepositories;
            if (artifact.isSnapshot()) {
                VersionRange rng = VersionRange.createFromVersion(artifact.getBaseVersion());
                artifact = artifactFactory.createDependencyArtifact(artifact.getGroupId(), artifact.getArtifactId(), rng, artifact.getType(), artifact.getClassifier(), artifact.getScope(), null, artifact.isOptional());
            }
            getLog().debug("Building project for " + artifact);
            MavenProject p;
            try {
                p = mavenProjectBuilder.buildFromRepository(artifact, remoteRepo, localRepository);
            } catch (InvalidProjectModelException e) {
                getLog().warn("Invalid project model for artifact [" + artifact.getArtifactId() + ":" + artifact.getGroupId() + ":" + artifact.getVersion() + "]. " + "It will be ignored by the remote resources Mojo.");
                continue;
            }
            String supplementKey = generateSupplementMapKey(p.getModel().getGroupId(), p.getModel().getArtifactId());
            if (supplementModels.containsKey(supplementKey)) {
                Model mergedModel = mergeModels(p.getModel(), supplementModels.get(supplementKey));
                MavenProject mergedProject = new MavenProject(mergedModel);
                projects.add(mergedProject);
                mergedProject.setArtifact(artifact);
                mergedProject.setVersion(artifact.getVersion());
                getLog().debug("Adding project with groupId [" + mergedProject.getGroupId() + "] (supplemented)");
            } else {
                projects.add(p);
                getLog().debug("Adding project with groupId [" + p.getGroupId() + "]");
            }
        } catch (ProjectBuildingException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    Collections.sort(projects, new ProjectComparator());
    return projects;
}
Also used : InvalidProjectModelException(org.apache.maven.project.InvalidProjectModelException) ArtifactIdFilter(org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ProjectTransitivityFilter(org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) VersionRange(org.apache.maven.artifact.versioning.VersionRange) Artifact(org.apache.maven.artifact.Artifact) ArtifactFilterException(org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException) ScopeFilter(org.apache.maven.shared.artifact.filter.collection.ScopeFilter) MavenProject(org.apache.maven.project.MavenProject) FilterArtifacts(org.apache.maven.shared.artifact.filter.collection.FilterArtifacts) Model(org.apache.maven.model.Model) GroupIdFilter(org.apache.maven.shared.artifact.filter.collection.GroupIdFilter)

Example 9 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)

Example 10 with VersionRange

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

the class TestDependencyUtil method testTestJar.

public void testTestJar() {
    ArtifactHandler ah = new DefaultArtifactHandlerStub("test-jar", null);
    VersionRange vr = VersionRange.createFromVersion("1.1-SNAPSHOT");
    Artifact artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "test-jar", null, ah, false);
    String name = DependencyUtil.getFormattedFileName(artifact, false);
    String expectedResult = "two-1.1-SNAPSHOT.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) 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)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