Search in sources :

Example 11 with DefaultContainerConfiguration

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

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

the class ReusableAFMavenCli 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");
        if (reusableContainerCoreExtensions == null) {
            reusableContainerCoreExtensions = new DefaultPlexusContainer(cc, new AbstractModule() {

                @Override
                protected void configure() {
                    bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory);
                }
            });
        }
        try {
            reusableContainerCoreExtensions.setLookupRealm(null);
            ((DefaultPlexusContainer) reusableContainerCoreExtensions).setLoggerManager(plexusLoggerManager);
            ((DefaultPlexusContainer) reusableContainerCoreExtensions).getLoggerManager().setThresholds(cliRequest.getRequest().getLoggingLevel());
            Thread.currentThread().setContextClassLoader(reusableContainerCoreExtensions.getContainerRealm());
            reusableExecutionRequestPopulator = reusableContainerCoreExtensions.lookup(MavenExecutionRequestPopulator.class);
            reusableConfigurationProcessors = reusableContainerCoreExtensions.lookupMap(AFConfigurationProcessor.class);
            configure(cliRequest);
            MavenExecutionRequest request = DefaultMavenExecutionRequest.copy(cliRequest.getRequest());
            request = populateRequest(cliRequest, request);
            request = reusableExecutionRequestPopulator.populateDefaults(request);
            BootstrapCoreExtensionManager resolver = reusableContainerCoreExtensions.lookup(BootstrapCoreExtensionManager.class);
            reusableExtensions = resolver.loadCoreExtensions(request, providedArtifacts, extensions);
            return reusableExtensions;
        } finally {
            reusableExecutionRequestPopulator = null;
        }
    } catch (RuntimeException e) {
        // runtime exceptions are most likely bugs in maven, let them bubble up to the user
        throw e;
    } catch (Exception e) {
        reusableSlf4jLogger.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) ILoggerFactory(org.slf4j.ILoggerFactory) BootstrapCoreExtensionManager(org.apache.maven.cli.internal.BootstrapCoreExtensionManager) MavenExecutionRequestPopulator(org.apache.maven.execution.MavenExecutionRequestPopulator)

Example 13 with DefaultContainerConfiguration

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

the class MavenProjectsCreator method createNow.

private Set<MavenProject> createNow(Settings settings, File pomFile) throws PlexusContainerException, ComponentLookupException, MavenExecutionRequestPopulationException, ProjectBuildingException {
    ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration().setClassWorld(new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader())).setName("mavenCore").setClassPathScanning(PlexusConstants.SCANNING_INDEX).setAutoWiring(true);
    DefaultPlexusContainer container = new DefaultPlexusContainer(containerConfiguration);
    ProjectBuilder builder = container.lookup(ProjectBuilder.class);
    MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
    final Properties properties = SystemProperties.getInstance().withSystemProperties(() -> {
        final Properties currentProperties = new Properties();
        currentProperties.putAll(System.getProperties());
        return currentProperties;
    });
    executionRequest.setSystemProperties(properties);
    MavenExecutionRequestPopulator populator = container.lookup(MavenExecutionRequestPopulator.class);
    populateFromSettings(settings, executionRequest, populator);
    populator.populateDefaults(executionRequest);
    ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
    buildingRequest.getRemoteRepositories().forEach(repository -> {
        if (repository.getId().equals(RepositorySystem.DEFAULT_REMOTE_REPO_ID)) {
            repository.setUrl(RepositoryHandler.MAVEN_CENTRAL_URL);
        }
    });
    buildingRequest.setProcessPlugins(false);
    MavenProject mavenProject = builder.build(pomFile, buildingRequest).getProject();
    Set<MavenProject> reactorProjects = new LinkedHashSet<>();
    // 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);
    // noinspection NullableProblems
    CollectionUtils.collect(allProjects, reactorProjects, ProjectBuildingResult::getProject);
    MavenExecutionResult result = new DefaultMavenExecutionResult();
    result.setProject(mavenProject);
    RepositorySystemSession repoSession = new DefaultRepositorySystemSession();
    @SuppressWarnings("deprecation") MavenSession session = new MavenSession(container, repoSession, executionRequest, result);
    session.setCurrentProject(mavenProject);
    return reactorProjects;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) Properties(java.util.Properties) SystemProperties(org.gradle.internal.SystemProperties) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) MavenExecutionRequestPopulator(org.apache.maven.execution.MavenExecutionRequestPopulator)

Example 14 with DefaultContainerConfiguration

use of org.codehaus.plexus.DefaultContainerConfiguration in project spring-cloud-function by spring-cloud.

the class DependencyResolutionModule method initialize.

private void initialize() {
    if (this.container == null) {
        synchronized (lock) {
            if (this.container == null) {
                ClassWorld classWorld = new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader());
                ContainerConfiguration config = new DefaultContainerConfiguration().setClassWorld(classWorld).setRealm(classWorld.getClassRealm("plexus.core")).setClassPathScanning(PlexusConstants.SCANNING_INDEX).setAutoWiring(true).setName("maven");
                PlexusContainer container;
                try {
                    container = new DefaultPlexusContainer(config, new AetherModule(), new DependencyResolutionModule());
                    localRepositoryManagerFactory = container.lookup(LocalRepositoryManagerFactory.class);
                    container.addComponent(new ClassRealmManager((MutablePlexusContainer) container, new DefaultBeanLocator()), ClassRealmManager.class.getName());
                    projectBuilder = container.lookup(ProjectBuilder.class);
                    repositorySystem = container.lookup(RepositorySystem.class);
                } catch (Exception e) {
                    throw new IllegalStateException("Cannot create container", e);
                }
                this.container = container;
                this.settings = new MavenSettingsReader().readSettings();
            }
        }
    }
}
Also used : ClassRealmManager(org.eclipse.sisu.plexus.ClassRealmManager) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) MutablePlexusContainer(org.codehaus.plexus.MutablePlexusContainer) PlexusContainer(org.codehaus.plexus.PlexusContainer) AetherModule(org.eclipse.aether.impl.guice.AetherModule) DefaultBeanLocator(org.eclipse.sisu.inject.DefaultBeanLocator) MutablePlexusContainer(org.codehaus.plexus.MutablePlexusContainer) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) NoLocalRepositoryManagerException(org.eclipse.aether.repository.NoLocalRepositoryManagerException) IOException(java.io.IOException) RepositorySystem(org.eclipse.aether.RepositorySystem) LocalRepositoryManagerFactory(org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ProjectBuilder(org.apache.maven.project.ProjectBuilder)

Example 15 with DefaultContainerConfiguration

use of org.codehaus.plexus.DefaultContainerConfiguration in project sts4 by spring-projects.

the class MavenBridge method newPlexusContainer.

private static DefaultPlexusContainer newPlexusContainer() throws PlexusContainerException {
    final ClassWorld classWorld = new ClassWorld(MAVEN_CORE_REALM_ID, ClassWorld.class.getClassLoader());
    final ClassRealm realm;
    try {
        realm = classWorld.getRealm(MAVEN_CORE_REALM_ID);
    } catch (NoSuchRealmException e) {
        throw new PlexusContainerException("Could not lookup required class realm", e);
    }
    final ContainerConfiguration mavenCoreCC = // 
    new DefaultContainerConfiguration().setClassWorld(// 
    classWorld).setRealm(// 
    realm).setClassPathScanning(// 
    PlexusConstants.SCANNING_INDEX).setAutoWiring(// 
    true).setName(// $NON-NLS-1$
    "mavenCore");
    final Module logginModule = new AbstractModule() {

        protected void configure() {
            bind(ILoggerFactory.class).toInstance(LoggerFactory.getILoggerFactory());
        }
    };
    final Module coreExportsModule = new AbstractModule() {

        protected void configure() {
            ClassRealm realm = mavenCoreCC.getRealm();
            CoreExtensionEntry entry = CoreExtensionEntry.discoverFrom(realm);
            CoreExports exports = new CoreExports(entry);
            bind(CoreExports.class).toInstance(exports);
        }
    };
    return new DefaultPlexusContainer(mavenCoreCC, logginModule, coreExportsModule);
}
Also used : NoSuchRealmException(org.codehaus.plexus.classworlds.realm.NoSuchRealmException) PlexusContainerException(org.codehaus.plexus.PlexusContainerException) ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ILoggerFactory(org.slf4j.ILoggerFactory) CoreExports(org.apache.maven.extension.internal.CoreExports) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) CoreExtensionEntry(org.apache.maven.extension.internal.CoreExtensionEntry) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) AbstractModule(com.google.inject.AbstractModule)

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