Search in sources :

Example 46 with Dependency

use of org.apache.maven.model.Dependency 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 47 with Dependency

use of org.apache.maven.model.Dependency in project camel by apache.

the class BomGeneratorMojo method getProvidedDependencyManagement.

private Set<String> getProvidedDependencyManagement(String groupId, String artifactId, String version) throws Exception {
    Artifact bom = resolveArtifact(groupId, artifactId, version, "pom");
    MavenProject bomProject = loadExternalProjectPom(bom.getFile());
    Set<String> provided = new HashSet<>();
    if (bomProject.getDependencyManagement() != null && bomProject.getDependencyManagement().getDependencies() != null) {
        for (Dependency dep : bomProject.getDependencyManagement().getDependencies()) {
            provided.add(comparisonKey(dep));
        }
    }
    return provided;
}
Also used : MavenProject(org.apache.maven.project.MavenProject) Dependency(org.apache.maven.model.Dependency) Artifact(org.apache.maven.artifact.Artifact) HashSet(java.util.HashSet)

Example 48 with Dependency

use of org.apache.maven.model.Dependency in project camel by apache.

the class BomGeneratorMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        DependencyManagement mng = project.getDependencyManagement();
        List<Dependency> filteredDependencies = enhance(filter(mng.getDependencies()));
        Set<String> externallyManagedDependencies = getExternallyManagedDependencies();
        checkConflictsWithExternalBoms(filteredDependencies, externallyManagedDependencies);
        Document pom = loadBasePom();
        // transform
        overwriteDependencyManagement(pom, filteredDependencies);
        writePom(pom);
    } catch (MojoFailureException ex) {
        throw ex;
    } catch (MojoExecutionException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MojoExecutionException("Cannot generate the output BOM file", ex);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Dependency(org.apache.maven.model.Dependency) Document(org.w3c.dom.Document) DependencyManagement(org.apache.maven.model.DependencyManagement) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 49 with Dependency

use of org.apache.maven.model.Dependency in project camel by apache.

the class BomGeneratorMojo method filter.

private List<Dependency> filter(List<Dependency> dependencyList) {
    List<Dependency> outDependencies = new ArrayList<>();
    DependencyMatcher inclusions = new DependencyMatcher(dependencies.getIncludes());
    DependencyMatcher exclusions = new DependencyMatcher(dependencies.getExcludes());
    for (Dependency dep : dependencyList) {
        boolean accept = inclusions.matches(dep) && !exclusions.matches(dep);
        getLog().debug(dep + (accept ? " included in the BOM" : " excluded from BOM"));
        if (accept) {
            outDependencies.add(dep);
        }
    }
    Collections.sort(outDependencies, (d1, d2) -> (d1.getGroupId() + ":" + d1.getArtifactId()).compareTo(d2.getGroupId() + ":" + d2.getArtifactId()));
    return outDependencies;
}
Also used : ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency)

Example 50 with Dependency

use of org.apache.maven.model.Dependency in project grails-maven by grails.

the class AbstractGrailsMojo method findArtefactVersionFromPlugin.

private String findArtefactVersionFromPlugin(String group, String name) throws ProjectBuildingException {
    MavenProject pluginProject = getPluginProject();
    List<Dependency> dependencyArtifacts = pluginProject.getDependencies();
    if (dependencyArtifacts != null) {
        for (Dependency d : dependencyArtifacts) {
            if (d.getArtifactId().equals(name) && d.getGroupId().equals(group)) {
                return d.getVersion();
            }
        }
    }
    return null;
}
Also used : Dependency(org.apache.maven.model.Dependency)

Aggregations

Dependency (org.apache.maven.model.Dependency)78 ArrayList (java.util.ArrayList)24 Artifact (org.apache.maven.artifact.Artifact)18 File (java.io.File)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)11 MavenProject (org.apache.maven.project.MavenProject)8 IOException (java.io.IOException)7 Exclusion (org.apache.maven.model.Exclusion)7 MojoFailureException (org.apache.maven.plugin.MojoFailureException)7 WebappStructure (org.apache.maven.plugins.war.util.WebappStructure)7 HashMap (java.util.HashMap)6 DependencyManagement (org.apache.maven.model.DependencyManagement)5 Model (org.apache.maven.model.Model)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Plugin (org.apache.maven.model.Plugin)4 List (java.util.List)3 Properties (java.util.Properties)3 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)3