Search in sources :

Example 11 with ProjectBuildingException

use of org.apache.maven.project.ProjectBuildingException in project che by eclipse.

the class EffectivePomWriter method getEffectivePom.

public static String getEffectivePom(MavenServerImpl server, final File pom, List<String> activeProfiles, List<String> inactiveProfiles) {
    StringWriter stringWriter = new StringWriter();
    try {
        MavenExecutionRequest request = server.newMavenRequest(pom, activeProfiles, inactiveProfiles, Collections.emptyList());
        server.runMavenRequest(request, () -> {
            try {
                ProjectBuilder builder = server.getMavenComponent(ProjectBuilder.class);
                ProjectBuildingResult projectBuildingResult = builder.build(new File(pom.getPath()), request.getProjectBuildingRequest());
                MavenProject project = projectBuildingResult.getProject();
                XMLWriter writer = new PrettyPrintXMLWriter(stringWriter, "    ");
                writeHeader(writer);
                writeEffectivePom(project, writer);
            } catch (ProjectBuildingException | MojoExecutionException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        });
    } catch (Exception e) {
        return null;
    }
    return stringWriter.toString();
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) JDOMException(org.jdom.JDOMException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StringWriter(java.io.StringWriter) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) File(java.io.File)

Example 12 with ProjectBuildingException

use of org.apache.maven.project.ProjectBuildingException in project karaf by apache.

the class GenerateDescriptorMojo method resolveProject.

private MavenProject resolveProject(final Object artifact) throws MojoExecutionException {
    MavenProject resolvedProject = project;
    if (includeTransitiveVersionRanges) {
        resolvedProject = resolvedProjects.get(artifact);
        if (resolvedProject == null) {
            final ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
            // Fixes KARAF-4626; if the system properties are not transferred to the request, 
            // test-feature-use-version-range-transfer-properties will fail
            request.setSystemProperties(System.getProperties());
            request.setResolveDependencies(true);
            request.setRemoteRepositories(project.getPluginArtifactRepositories());
            request.setLocalRepository(localRepo);
            request.setProfiles(new ArrayList<>(mavenSession.getRequest().getProfiles()));
            request.setActiveProfileIds(new ArrayList<>(mavenSession.getRequest().getActiveProfiles()));
            dependencyHelper.setRepositorySession(request);
            final Artifact pomArtifact = repoSystem.createArtifact(dependencyHelper.getGroupId(artifact), dependencyHelper.getArtifactId(artifact), dependencyHelper.getBaseVersion(artifact), "pom");
            try {
                resolvedProject = mavenProjectBuilder.build(pomArtifact, request).getProject();
                resolvedProjects.put(pomArtifact, resolvedProject);
            } catch (final ProjectBuildingException e) {
                throw new MojoExecutionException(format("Maven-project could not be built for artifact %s", pomArtifact), e);
            }
        }
    }
    return resolvedProject;
}
Also used : DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MavenProject(org.apache.maven.project.MavenProject) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 13 with ProjectBuildingException

use of org.apache.maven.project.ProjectBuildingException 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 14 with ProjectBuildingException

use of org.apache.maven.project.ProjectBuildingException in project maven-plugins by apache.

the class DeployFileMojo method createMavenProject.

/**
     * Creates a Maven project in-memory from the user-supplied groupId, artifactId and version. When a classifier is
     * supplied, the packaging must be POM because the project with only have attachments. This project serves as basis
     * to attach the artifacts to deploy to.
     * 
     * @return The created Maven project, never <code>null</code>.
     * @throws MojoExecutionException When the model of the project could not be built.
     * @throws MojoFailureException When building the project failed.
     */
private MavenProject createMavenProject() throws MojoExecutionException, MojoFailureException {
    if (groupId == null || artifactId == null || version == null || packaging == null) {
        throw new MojoExecutionException("The artifact information is incomplete: 'groupId', 'artifactId', " + "'version' and 'packaging' are required.");
    }
    ModelSource modelSource = new StringModelSource("<project>" + "<modelVersion>4.0.0</modelVersion>" + "<groupId>" + groupId + "</groupId>" + "<artifactId>" + artifactId + "</artifactId>" + "<version>" + version + "</version>" + "<packaging>" + (classifier == null ? packaging : "pom") + "</packaging>" + "</project>");
    DefaultProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(getSession().getProjectBuildingRequest());
    buildingRequest.setProcessPlugins(false);
    try {
        return projectBuilder.build(modelSource, buildingRequest).getProject();
    } catch (ProjectBuildingException e) {
        if (e.getCause() instanceof ModelBuildingException) {
            throw new MojoExecutionException("The artifact information is not valid:" + Os.LINE_SEP + e.getCause().getMessage());
        }
        throw new MojoFailureException("Unable to create the project.", e);
    }
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) StringModelSource(org.apache.maven.model.building.StringModelSource) StringModelSource(org.apache.maven.model.building.StringModelSource) ModelSource(org.apache.maven.model.building.ModelSource) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 15 with ProjectBuildingException

use of org.apache.maven.project.ProjectBuildingException in project maven-plugins by apache.

the class DoapMojo method getMavenProject.

// ----------------------------------------------------------------------
// Private methods
// ----------------------------------------------------------------------
/**
     * @param artifact not null
     * @return the maven project for the given doap artifact
     * @since 1.1
     */
private MavenProject getMavenProject(DoapArtifact artifact) {
    if (artifact == null) {
        return null;
    }
    if (StringUtils.isEmpty(artifact.getGroupId()) || StringUtils.isEmpty(artifact.getArtifactId()) || StringUtils.isEmpty(artifact.getVersion())) {
        getLog().warn("Missing groupId or artifactId or version in <artifact/> parameter, ignored it.");
        return null;
    }
    getLog().info("Using artifact " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion());
    try {
        Artifact art = factory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), Artifact.SCOPE_COMPILE);
        if (art.getFile() == null) {
            MavenProject proj = mavenProjectBuilder.buildFromRepository(art, remoteRepositories, localRepository);
            art = proj.getArtifact();
            resolver.resolve(art, remoteRepositories, localRepository);
            return proj;
        }
    } catch (ArtifactResolutionException e) {
        getLog().error("ArtifactResolutionException: " + e.getMessage() + "\nIgnored <artifact/> parameter.");
    } catch (ArtifactNotFoundException e) {
        getLog().error("ArtifactNotFoundException: " + e.getMessage() + "\nIgnored <artifact/> parameter.");
    } catch (ProjectBuildingException e) {
        getLog().error("ProjectBuildingException: " + e.getMessage() + "\nIgnored <artifact/> parameter.");
    }
    return null;
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MavenProject(org.apache.maven.project.MavenProject) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) DoapArtifact(org.apache.maven.plugin.doap.options.DoapArtifact) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)18 MavenProject (org.apache.maven.project.MavenProject)12 Artifact (org.apache.maven.artifact.Artifact)9 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 VersionRange (org.apache.maven.artifact.versioning.VersionRange)3 License (org.apache.maven.model.License)3 Model (org.apache.maven.model.Model)3 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)3 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)3 File (java.io.File)2 HashMap (java.util.HashMap)2 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)2 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)2 ModelBuildingException (org.apache.maven.model.building.ModelBuildingException)2 ModelSource (org.apache.maven.model.building.ModelSource)2 StringModelSource (org.apache.maven.model.building.StringModelSource)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 InvalidProjectModelException (org.apache.maven.project.InvalidProjectModelException)2