Search in sources :

Example 26 with Artifact

use of org.apache.maven.artifact.Artifact in project camel by apache.

the class RunMojo method addRelevantPluginDependenciesToClasspath.

/**
     * Add any relevant project dependencies to the classpath. Indirectly takes
     * includePluginDependencies and ExecutableDependency into consideration.
     *
     * @param path classpath of {@link java.net.URL} objects
     * @throws MojoExecutionException
     */
private void addRelevantPluginDependenciesToClasspath(Set<URL> path) throws MojoExecutionException {
    if (hasCommandlineArgs()) {
        arguments = parseCommandlineArgs();
    }
    try {
        Iterator<Artifact> iter = this.determineRelevantPluginDependencies().iterator();
        while (iter.hasNext()) {
            Artifact classPathElement = iter.next();
            // java.lang.NoClassDefFoundError: org.osgi.vendor.framework property not set
            if (classPathElement.getArtifactId().equals("org.osgi.core")) {
                getLog().debug("Skipping org.osgi.core -> " + classPathElement.getGroupId() + "/" + classPathElement.getArtifactId() + "/" + classPathElement.getVersion());
                continue;
            }
            getLog().debug("Adding plugin dependency artifact: " + classPathElement.getArtifactId() + " to classpath");
            path.add(classPathElement.getFile().toURI().toURL());
        }
    } catch (MalformedURLException e) {
        throw new MojoExecutionException("Error during setting up classpath", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Artifact(org.apache.maven.artifact.Artifact)

Example 27 with Artifact

use of org.apache.maven.artifact.Artifact 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 28 with Artifact

use of org.apache.maven.artifact.Artifact 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 29 with Artifact

use of org.apache.maven.artifact.Artifact in project camel by apache.

the class RunMojo method addRelevantProjectDependenciesToClasspath.

/**
     * Add any relevant project dependencies to the classpath. Takes
     * includeProjectDependencies into consideration.
     *
     * @param path classpath of {@link java.net.URL} objects
     * @throws org.apache.maven.plugin.MojoExecutionException
     */
private void addRelevantProjectDependenciesToClasspath(List<URL> path) throws MojoExecutionException {
    if (this.includeProjectDependencies) {
        try {
            getLog().debug("Project Dependencies will be included.");
            URL mainClasses = new File(project.getBuild().getOutputDirectory()).toURI().toURL();
            getLog().debug("Adding to classpath : " + mainClasses);
            path.add(mainClasses);
            Set<Artifact> dependencies = CastUtils.cast(project.getArtifacts());
            // system scope dependencies are not returned by maven 2.0. See
            // MEXEC-17
            dependencies.addAll(getAllNonTestScopedDependencies());
            Iterator<Artifact> iter = dependencies.iterator();
            while (iter.hasNext()) {
                Artifact classPathElement = iter.next();
                getLog().debug("Adding project dependency artifact: " + classPathElement.getArtifactId() + " to classpath");
                File file = classPathElement.getFile();
                if (file != null) {
                    path.add(file.toURI().toURL());
                }
            }
        } catch (MalformedURLException e) {
            throw new MojoExecutionException("Error during setting up classpath", e);
        }
    } else {
        getLog().debug("Project Dependencies will be excluded.");
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) URL(java.net.URL) Artifact(org.apache.maven.artifact.Artifact)

Example 30 with Artifact

use of org.apache.maven.artifact.Artifact 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) 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)

Aggregations

Artifact (org.apache.maven.artifact.Artifact)450 File (java.io.File)175 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)92 ArrayList (java.util.ArrayList)91 MavenProject (org.apache.maven.project.MavenProject)63 IOException (java.io.IOException)50 HashSet (java.util.HashSet)42 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)32 LinkedHashSet (java.util.LinkedHashSet)29 MojoFailureException (org.apache.maven.plugin.MojoFailureException)24 HashMap (java.util.HashMap)22 Set (java.util.Set)22 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)22 ScopeArtifactFilter (org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)21 URL (java.net.URL)20 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)20 Test (org.junit.Test)20 MalformedURLException (java.net.MalformedURLException)18 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)17 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)16