Search in sources :

Example 6 with ModelSource

use of org.apache.maven.model.building.ModelSource 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)

Aggregations

ModelSource (org.apache.maven.model.building.ModelSource)6 UnresolvableModelException (org.apache.maven.model.resolution.UnresolvableModelException)3 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)3 FileModelSource (org.apache.maven.model.building.FileModelSource)2 ModelBuildingException (org.apache.maven.model.building.ModelBuildingException)2 StringModelSource (org.apache.maven.model.building.StringModelSource)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)2 Artifact (org.eclipse.aether.artifact.Artifact)2 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)1 Location (com.google.devtools.build.lib.events.Location)1 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)1 Target (com.google.devtools.build.lib.packages.Target)1 DefaultModelResolver (com.google.devtools.build.workspace.maven.DefaultModelResolver)1 InvalidArtifactCoordinateException (com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException)1 Rule (com.google.devtools.build.workspace.maven.Rule)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1