Skip to content

Commit

Permalink
Change context classloader when starting clojure runtime
Browse files Browse the repository at this point in the history
If running under maven 3.8.4, the clojure extension dies when it's
trying to initialize clojure runtime. Unlike in #117, this does not
happen during compilation of clojure sources, but rather when reading
`pom.clj`.

The fix changes the context classloader of the current thread from
whatever maven now sets it to to a classloader known to have clojure
available. It's copied essentially verbatim from [this SO
answer](https://stackoverflow.com/a/13711141/6564029).
  • Loading branch information
Michail Pevnev authored and jvanzyl committed Feb 10, 2022
1 parent 7426006 commit 8f0fb13
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ public class ClojureModelReader extends ModelReaderSupport {
public Model read(final Reader input, final Map<String, ?> options) throws IOException {
assert input != null;

Thread currentThread = Thread.currentThread();
ClassLoader originalCL = currentThread.getContextClassLoader();
ClassLoader classloaderWithClojure = this.getClass().getClassLoader();
try {
String location = PolyglotModelUtil.getLocation(options);
currentThread.setContextClassLoader(classloaderWithClojure);

String location = PolyglotModelUtil.getLocation(options);
final Var USE = Var.intern(RT.CLOJURE_NS, Symbol.create("use"));
final Symbol READER = Symbol.create("org.sonatype.maven.polyglot.clojure.dsl.reader");
final Symbol LEININGEN = Symbol.create("org.sonatype.maven.polyglot.clojure.dsl.leiningen");
Expand All @@ -47,10 +51,11 @@ public Model read(final Reader input, final Map<String, ?> options) throws IOExc
clojure.lang.Compiler.load(input, location, location);
final Var MODEL = Var.intern(Namespace.findOrCreate(READER), Symbol.create("*MODEL*"));
return (Model) ((Atom) MODEL.get()).deref();
}
catch (Exception e) {
} catch (Exception e) {
// Don't use new IOException(e) because it doesn't exist in Java 5
throw (IOException) new IOException(e.toString()).initCause(e);
} finally {
currentThread.setContextClassLoader(originalCL);
}
}
}

0 comments on commit 8f0fb13

Please sign in to comment.