Search in sources :

Example 1 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project randomizedtesting by randomizedtesting.

the class JUnit4Mojo method setupTestClasspath.

/**
   * Setup the classpath used for tests.
   */
private void setupTestClasspath(Element junit4) {
    junit4.addComment("Runtime classpath.");
    Element cp = junit4.addElement("classpath");
    // Test classes.
    cp.addComment("Test classes directory.");
    cp.addElement("pathelement").addAttribute("location", testClassesDirectory.getAbsolutePath());
    // Classes directory.
    cp.addComment("Test classes directory.");
    cp.addElement("pathelement").addAttribute("location", classesDirectory.getAbsolutePath());
    // Project dependencies.
    cp.addComment("Project dependencies.");
    Set<Artifact> classpathArtifacts = (Set<Artifact>) project.getArtifacts();
    if (!Strings.isNullOrEmpty(classpathDependencyScopeExclude)) {
        classpathArtifacts = filterArtifacts(cp, classpathArtifacts, new ScopeArtifactFilter(classpathDependencyScopeExclude));
    }
    if (classpathDependencyExcludes != null && !classpathDependencyExcludes.isEmpty()) {
        classpathArtifacts = filterArtifacts(cp, classpathArtifacts, new PatternIncludesArtifactFilter(classpathDependencyExcludes));
    }
    for (Artifact artifact : classpathArtifacts) {
        if (artifact.getArtifactHandler().isAddedToClasspath()) {
            File file = artifact.getFile();
            if (file != null) {
                cp.addComment("Dependency artifact: " + artifact.getId());
                cp.addElement("pathelement").addAttribute("location", file.getAbsolutePath());
            }
        }
    }
    // Additional dependencies.
    cp.addComment("Additional classpath elements.");
    if (additionalClasspathElements != null && !additionalClasspathElements.isEmpty()) {
        for (String classpathElement : additionalClasspathElements) {
            if (!Strings.isNullOrEmpty(classpathElement)) {
                cp.addElement("pathelement").addAttribute("location", classpathElement);
            }
        }
    }
}
Also used : ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) Element(org.dom4j.Element) PatternIncludesArtifactFilter(org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 2 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project camel by apache.

the class RunMojo method determineRelevantPluginDependencies.

/**
     * Determine all plugin dependencies relevant to the executable. Takes
     * includePlugins, and the executableDependency into consideration.
     *
     * @return a set of Artifact objects. (Empty set is returned if there are no
     *         relevant plugin dependencies.)
     * @throws MojoExecutionException
     */
private Set<Artifact> determineRelevantPluginDependencies() throws MojoExecutionException {
    Set<Artifact> relevantDependencies;
    if (this.includePluginDependencies) {
        if (this.executableDependency == null) {
            getLog().debug("All Plugin Dependencies will be included.");
            relevantDependencies = new HashSet<Artifact>(this.pluginDependencies);
        } else {
            getLog().debug("Selected plugin Dependencies will be included.");
            Artifact executableArtifact = this.findExecutableArtifact();
            Artifact executablePomArtifact = this.getExecutablePomArtifact(executableArtifact);
            relevantDependencies = this.resolveExecutableDependencies(executablePomArtifact, false);
        }
    } else {
        getLog().debug("Only Direct Plugin Dependencies will be included.");
        PluginDescriptor descriptor = (PluginDescriptor) getPluginContext().get("pluginDescriptor");
        try {
            relevantDependencies = artifactResolver.resolveTransitively(MavenMetadataSource.createArtifacts(this.artifactFactory, descriptor.getPlugin().getDependencies(), null, null, null), this.project.getArtifact(), Collections.emptyMap(), this.localRepository, this.remoteRepositories, metadataSource, new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME), Collections.emptyList()).getArtifacts();
        } catch (Exception ex) {
            throw new MojoExecutionException("Encountered problems resolving dependencies of the plugin " + "in preparation for its execution.", ex);
        }
    }
    return relevantDependencies;
}
Also used : PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Artifact(org.apache.maven.artifact.Artifact) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 3 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project camel by apache.

the class RunMojo method resolveExecutableDependencies.

private Set<Artifact> resolveExecutableDependencies(Artifact executablePomArtifact, boolean ignoreFailures) throws MojoExecutionException {
    Set<Artifact> executableDependencies = null;
    try {
        MavenProject executableProject = this.projectBuilder.buildFromRepository(executablePomArtifact, this.remoteRepositories, this.localRepository);
        // get all of the dependencies for the executable project
        List<Artifact> dependencies = CastUtils.cast(executableProject.getDependencies());
        // make Artifacts of all the dependencies
        Set<Artifact> dependencyArtifacts = CastUtils.cast(MavenMetadataSource.createArtifacts(this.artifactFactory, dependencies, null, null, null));
        // not forgetting the Artifact of the project itself
        dependencyArtifacts.add(executableProject.getArtifact());
        // resolve runtime dependencies transitively to obtain a comprehensive list of assemblies
        ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts, executablePomArtifact, Collections.emptyMap(), this.localRepository, this.remoteRepositories, metadataSource, new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME), Collections.emptyList());
        executableDependencies = CastUtils.cast(result.getArtifacts());
    } catch (Exception ex) {
        if (ignoreFailures) {
            getLog().debug("Ignoring maven resolving dependencies failure " + ex.getMessage());
        } else {
            throw new MojoExecutionException("Encountered problems resolving dependencies of the executable " + "in preparation for its execution.", ex);
        }
    }
    return executableDependencies;
}
Also used : ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactResolutionResult(org.apache.maven.artifact.resolver.ArtifactResolutionResult) Artifact(org.apache.maven.artifact.Artifact) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 4 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project camel by apache.

the class SpringBootStarterMojo method filterIncludedArtifacts.

private Set<String> filterIncludedArtifacts(Set<String> artifacts) throws DependencyTreeBuilderException {
    Set<String> included = new TreeSet<>();
    ArtifactFilter artifactFilter = new ScopeArtifactFilter(null);
    DependencyNode node = treeBuilder.buildDependencyTree(project, localRepository, artifactFactory, artifactMetadataSource, artifactFilter, artifactCollector);
    CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
    node.accept(visitor);
    List<DependencyNode> nodes = visitor.getNodes();
    for (DependencyNode dependencyNode : nodes) {
        Artifact artifact = dependencyNode.getArtifact();
        getLog().debug("Found dependency node: " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + " - scope=" + artifact.getScope());
        if (!Artifact.SCOPE_TEST.equals(artifact.getScope()) && !Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) {
            String canonicalName = artifact.getGroupId() + ":" + artifact.getArtifactId();
            if (artifacts.contains(canonicalName)) {
                getLog().debug(canonicalName + " marked for exclusion");
                included.add(canonicalName);
            }
        }
    }
    return included;
}
Also used : ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) TreeSet(java.util.TreeSet) DependencyNode(org.apache.maven.shared.dependency.tree.DependencyNode) Artifact(org.apache.maven.artifact.Artifact)

Example 5 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project maven-plugins by apache.

the class TreeMojo method createResolvingArtifactFilter.

// private methods --------------------------------------------------------
/**
     * Gets the artifact filter to use when resolving the dependency tree.
     *
     * @return the artifact filter
     */
private ArtifactFilter createResolvingArtifactFilter() {
    ArtifactFilter filter;
    // filter scope
    if (scope != null) {
        getLog().debug("+ Resolving dependency tree for scope '" + scope + "'");
        filter = new ScopeArtifactFilter(scope);
    } else {
        filter = null;
    }
    return filter;
}
Also used : StrictPatternExcludesArtifactFilter(org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) StrictPatternIncludesArtifactFilter(org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)

Aggregations

ScopeArtifactFilter (org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)20 Artifact (org.apache.maven.artifact.Artifact)19 File (java.io.File)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)2 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 MavenArchiver (org.apache.maven.archiver.MavenArchiver)1 PomPropertiesUtil (org.apache.maven.archiver.PomPropertiesUtil)1 ArtifactResolutionResult (org.apache.maven.artifact.resolver.ArtifactResolutionResult)1 PluginDescriptor (org.apache.maven.plugin.descriptor.PluginDescriptor)1 ArtifactTypeMappingService (org.apache.maven.plugins.ear.util.ArtifactTypeMappingService)1 JavaEEVersion (org.apache.maven.plugins.ear.util.JavaEEVersion)1 MavenProject (org.apache.maven.project.MavenProject)1