Search in sources :

Example 1 with ProjectBuildingResult

use of org.apache.maven.project.ProjectBuildingResult in project evosuite by EvoSuite.

the class EvoSuiteRunner method getCommandToRunEvoSuite.

/**
 * We run the EvoSuite that is provided with the plugin
 *
 * @return
 */
private List<String> getCommandToRunEvoSuite() {
    logger.debug("EvoSuite Maven Plugin Artifacts: " + Arrays.toString(artifacts.toArray()));
    Artifact evosuite = null;
    for (Artifact art : artifacts) {
        // first find the main EvoSuite jar among the dependencies
        if (art.getArtifactId().equals("evosuite-master")) {
            evosuite = art;
            break;
        }
    }
    if (evosuite == null) {
        logger.error("CRITICAL ERROR: plugin can detect EvoSuite executable");
        return null;
    }
    logger.debug("EvoSuite located at: " + evosuite.getFile());
    /*
		 * now, build a project descriptor for evosuite, which is needed to
		 * query all of its dependencies 
		 */
    DefaultProjectBuildingRequest req = new DefaultProjectBuildingRequest();
    req.setRepositorySession(repoSession);
    req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
    req.setSystemProperties(System.getProperties());
    req.setResolveDependencies(true);
    ProjectBuildingResult res;
    try {
        res = projectBuilder.build(evosuite, req);
    } catch (ProjectBuildingException e) {
        logger.error("Failed: " + e.getMessage(), e);
        return null;
    }
    // build the classpath to run EvoSuite
    String cp = evosuite.getFile().getAbsolutePath();
    for (Artifact dep : res.getProject().getArtifacts()) {
        cp += File.pathSeparator + dep.getFile().getAbsolutePath();
    }
    logger.debug("EvoSuite classpath: " + cp);
    String entryPoint = EvoSuite.class.getName();
    List<String> cmd = new ArrayList<>();
    cmd.add(JavaExecCmdUtil.getJavaBinExecutablePath());
    cmd.add("-D" + LoggingUtils.USE_DIFFERENT_LOGGING_XML_PARAMETER + "=logback-ctg-entry.xml");
    cmd.add("-Dlogback.configurationFile=logback-ctg-entry.xml");
    cmd.add("-cp");
    cmd.add(cp);
    cmd.add(entryPoint);
    return cmd;
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) ArrayList(java.util.ArrayList) Artifact(org.apache.maven.artifact.Artifact) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 2 with ProjectBuildingResult

use of org.apache.maven.project.ProjectBuildingResult in project spring-cloud-function by spring-cloud.

the class DependencyResolutionModule method dependencies.

public List<Dependency> dependencies(final Resource resource, final Properties properties) {
    initialize();
    try {
        ProjectBuildingRequest request = getProjectBuildingRequest(properties);
        request.setResolveDependencies(true);
        synchronized (DependencyResolver.class) {
            ProjectBuildingResult result = projectBuilder.build(new PropertiesModelSource(properties, resource), request);
            DependencyResolver.globals = null;
            DependencyResolutionResult dependencies = result.getDependencyResolutionResult();
            if (!dependencies.getUnresolvedDependencies().isEmpty()) {
                StringBuilder builder = new StringBuilder();
                for (Dependency dependency : dependencies.getUnresolvedDependencies()) {
                    List<Exception> errors = dependencies.getResolutionErrors(dependency);
                    for (Exception exception : errors) {
                        if (builder.length() > 0) {
                            builder.append("\n");
                        }
                        builder.append(exception.getMessage());
                    }
                }
                throw new RuntimeException(builder.toString());
            }
            return runtime(dependencies.getDependencies());
        }
    } catch (ProjectBuildingException | NoLocalRepositoryManagerException e) {
        throw new IllegalStateException("Cannot build model", e);
    }
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) Dependency(org.eclipse.aether.graph.Dependency) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) NoLocalRepositoryManagerException(org.eclipse.aether.repository.NoLocalRepositoryManagerException) IOException(java.io.IOException) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) NoLocalRepositoryManagerException(org.eclipse.aether.repository.NoLocalRepositoryManagerException)

Example 3 with ProjectBuildingResult

use of org.apache.maven.project.ProjectBuildingResult in project spring-cloud-function by spring-cloud.

the class DependencyResolutionModule method readModel.

public Model readModel(final Resource resource, final Properties properties) {
    initialize();
    try {
        ProjectBuildingRequest request = getProjectBuildingRequest(properties);
        request.setResolveDependencies(false);
        ProjectBuildingResult result = projectBuilder.build(new PropertiesModelSource(properties, resource), request);
        return result.getProject().getModel();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to build model from effective pom", e);
    }
}
Also used : DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) NoLocalRepositoryManagerException(org.eclipse.aether.repository.NoLocalRepositoryManagerException) IOException(java.io.IOException)

Example 4 with ProjectBuildingResult

use of org.apache.maven.project.ProjectBuildingResult in project tycho by eclipse.

the class TargetPlatformFilterConfigurationReaderTest method buildProjectModel.

// TODO share this?
MavenProject buildProjectModel(File pom) throws Exception, ProjectBuildingException {
    ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
    List<ProjectBuildingResult> projects = projectBuilder.build(Collections.singletonList(pom), false, projectBuildRequestForUnitTest());
    MavenProject project = projects.get(0).getProject();
    return project;
}
Also used : ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult)

Example 5 with ProjectBuildingResult

use of org.apache.maven.project.ProjectBuildingResult in project component-runtime by Talend.

the class ArtifactTransformer method hasTransformedResource.

@Override
public boolean hasTransformedResource() {
    artifacts = new ArrayList<>();
    final ArtifactFilter filter = getFilter();
    if (userArtifacts != null && !userArtifacts.isEmpty()) {
        final ArtifactResolver resolver;
        final DependencyGraphBuilder graphBuilder;
        final ProjectBuilder projectBuilder;
        final PlexusContainer container = session.getContainer();
        try {
            resolver = ArtifactResolver.class.cast(container.lookup(ArtifactResolver.class, "default"));
            projectBuilder = ProjectBuilder.class.cast(container.lookup(ProjectBuilder.class, "default"));
            graphBuilder = includeTransitiveDependencies ? DependencyGraphBuilder.class.cast(container.lookup(DependencyGraphBuilder.class, "default")) : null;
        } catch (final ComponentLookupException e) {
            throw new IllegalArgumentException(e);
        }
        artifacts.addAll(userArtifacts.stream().flatMap(coords -> {
            try {
                final String type = ofNullable(coords.type).filter(s -> !s.isEmpty()).orElse("jar");
                final Artifact art = new DefaultArtifact(coords.groupId, coords.artifactId, coords.version, coords.scope, type, ofNullable(coords.classifier).orElse(""), new DefaultArtifactHandler() {

                    {
                        setExtension(type);
                    }
                });
                final Artifact artifact = resolver.resolveArtifact(session.getProjectBuildingRequest(), art).getArtifact();
                if (includeTransitiveDependencies) {
                    final DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
                    try {
                        final ProjectBuildingResult projectBuildingResult = projectBuilder.build(artifact, true, request);
                        final DependencyNode transitives = graphBuilder.buildDependencyGraph(projectBuildingResult.getProject(), filter);
                        final CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
                        transitives.accept(new FilteringDependencyNodeVisitor(visitor, new ArtifactDependencyNodeFilter(filter)));
                        return Stream.concat(Stream.of(artifact), visitor.getNodes().stream().map(DependencyNode::getArtifact).map(a -> {
                            try {
                                return resolver.resolveArtifact(request, a).getArtifact();
                            } catch (final ArtifactResolverException e) {
                                throw new IllegalStateException(e);
                            }
                        }));
                    } catch (final ProjectBuildingException | DependencyGraphBuilderException e) {
                        throw new IllegalStateException(e);
                    }
                }
                return Stream.of(artifact);
            } catch (final ArtifactResolverException e) {
                throw new IllegalArgumentException(e);
            }
        }).collect(toSet()));
    }
    if (includeProjectComponentDependencies) {
        // this is a weird category of projects but can exist
        final MavenProject project = session.getCurrentProject();
        if (project.getArtifacts() != null && !project.getArtifacts().isEmpty()) {
            project.setArtifactFilter(filter);
            try {
                artifacts.addAll(project.getArtifacts());
            } finally {
                // shade plugin uses it OOTB so reset it for the end of the execution (in case
                // another transformer needs it)
                project.setArtifactFilter(new CumulativeScopeArtifactFilter(singletonList("runtime")));
            }
        }
    }
    return !artifacts.isEmpty();
}
Also used : CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) Setter(lombok.Setter) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) ResourceTransformer(org.apache.maven.plugins.shade.resource.ResourceTransformer) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) IncludesArtifactFilter(org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) Artifact(org.apache.maven.artifact.Artifact) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) Collectors.toSet(java.util.stream.Collectors.toSet) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) MavenSession(org.apache.maven.execution.MavenSession) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) Optional.ofNullable(java.util.Optional.ofNullable) NONE(lombok.AccessLevel.NONE) Relocator(org.apache.maven.plugins.shade.relocation.Relocator) IOException(java.io.IOException) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) PlexusContainer(org.codehaus.plexus.PlexusContainer) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) Stream(java.util.stream.Stream) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) Data(lombok.Data) ProjectBuilder(org.apache.maven.project.ProjectBuilder) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) InputStream(java.io.InputStream) FilteringDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor) CollectingDependencyNodeVisitor(org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) PlexusContainer(org.codehaus.plexus.PlexusContainer) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) MavenProject(org.apache.maven.project.MavenProject) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) ArtifactDependencyNodeFilter(org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) CumulativeScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter) AndArtifactFilter(org.apache.maven.artifact.resolver.filter.AndArtifactFilter) IncludesArtifactFilter(org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ExcludesArtifactFilter(org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) ProjectBuilder(org.apache.maven.project.ProjectBuilder) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Aggregations

ProjectBuildingResult (org.apache.maven.project.ProjectBuildingResult)15 MavenProject (org.apache.maven.project.MavenProject)9 ProjectBuilder (org.apache.maven.project.ProjectBuilder)9 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)9 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)6 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)4 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)4 File (java.io.File)3 Artifact (org.apache.maven.artifact.Artifact)3 Writer (java.io.Writer)2 RemoteException (java.rmi.RemoteException)2 Properties (java.util.Properties)2 InvalidRepositoryException (org.apache.maven.artifact.InvalidRepositoryException)2 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)2 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)2 MavenExecutionResult (org.apache.maven.execution.MavenExecutionResult)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)2