Example usage for org.apache.maven.model.building ModelSource ModelSource

List of usage examples for org.apache.maven.model.building ModelSource ModelSource

Introduction

In this page you can find the example usage for org.apache.maven.model.building ModelSource ModelSource.

Prototype

ModelSource

Source Link

Usage

From source file:org.appformer.maven.integration.embedder.MavenEmbedder.java

License:Apache License

public MavenProject readProject(final InputStream mavenProjectStream)
        throws ProjectBuildingException, MavenEmbedderException {
    ModelSource modelSource = new ModelSource() {
        @Override// www .j  a va  2 s.  c om
        public InputStream getInputStream() throws IOException {
            return mavenProjectStream;
        }

        @Override
        public String getLocation() {
            return "";
        }
    };

    ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(componentProvider.getSystemClassLoader());
        ProjectBuilder projectBuilder = componentProvider.lookup(ProjectBuilder.class);
        // BZ-1007894: Check if added dependencies are resolvable.
        ProjectBuildingResult result = projectBuilder.build(modelSource, getProjectBuildingRequest());
        if (result != null && result.getDependencyResolutionResult() != null
                && !result.getDependencyResolutionResult().getCollectionErrors().isEmpty()) {
            // A dependency resolution error has been produced. It can contains some error. Throw the first one to the client, so the user will fix every one sequentially.
            Exception depedencyResolutionException = result.getDependencyResolutionResult()
                    .getCollectionErrors().get(0);
            if (depedencyResolutionException != null) {
                throw new MavenEmbedderException(depedencyResolutionException.getMessage(),
                        depedencyResolutionException);
            }
        }
        return result.getProject();
    } catch (ComponentLookupException e) {
        throw new MavenEmbedderException(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(originalCl);
        try {
            mavenProjectStream.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.echocat.jomon.maven.boot.ArtifactFactory.java

License:Mozilla Public License

protected ModelSource createModelSource() {
    return new ModelSource() {
        @Override/*from   w  ww. java 2  s . c  o  m*/
        public InputStream getInputStream() throws IOException {
            final InputStream is = ArtifactFactory.class.getResourceAsStream(BOOTSTRAP_POM_XML);
            if (is == null) {
                throw new FileNotFoundException("Could not find the " + BOOTSTRAP_POM_XML + ".");
            }
            return is;
        }

        @Override
        public String getLocation() {
            return BOOTSTRAP_POM_XML;
        }
    };
}

From source file:org.kie.scanner.embedder.MavenEmbedder.java

License:Apache License

public MavenProject readProject(final InputStream mavenProjectStream)
        throws ProjectBuildingException, MavenEmbedderException {
    ModelSource modelSource = new ModelSource() {
        @Override//  www.  j a  v  a  2s.  c o m
        public InputStream getInputStream() throws IOException {
            return mavenProjectStream;
        }

        @Override
        public String getLocation() {
            return "";
        }
    };

    ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.plexusContainer.getContainerRealm());
        ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
        // BZ-1007894: Check if added dependencies are resolvable.
        ProjectBuildingResult result = projectBuilder.build(modelSource, getProjectBuildingRequest());
        if (result != null && result.getDependencyResolutionResult() != null
                && !result.getDependencyResolutionResult().getCollectionErrors().isEmpty()) {
            // A dependency resolution error has been produced. It can contains some error. Throw the first one to the client, so the user will fix every one sequentially.
            Exception depedencyResolutionException = result.getDependencyResolutionResult()
                    .getCollectionErrors().get(0);
            if (depedencyResolutionException != null)
                throw new MavenEmbedderException(depedencyResolutionException.getMessage(),
                        depedencyResolutionException);
        }
        return result.getProject();
    } catch (ComponentLookupException e) {
        throw new MavenEmbedderException(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(originalCl);
        try {
            mavenProjectStream.close();
        } catch (IOException e) {
        }
    }
}

From source file:org.sourcepit.maven.dependency.model.aether.AetherDependencyModelResolver.java

License:Apache License

/**
 * {@inheritDoc}//  www .  j  ava  2s .co  m
 */
@Override
public DependencyModel resolve(@NotNull Collection<Dependency> dependencies,
        ArtifactAttachmentFactory attachmentFactory)
        throws ProjectBuildingException, DependencyResolutionException {
    final Model model;

    final MavenProject currentProject = buildContext.getSession().getCurrentProject();
    if (currentProject == null) {
        model = new Model();
    } else {
        model = currentProject.getModel().clone();
    }

    model.setModelVersion("4.0.0");
    model.setGroupId("org.sourcepit");
    model.setArtifactId("dummy-project");
    model.setVersion("1337");

    model.getDependencies().clear();
    model.getDependencies().addAll(dependencies);

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        new DefaultModelWriter().write(out, null, model);
    } catch (IOException e) {
        throw Exceptions.pipe(e);
    }
    final byte[] bytes = out.toByteArray();

    final ProjectBuildingRequest request = newProjectBuildingRequest(false, false);

    ProjectBuildingResult result = projectBuilder.build(new ModelSource() {
        @Override
        public String getLocation() {
            return "memory";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }
    }, request);

    final MavenProject project = result.getProject();
    return resolve(project, false, attachmentFactory);
}

From source file:org.talend.components.api.service.internal.ComponentServiceImpl.java

License:Open Source License

Model loadPom(final InputStream pomStream, MavenBooter booter, List<String> profilesList)
        throws ModelBuildingException {

    RepositorySystem system = booter.newRepositorySystem();
    RepositorySystemSession session = booter.newRepositorySystemSession(system);
    ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest();
    modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
    modelRequest.setProcessPlugins(false);
    modelRequest.setTwoPhaseBuilding(false);
    modelRequest.setSystemProperties(toProperties(session.getUserProperties(), session.getSystemProperties()));
    // modelRequest.setModelCache( DefaultModelCache.newInstance( session ) );
    ProjectModelResolver projectModelResolver = new ProjectModelResolver(session, null, system,
            new DefaultRemoteRepositoryManager(),
            booter.getRemoteRepositoriesWithAuthentification(system, session), null, null);
    modelRequest.setModelResolver(projectModelResolver);
    modelRequest.setActiveProfileIds(profilesList);
    modelRequest.setModelSource(new ModelSource() {

        @Override/* w w  w .  j  a  v  a  2  s .  c o m*/
        public InputStream getInputStream() throws IOException {
            return pomStream;
        }

        @Override
        public String getLocation() {
            return "";// FIXME return the component name
        }
    });
    if (modelBuilder == null) {
        modelBuilder = new DefaultModelBuilderFactory().newInstance();
    }
    ModelBuildingResult builtModel = modelBuilder.build(modelRequest);
    LOGGER.debug("built problems:" + builtModel.getProblems());
    return builtModel.getEffectiveModel();
}