Search in sources :

Example 1 with Strategy

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

the class ScalaScriptMojo method createScriptClassloader.

private URLClassLoader createScriptClassloader(File scriptDir, Set<String> classpath) throws Exception {
    ClassWorld w = new ClassWorld("zero", null);
    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());
    rScript.importFrom("mojo", "org.apache.maven");
    // add the script directory to the classpath
    rScript.addURL(scriptDir.toURI().toURL());
    for (String string : classpath) {
        rScript.addURL(new File(string).toURI().toURL());
    }
    return rScript;
}
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)

Example 2 with Strategy

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

Aggregations

File (java.io.File)2 ClassWorld (org.codehaus.plexus.classworlds.ClassWorld)2 ClassRealm (org.codehaus.plexus.classworlds.realm.ClassRealm)2 SelfFirstStrategy (org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy)2 Strategy (org.codehaus.plexus.classworlds.strategy.Strategy)2 Test (org.junit.Test)1