use of org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy 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;
}
use of org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy 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);
}
}
Aggregations