Search in sources :

Example 1 with DefaultPlexusContainer

use of org.codehaus.plexus.DefaultPlexusContainer in project intellij-community by JetBrains.

the class MavenEmbedder method create.

@NotNull
public static MavenEmbedder create(@NotNull final MavenEmbedderSettings embedderSettings) {
    @NotNull final Logger logger = getLogger(embedderSettings);
    DefaultPlexusContainer container = new DefaultPlexusContainer();
    container.setClassWorld(new ClassWorld("plexus.core", embedderSettings.getClass().getClassLoader()));
    container.setLoggerManager(new BaseLoggerManager() {

        @Override
        protected Logger createLogger(final String s) {
            return logger;
        }
    });
    try {
        container.initialize();
        container.start();
    } catch (PlexusContainerException e) {
        MavenEmbedderLog.LOG.error(e);
        throw new RuntimeException(e);
    }
    final PlexusComponentConfigurator configurator = embedderSettings.getConfigurator();
    if (configurator != null) {
        configurator.configureComponents(container);
    }
    File mavenHome = embedderSettings.getMavenHome();
    if (mavenHome != null) {
        System.setProperty(PROP_MAVEN_HOME, mavenHome.getPath());
    }
    Settings nativeSettings = buildSettings(container, embedderSettings);
    return new MavenEmbedder(container, nativeSettings, logger, embedderSettings);
}
Also used : PlexusContainerException(org.codehaus.plexus.PlexusContainerException) BaseLoggerManager(org.codehaus.plexus.logging.BaseLoggerManager) ClassWorld(org.codehaus.classworlds.ClassWorld) Logger(org.codehaus.plexus.logging.Logger) NotNull(org.jetbrains.annotations.NotNull) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DefaultPlexusContainer

use of org.codehaus.plexus.DefaultPlexusContainer in project gradle by gradle.

the class MavenProjectsCreator method createNow.

private Set<MavenProject> createNow(Settings settings, File pomFile) throws PlexusContainerException, PlexusConfigurationException, ComponentLookupException, MavenExecutionRequestPopulationException, ProjectBuildingException {
    ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration().setClassWorld(new ClassWorld("plexus.core", ClassWorld.class.getClassLoader())).setName("mavenCore");
    DefaultPlexusContainer container = new DefaultPlexusContainer(containerConfiguration);
    ProjectBuilder builder = container.lookup(ProjectBuilder.class);
    MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
    final Properties properties = new Properties();
    properties.putAll(SystemProperties.getInstance().asMap());
    executionRequest.setSystemProperties(properties);
    MavenExecutionRequestPopulator populator = container.lookup(MavenExecutionRequestPopulator.class);
    populator.populateFromSettings(executionRequest, settings);
    populator.populateDefaults(executionRequest);
    ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
    buildingRequest.setProcessPlugins(false);
    MavenProject mavenProject = builder.build(pomFile, buildingRequest).getProject();
    Set<MavenProject> reactorProjects = new LinkedHashSet<MavenProject>();
    // TODO adding the parent project first because the converter needs it this way ATM. This is oversimplified.
    // the converter should not depend on the order of reactor projects.
    // we should add coverage for nested multi-project builds with multiple parents.
    reactorProjects.add(mavenProject);
    List<ProjectBuildingResult> allProjects = builder.build(ImmutableList.of(pomFile), true, buildingRequest);
    CollectionUtils.collect(allProjects, reactorProjects, new Transformer<MavenProject, ProjectBuildingResult>() {

        public MavenProject transform(ProjectBuildingResult original) {
            return original.getProject();
        }
    });
    MavenExecutionResult result = new DefaultMavenExecutionResult();
    result.setProject(mavenProject);
    RepositorySystemSession repoSession = new DefaultRepositorySystemSession();
    MavenSession session = new MavenSession(container, repoSession, executionRequest, result);
    session.setCurrentProject(mavenProject);
    return reactorProjects;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) Properties(java.util.Properties) SystemProperties(org.gradle.internal.SystemProperties) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession)

Example 3 with DefaultPlexusContainer

use of org.codehaus.plexus.DefaultPlexusContainer in project kie-wb-common by kiegroup.

the class AFMavenCli method loadCoreExtensions.

protected List<CoreExtensionEntry> loadCoreExtensions(AFCliRequest cliRequest, ClassRealm containerRealm, Set<String> providedArtifacts) {
    if (cliRequest.getMultiModuleProjectDirectory() == null) {
        return Collections.emptyList();
    }
    Path extensionsFile = Paths.get(cliRequest.getMultiModuleProjectDirectory().toString(), EXTENSIONS_FILENAME);
    if (!java.nio.file.Files.isRegularFile(extensionsFile)) {
        return Collections.emptyList();
    }
    try {
        List<CoreExtension> extensions = readCoreExtensionsDescriptor(extensionsFile);
        if (extensions.isEmpty()) {
            return Collections.emptyList();
        }
        ContainerConfiguration cc = // 
        new DefaultContainerConfiguration().setClassWorld(// 
        cliRequest.getClassWorld()).setRealm(// 
        containerRealm).setClassPathScanning(// 
        PlexusConstants.SCANNING_INDEX).setAutoWiring(// 
        true).setName("maven");
        DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() {

            @Override
            protected void configure() {
                bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory);
            }
        });
        try {
            container.setLookupRealm(null);
            container.setLoggerManager(plexusLoggerManager);
            container.getLoggerManager().setThresholds(cliRequest.getRequest().getLoggingLevel());
            Thread.currentThread().setContextClassLoader(container.getContainerRealm());
            executionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class);
            configurationProcessors = container.lookupMap(AFConfigurationProcessor.class);
            configure(cliRequest);
            MavenExecutionRequest request = DefaultMavenExecutionRequest.copy(cliRequest.getRequest());
            request = populateRequest(cliRequest, request);
            request = executionRequestPopulator.populateDefaults(request);
            BootstrapCoreExtensionManager resolver = container.lookup(BootstrapCoreExtensionManager.class);
            return resolver.loadCoreExtensions(request, providedArtifacts, extensions);
        } finally {
            executionRequestPopulator = null;
            container.dispose();
        }
    } catch (RuntimeException e) {
        // runtime exceptions are most likely bugs in maven, let them bubble up to the user
        throw e;
    } catch (Exception e) {
        slf4jLogger.warn("Failed to read extensions descriptor " + extensionsFile + ": " + e.getMessage());
    }
    return Collections.emptyList();
}
Also used : Path(java.nio.file.Path) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) LifecycleExecutionException(org.apache.maven.lifecycle.LifecycleExecutionException) InternalErrorException(org.apache.maven.InternalErrorException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.commons.cli.ParseException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) IOException(java.io.IOException) MavenExecutionRequestPopulationException(org.apache.maven.execution.MavenExecutionRequestPopulationException) AbstractModule(com.google.inject.AbstractModule) CoreExtension(org.apache.maven.cli.internal.extension.model.CoreExtension) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) BootstrapCoreExtensionManager(org.apache.maven.cli.internal.BootstrapCoreExtensionManager) MavenExecutionRequestPopulator(org.apache.maven.execution.MavenExecutionRequestPopulator)

Example 4 with DefaultPlexusContainer

use of org.codehaus.plexus.DefaultPlexusContainer in project kie-wb-common by kiegroup.

the class AFMavenCli method container.

protected PlexusContainer container(AFCliRequest cliRequest, ClassWorld classWorld) throws Exception {
    if (cliRequest.getClassWorld() == null) {
        /**
         *             The classworld is now Created in the NioMavenCompiler and in the InternalNioDefaultMaven compielr for this reasons:
         *             problem: https://stackoverflow.com/questions/22410706/error-when-execute-mavencli-in-the-loop-maven-embedder
         *             problem:https://stackoverflow.com/questions/40587683/invocation-of-mavencli-fails-within-a-maven-plugin
         *             solution:https://dev.eclipse.org/mhonarc/lists/sisu-users/msg00063.html
         *             original version:
         *             cliRequest.setClassWorld(new ClassWorld("plexus.core",Thread.currentThread().getContextClassLoader()));
         */
        cliRequest.setClassWorld(classWorld);
    }
    ClassRealm coreRealm = cliRequest.getClassWorld().getClassRealm("plexus.core");
    if (coreRealm == null) {
        coreRealm = cliRequest.getClassWorld().getRealms().iterator().next();
    }
    List<File> extClassPath = parseExtClasspath(cliRequest);
    CoreExtensionEntry coreEntry = CoreExtensionEntry.discoverFrom(coreRealm);
    List<CoreExtensionEntry> extensions = loadCoreExtensions(cliRequest, coreRealm, coreEntry.getExportedArtifacts());
    ClassRealm containerRealm = setupContainerRealm(cliRequest.getClassWorld(), coreRealm, extClassPath, extensions);
    ContainerConfiguration cc = new DefaultContainerConfiguration().setClassWorld(cliRequest.getClassWorld()).setRealm(containerRealm).setClassPathScanning(PlexusConstants.SCANNING_INDEX).setAutoWiring(true).setName("maven");
    Set<String> exportedArtifacts = new HashSet<String>(coreEntry.getExportedArtifacts());
    Set<String> exportedPackages = new HashSet<String>(coreEntry.getExportedPackages());
    for (CoreExtensionEntry extension : extensions) {
        exportedArtifacts.addAll(extension.getExportedArtifacts());
        exportedPackages.addAll(extension.getExportedPackages());
    }
    final CoreExports exports = new CoreExports(containerRealm, exportedArtifacts, exportedPackages);
    DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() {

        @Override
        protected void configure() {
            bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory);
            bind(CoreExports.class).toInstance(exports);
        }
    });
    // This is used to share informations at runtime between Maven plugins and our compiler
    container.addComponent(cliRequest.getMap(), HashMap.class, "kieMap");
    // NOTE: To avoid inconsistencies, we'll use the TCCL exclusively for lookups
    container.setLookupRealm(null);
    container.setLoggerManager(plexusLoggerManager);
    for (CoreExtensionEntry extension : extensions) {
        container.discoverComponents(extension.getClassRealm());
    }
    customizeContainer(container);
    container.getLoggerManager().setThresholds(cliRequest.getRequest().getLoggingLevel());
    Thread.currentThread().setContextClassLoader(container.getContainerRealm());
    eventSpyDispatcher = container.lookup(EventSpyDispatcher.class);
    DefaultEventSpyContext eventSpyContext = new DefaultEventSpyContext();
    Map<String, Object> data = eventSpyContext.getData();
    data.put("plexus", container);
    data.put("workingDirectory", cliRequest.getWorkingDirectory());
    data.put("systemProperties", cliRequest.getSystemProperties());
    data.put("userProperties", cliRequest.getUserProperties());
    data.put("versionProperties", AFCLIReportingUtils.getBuildProperties());
    eventSpyDispatcher.init(eventSpyContext);
    slf4jLogger = slf4jLoggerFactory.getLogger(this.getClass().getName());
    maven = container.lookup(Maven.class);
    executionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class);
    modelProcessor = createModelProcessor(container);
    configurationProcessors = container.lookupMap(AFConfigurationProcessor.class);
    toolchainsBuilder = container.lookup(ToolchainsBuilder.class);
    dispatcher = (DefaultSecDispatcher) container.lookup(SecDispatcher.class, "maven");
    return container;
}
Also used : EventSpyDispatcher(org.apache.maven.eventspy.internal.EventSpyDispatcher) Maven(org.apache.maven.Maven) CoreExtensionEntry(org.apache.maven.extension.internal.CoreExtensionEntry) ToolchainsBuilder(org.apache.maven.toolchain.building.ToolchainsBuilder) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) AbstractModule(com.google.inject.AbstractModule) ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) CoreExports(org.apache.maven.extension.internal.CoreExports) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) DefaultEventSpyContext(org.apache.maven.cli.event.DefaultEventSpyContext) File(java.io.File) HashSet(java.util.HashSet) MavenExecutionRequestPopulator(org.apache.maven.execution.MavenExecutionRequestPopulator)

Example 5 with DefaultPlexusContainer

use of org.codehaus.plexus.DefaultPlexusContainer in project archiva by apache.

the class PlexusSisuBridge method initialize.

@PostConstruct
public void initialize() throws PlexusSisuBridgeException {
    DefaultContainerConfiguration conf = new DefaultContainerConfiguration();
    conf.setAutoWiring(containerAutoWiring);
    conf.setClassPathScanning(containerClassPathScanning);
    conf.setComponentVisibility(containerComponentVisibility);
    conf.setContainerConfigurationURL(overridingComponentsXml);
    ClassWorld classWorld = new ClassWorld();
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    containerRealm = new ClassRealm(classWorld, "maven", tccl);
    if (tccl instanceof URLClassLoader) {
        URL[] urls = ((URLClassLoader) tccl).getURLs();
        for (URL url : urls) {
            containerRealm.addURL(url);
        }
    }
    conf.setRealm(containerRealm);
    // conf.setClassWorld( classWorld );
    ClassLoader ori = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(containerRealm);
        plexusContainer = new DefaultPlexusContainer(conf);
    } catch (PlexusContainerException e) {
        throw new PlexusSisuBridgeException(e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(ori);
    }
}
Also used : PlexusContainerException(org.codehaus.plexus.PlexusContainerException) ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) URL(java.net.URL) PostConstruct(javax.annotation.PostConstruct)

Aggregations

DefaultPlexusContainer (org.codehaus.plexus.DefaultPlexusContainer)16 DefaultContainerConfiguration (org.codehaus.plexus.DefaultContainerConfiguration)8 PlexusContainer (org.codehaus.plexus.PlexusContainer)6 PlexusContainerException (org.codehaus.plexus.PlexusContainerException)6 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)5 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)5 ContainerConfiguration (org.codehaus.plexus.ContainerConfiguration)5 ClassWorld (org.codehaus.plexus.classworlds.ClassWorld)5 File (java.io.File)4 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)4 MavenSession (org.apache.maven.execution.MavenSession)4 ClassRealm (org.codehaus.plexus.classworlds.realm.ClassRealm)4 AbstractModule (com.google.inject.AbstractModule)3 MavenExecutionRequestPopulationException (org.apache.maven.execution.MavenExecutionRequestPopulationException)3 MavenExecutionRequestPopulator (org.apache.maven.execution.MavenExecutionRequestPopulator)3 IOException (java.io.IOException)2 ParseException (org.apache.commons.cli.ParseException)2 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)2 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)2 CoreExports (org.apache.maven.extension.internal.CoreExports)2