Search in sources :

Example 11 with ClassWorld

use of org.codehaus.plexus.classworlds.ClassWorld 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 12 with ClassWorld

use of org.codehaus.plexus.classworlds.ClassWorld in project scala-maven-plugin by davidB.

the class MiscTest method classworldSeftFirstStrategy.

@Test
public void classworldSeftFirstStrategy() throws Exception {
    ClassWorld w = new ClassWorld("zero", null);
    ClassRealm rMojo = w.newRealm("mojo", getClass().getClassLoader());
    Strategy s = new SelfFirstStrategy(w.newRealm("scalaScript", null));
    ClassRealm rScript = s.getRealm();
    rScript.setParentClassLoader(getClass().getClassLoader());
    rScript.importFrom("mojo", MavenProject.class.getPackage().getName());
    rScript.importFrom("mojo", MavenSession.class.getPackage().getName());
    rScript.importFrom("mojo", Log.class.getPackage().getName());
    assertEquals(rScript, rScript.getStrategy().getRealm());
    assertEquals(SelfFirstStrategy.class, rScript.getStrategy().getClass());
    File olderjar = new File(System.getProperty("user.home"), ".m2/repository/net/alchim31/maven/scala-maven-plugin/3.1.0/scala-maven-plugin-3.1.0.jar");
    if (olderjar.exists()) {
        System.out.println("found older jar");
        rScript.addURL(olderjar.toURI().toURL());
        String clname = "scala_maven.ScalaScriptMojo";
        // assertNotSame(s.loadClass(clname), getClass().getClassLoader().loadClass(clname));
        assertNotSame(rScript.loadClass(clname), getClass().getClassLoader().loadClass(clname));
        assertSame(rMojo.loadClass(clname), getClass().getClassLoader().loadClass(clname));
        assertSame(rScript.loadClass(MavenProject.class.getCanonicalName()), MavenProject.class);
        assertSame(rScript.loadClass(MavenSession.class.getCanonicalName()), MavenSession.class);
        assertSame(rScript.loadClass(Log.class.getCanonicalName()), Log.class);
    }
}
Also used : ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) SelfFirstStrategy(org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy) SelfFirstStrategy(org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy) Strategy(org.codehaus.plexus.classworlds.strategy.Strategy) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) File(java.io.File) Test(org.junit.Test)

Example 13 with ClassWorld

use of org.codehaus.plexus.classworlds.ClassWorld 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 14 with ClassWorld

use of org.codehaus.plexus.classworlds.ClassWorld 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)

Example 15 with ClassWorld

use of org.codehaus.plexus.classworlds.ClassWorld in project aries by apache.

the class GenerateMojo method createProjectScopeFinder.

private ClassFinder createProjectScopeFinder() throws Exception {
    List<URL> urls = new ArrayList<>();
    long startTime = System.currentTimeMillis();
    ClassRealm classRealm = new ClassRealm(new ClassWorld(), "maven-blueprint-plugin-classloader", getClass().getClassLoader());
    classRealm.addURL(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
    urls.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
    ArtifactFilter artifactFilter = new ArtifactFilter(includeArtifacts, excludeArtifacts);
    for (Object artifactO : project.getArtifacts()) {
        Artifact artifact = (Artifact) artifactO;
        File file = artifact.getFile();
        if (file == null) {
            continue;
        }
        URL artifactUrl = file.toURI().toURL();
        classRealm.addURL(artifactUrl);
        if (artifactFilter.shouldExclude(artifact)) {
            getLog().debug("Excluded artifact: " + artifact);
            continue;
        }
        getLog().debug("Taken artifact: " + artifact);
        urls.add(artifactUrl);
    }
    getLog().debug(" Create class loader: " + (System.currentTimeMillis() - startTime) + "ms");
    startTime = System.currentTimeMillis();
    ClassFinder classFinder = new ClassFinder(classRealm, urls);
    getLog().debug(" Building class finder: " + (System.currentTimeMillis() - startTime) + "ms");
    return classFinder;
}
Also used : ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) ArrayList(java.util.ArrayList) ClassFinder(org.apache.xbean.finder.ClassFinder) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) File(java.io.File) URL(java.net.URL) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

ClassWorld (org.codehaus.plexus.classworlds.ClassWorld)15 ClassRealm (org.codehaus.plexus.classworlds.realm.ClassRealm)9 DefaultContainerConfiguration (org.codehaus.plexus.DefaultContainerConfiguration)8 DefaultPlexusContainer (org.codehaus.plexus.DefaultPlexusContainer)6 File (java.io.File)5 ContainerConfiguration (org.codehaus.plexus.ContainerConfiguration)4 PlexusContainerException (org.codehaus.plexus.PlexusContainerException)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 LinkedHashSet (java.util.LinkedHashSet)2 Properties (java.util.Properties)2 ProjectBuilder (org.apache.maven.project.ProjectBuilder)2 SelfFirstStrategy (org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy)2 Strategy (org.codehaus.plexus.classworlds.strategy.Strategy)2 AbstractModule (com.google.inject.AbstractModule)1 Module (com.google.inject.Module)1 FilenameFilter (java.io.FilenameFilter)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1