Search in sources :

Example 6 with DefaultContainerConfiguration

use of org.codehaus.plexus.DefaultContainerConfiguration 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 7 with DefaultContainerConfiguration

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

the class ReusableAFMavenCli method createNewPlexusContainer.

private DefaultPlexusContainer createNewPlexusContainer(AFCliRequest cliRequest, ClassWorld classWorld) throws Exception {
    if (cliRequest.getClassWorld() == null) {
        /**
         *             The classworld is now Created in the NioMavenCompiler and in the InternalNioDefaultMaven compiler 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);
    reusableEventSpyDispatcher = container.lookup(EventSpyDispatcher.class);
    reusableEventSpyContext = new DefaultEventSpyContext();
    setEventSpyContextData(reusableEventSpyContext, container, cliRequest);
    reusableEventSpyDispatcher.init(reusableEventSpyContext);
    reusableSlf4jLogger = slf4jLoggerFactory.getLogger(this.getClass().getName());
    reusableMaven = container.lookup(Maven.class);
    reusableExecutionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class);
    reusableModelProcessor = createModelProcessor(container);
    reusableConfigurationProcessors = container.lookupMap(AFConfigurationProcessor.class);
    reusableToolchainsBuilder = container.lookup(ToolchainsBuilder.class);
    reusableDispatcher = (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 8 with DefaultContainerConfiguration

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

the class AbstractMavenPublishAction method newPlexusContainer.

private PlexusContainer newPlexusContainer(List<File> wagonJars) {
    try {
        ClassWorld world = new ClassWorld("plexus.core", ClassWorld.class.getClassLoader());
        ClassRealm classRealm = new ClassRealm(world, "plexus.core", ClassWorld.class.getClassLoader());
        if (wagonJars != null) {
            for (File jar : wagonJars) {
                classRealm.addURL(jar.toURI().toURL());
            }
        }
        return new DefaultPlexusContainer(new DefaultContainerConfiguration().setRealm(classRealm));
    } catch (PlexusContainerException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    } catch (MalformedURLException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : PlexusContainerException(org.codehaus.plexus.PlexusContainerException) ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) MalformedURLException(java.net.MalformedURLException) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) File(java.io.File)

Example 9 with DefaultContainerConfiguration

use of org.codehaus.plexus.DefaultContainerConfiguration in project pom-manipulation-ext by release-engineering.

the class PlexusTestRunner method createTest.

@Override
protected Object createTest() throws Exception {
    final TestClass testClass = getTestClass();
    final DefaultContainerConfiguration config = new DefaultContainerConfiguration();
    config.setAutoWiring(true);
    config.setClassPathScanning(PlexusConstants.SCANNING_ON);
    config.setComponentVisibility(PlexusConstants.GLOBAL_VISIBILITY);
    config.setName(testClass.getName());
    final DefaultPlexusContainer container = new DefaultPlexusContainer(config);
    final ClassSpace cs = new URLClassSpace(Thread.currentThread().getContextClassLoader());
    container.addPlexusInjector(Arrays.<PlexusBeanModule>asList(new PlexusAnnotatedBeanModule(cs, Collections.emptyMap())));
    return container.lookup(testClass.getJavaClass());
}
Also used : DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ClassSpace(org.sonatype.guice.bean.reflect.ClassSpace) URLClassSpace(org.sonatype.guice.bean.reflect.URLClassSpace) TestClass(org.junit.runners.model.TestClass) URLClassSpace(org.sonatype.guice.bean.reflect.URLClassSpace) PlexusAnnotatedBeanModule(org.sonatype.guice.plexus.binders.PlexusAnnotatedBeanModule)

Example 10 with DefaultContainerConfiguration

use of org.codehaus.plexus.DefaultContainerConfiguration in project drools by kiegroup.

the class MavenEmbedderUtils method buildPlexusContainer.

public static PlexusContainer buildPlexusContainer(ClassLoader mavenClassLoader, ClassLoader parent, MavenRequest mavenRequest) throws MavenEmbedderException {
    DefaultContainerConfiguration conf = new DefaultContainerConfiguration();
    conf.setAutoWiring(mavenRequest.isContainerAutoWiring()).setClassPathScanning(mavenRequest.getContainerClassPathScanning()).setComponentVisibility(mavenRequest.getContainerComponentVisibility()).setContainerConfigurationURL(mavenRequest.getOverridingComponentsXml());
    ClassWorld classWorld = new ClassWorld();
    ClassRealm parentRealm = createParentRealm(classWorld, parent, MavenExecutionRequestPopulator.class, RepositorySystem.class, PlexusCipher.class);
    ClassRealm classRealm = createClassRealm(classWorld, "maven", mavenClassLoader);
    classRealm.setParentRealm(parentRealm);
    conf.setRealm(classRealm);
    conf.setClassWorld(classWorld);
    return buildPlexusContainer(mavenRequest, conf);
}
Also used : ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld)

Aggregations

DefaultContainerConfiguration (org.codehaus.plexus.DefaultContainerConfiguration)15 DefaultPlexusContainer (org.codehaus.plexus.DefaultPlexusContainer)13 ContainerConfiguration (org.codehaus.plexus.ContainerConfiguration)10 ClassWorld (org.codehaus.plexus.classworlds.ClassWorld)8 ClassRealm (org.codehaus.plexus.classworlds.realm.ClassRealm)8 AbstractModule (com.google.inject.AbstractModule)7 MavenExecutionRequestPopulator (org.apache.maven.execution.MavenExecutionRequestPopulator)7 File (java.io.File)4 IOException (java.io.IOException)4 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)4 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)4 CoreExports (org.apache.maven.extension.internal.CoreExports)4 CoreExtensionEntry (org.apache.maven.extension.internal.CoreExtensionEntry)4 FileNotFoundException (java.io.FileNotFoundException)3 Path (java.nio.file.Path)3 HashSet (java.util.HashSet)3 ParseException (org.apache.commons.cli.ParseException)3 UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)3 InternalErrorException (org.apache.maven.InternalErrorException)3 Maven (org.apache.maven.Maven)3