Skip to content

Commit

Permalink
Fixes for non-interactive (client/server) mode on Java 9 (#221)
Browse files Browse the repository at this point in the history
* Fixes for client/server mode on Java 9.

* Upgraded ammonite to 1.0.5-4-c0cdbaf.
  • Loading branch information
robby-phd authored and rockjam committed Mar 13, 2018
1 parent ac7eedc commit a9d4eea
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ trait MillModule extends MillPublishModule{ outer =>

object clientserver extends MillModule{
def ivyDeps = Agg(
ivy"com.lihaoyi:::ammonite:1.0.5-4-c0cdbaf",
ivy"org.scala-sbt.ipcsocket:ipcsocket:1.0.0"
)
val test = new Tests(implicitly)
Expand All @@ -83,7 +84,7 @@ object core extends MillModule {

def ivyDeps = Agg(
ivy"com.lihaoyi::sourcecode:0.1.4",
ivy"com.lihaoyi:::ammonite:1.0.5-1-819bc80",
ivy"com.lihaoyi:::ammonite:1.0.5-4-c0cdbaf",
ivy"jline:jline:2.14.5"
)

Expand Down
19 changes: 14 additions & 5 deletions clientserver/src/mill/clientserver/Client.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
package mill.clientserver;

import io.github.retronym.java9rtexport.Export;
import org.scalasbt.ipcsocket.UnixDomainSocket;

import java.io.*;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Properties;

public class Client {
static void initServer(String lockBase) throws IOException{
StringBuilder selfJars = new java.lang.StringBuilder();
ArrayList<String> selfJars = new ArrayList<String>();
ClassLoader current = Client.class.getClassLoader();
while(current != null){
if (current instanceof java.net.URLClassLoader) {
URL[] urls = ((java.net.URLClassLoader) current).getURLs();
for (URL url: urls) {
if (selfJars.length() != 0) selfJars.append(':');
selfJars.append(url);
selfJars.add(url.toString());
}
}
current = current.getParent();
}

if (!System.getProperty("java.specification.version").startsWith("1.")) {
selfJars.addAll(Arrays.asList(System.getProperty("java.class.path").split(File.pathSeparator)));
File rtFile = new File(lockBase + "/rt-" + System.getProperty("java.version") + ".jar");
if (!rtFile.exists()) {
Files.copy(Export.export().toPath(), rtFile.toPath());
}
selfJars.add(rtFile.getCanonicalPath());
}
ArrayList<String> l = new java.util.ArrayList<String>();
l.add("java");
Properties props = System.getProperties();
Expand All @@ -33,7 +42,7 @@ static void initServer(String lockBase) throws IOException{
if (k.startsWith("MILL_")) l.add("-D" + k + "=" + props.getProperty(k));
}
l.add("-cp");
l.add(selfJars.toString());
l.add(String.join(File.pathSeparator, selfJars));
l.add("mill.ServerMain");
l.add(lockBase);
new java.lang.ProcessBuilder()
Expand Down
11 changes: 9 additions & 2 deletions core/src/mill/util/ClassLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import io.github.retronym.java9rtexport.Export

object ClassLoader {
def create(urls: Seq[URL], parent: java.lang.ClassLoader): URLClassLoader = {
val rtOpt = if (ammonite.util.Util.java9OrAbove) Some(Export.export().toURI.toURL) else None
new URLClassLoader((urls ++ rtOpt).toArray, parent)
val cl = new URLClassLoader(urls.toArray, parent)
if (!ammonite.util.Util.java9OrAbove) return cl
try {
cl.loadClass("javax.script.ScriptEngineManager")
cl
} catch {
case _: ClassNotFoundException =>
new URLClassLoader((urls ++ Some(Export.export().toURI.toURL)).toArray, parent)
}
}
}
2 changes: 1 addition & 1 deletion scalalib/src/mill/scalalib/ScalaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ trait ScalaModule extends mill.Module with TaskModule { outer =>
}

def ammoniteReplClasspath = T{
resolveDeps(T.task{Agg(ivy"com.lihaoyi:::ammonite:1.0.3")})()
resolveDeps(T.task{Agg(ivy"com.lihaoyi:::ammonite:1.0.5-4-c0cdbaf")})()
}
def repl() = T.command{
if (T.ctx().log.inStream == DummyInputStream){
Expand Down
4 changes: 2 additions & 2 deletions scalalib/src/mill/scalalib/ScalaWorkerApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ trait ScalaWorkerModule extends mill.Module{
}
}
def worker: Worker[ScalaWorkerApi] = T.worker{
val cl = new java.net.URLClassLoader(
classpath().map(_.toNIO.toUri.toURL).toArray,
val cl = mill.util.ClassLoader.create(
classpath().map(_.toNIO.toUri.toURL).toVector,
getClass.getClassLoader
)
val cls = cl.loadClass("mill.scalaworker.ScalaWorker")
Expand Down

0 comments on commit a9d4eea

Please sign in to comment.