Skip to content

Commit f77c154

Browse files
committed
add JDK 9 support
1 parent 5252119 commit f77c154

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

.java-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.8

build.sbt

+17-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ val root = (project in file(".")).
105105
// Universal ZIP download install.
106106
packageName in Universal := packageName.value, // needs to be set explicitly due to a bug in native-packager
107107
version in Universal := sbtVersionToRelease,
108-
mappings in Universal <+= sbtLaunchJar map { _ -> "bin/sbt-launch.jar" },
108+
mappings in Universal ++= {
109+
val launchJar = sbtLaunchJar.value
110+
val rtExportJar = (packageBin in Compile in java9rtexport).value
111+
Seq(launchJar -> "bin/sbt-launch.jar", rtExportJar -> "bin/java9-rt-export.jar")
112+
},
109113

110114
// Misccelaneous publishing stuff...
111115
projectID in Debian <<= moduleID,
@@ -117,6 +121,18 @@ val root = (project in file(".")).
117121
projectID in Universal <<= moduleID
118122
)
119123

124+
lazy val java9rtexport = (project in file("java9-rt-export"))
125+
.settings(
126+
name := "java9-rt-export",
127+
autoScalaLibrary := false,
128+
crossPaths := false,
129+
description := "Exports the contents of the Java 9. JEP-220 runtime image to a JAR for compatibility with older tools.",
130+
homepage := Some(url("http://github.com/retronym/" + name.value)),
131+
startYear := Some(2017),
132+
licenses += ("Scala license", url(homepage.value.get.toString + "/blob/master/LICENSE")),
133+
mainClass in Compile := Some("io.github.retronym.java9rtexport.Export")
134+
)
135+
120136
def downloadUrlForVersion(v: String) = (v split "[^\\d]" flatMap (i => catching(classOf[Exception]) opt (i.toInt))) match {
121137
case Array(0, 11, 3, _*) => "http://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/0.11.3-2/sbt-launch.jar"
122138
case Array(0, 11, x, _*) if x >= 3 => "http://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/"+v+"/sbt-launch.jar"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.retronym.java9rtexport;
2+
3+
import java.io.IOException;
4+
import java.nio.ByteBuffer;
5+
import java.nio.file.*;
6+
import java.nio.file.attribute.*;
7+
import java.util.EnumSet;
8+
9+
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
10+
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
11+
12+
public class Copy {
13+
public static void copyDirectory(final Path source, final Path target)
14+
throws IOException {
15+
Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS),
16+
Integer.MAX_VALUE, new FileVisitor<Path>() {
17+
18+
@Override
19+
public FileVisitResult preVisitDirectory(Path dir,
20+
BasicFileAttributes sourceBasic) throws IOException {
21+
22+
String relative = source.relativize(dir).toString();
23+
if (!Files.exists(target.getFileSystem().getPath(relative)))
24+
Files.createDirectory(target.getFileSystem().getPath(relative));
25+
return FileVisitResult.CONTINUE;
26+
}
27+
28+
@Override
29+
public FileVisitResult visitFile(Path file,
30+
BasicFileAttributes attrs) throws IOException {
31+
String relative = source.relativize(file).toString();
32+
Files.copy(file, target.getFileSystem().getPath(relative), COPY_ATTRIBUTES, REPLACE_EXISTING);
33+
return FileVisitResult.CONTINUE;
34+
}
35+
36+
@Override
37+
public FileVisitResult visitFileFailed(Path file, IOException e) throws IOException {
38+
throw e;
39+
}
40+
41+
@Override
42+
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
43+
if (e != null) throw e;
44+
return FileVisitResult.CONTINUE;
45+
}
46+
});
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.github.retronym.java9rtexport;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.nio.file.*;
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.util.Map;
9+
10+
public class Export {
11+
public static void main(String[] args) {
12+
try {
13+
14+
if (args.length == 0) {
15+
System.err.println("Usage:");
16+
System.err.println(" java -jar java9-rt-export-*.jar $HOME/.sbt/java9-rt-ext/rt.jar");
17+
System.err.println(" Exports rt.jar to the specified path.");
18+
System.err.println("");
19+
System.err.println(" java -jar java9-rt-export-*.jar --global-base");
20+
System.err.println(" Prints sbt global base.");
21+
System.exit(-1);
22+
}
23+
String destination = args[0];
24+
if (destination.equals("--global-base")) {
25+
Path defaultGlobalBase = Paths.get(System.getProperty("user.home"), ".sbt", "0.13");
26+
String globalBase = System.getProperty("sbt.global.base", defaultGlobalBase.toString());
27+
System.out.println(globalBase);
28+
System.exit(0);
29+
}
30+
FileSystem fileSystem = FileSystems.getFileSystem(URI.create("jrt:/"));
31+
Path path = fileSystem.getPath("/modules");
32+
Path destPath = Paths.get(destination);
33+
URI uri = URI.create( "jar:" + destPath.toUri() );
34+
Map<String, String> env = new HashMap<>();
35+
env.put( "create", "true" );
36+
try ( FileSystem zipfs = FileSystems.newFileSystem( uri, env ) ) {
37+
Iterator<Path> iterator = Files.list(path).iterator();
38+
while(iterator.hasNext()) {
39+
Path next = iterator.next();
40+
Copy.copyDirectory(next, zipfs.getPath("/"));
41+
}
42+
}
43+
} catch (IOException e) {
44+
e.printStackTrace();
45+
System.exit(-1);
46+
}
47+
}
48+
}

src/universal/bin/sbt-launch-lib.bash

+31-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ acquire_sbt_jar () {
3838
fi
3939
}
4040

41+
rt_export_file () {
42+
echo "${sbt_bin_dir}/java9-rt-export.jar"
43+
}
44+
4145
execRunner () {
4246
# print the arguments one to a line, quoting any containing spaces
4347
[[ $verbose || $debug ]] && echo "# Executing command line:" && {
@@ -147,8 +151,16 @@ process_args () {
147151
process_my_args "${myargs[@]}"
148152
}
149153

150-
java_version=$("$java_cmd" -Xmx512M -version 2>&1 | sed 's/.*version "\([0-9]*\)\.\([0-9]*\)\..*"/\1.\2/; 1q')
154+
## parses 1.7, 1.8, 9, etc out of java version "1.8.0_91"
155+
java_version=$("$java_cmd" -Xmx512M -version 2>&1 | sed 's/.*version "\([0-9]*\)\(\.[0-9]*\)\{0,1\}\(.*\)*"/\1\2/; 1q')
151156
vlog "[process_args] java_version = '$java_version'"
157+
rtexport=$(rt_export_file)
158+
sbt_global_dir=$("$java_cmd" ${JAVA_OPTS} ${SBT_OPTS:-$default_sbt_opts} ${java_args[@]} \
159+
-jar "$rtexport" --global-base)
160+
java9_ext=$(echo "$sbt_global_dir/java9-rt-ext")
161+
java9_rt=$(echo "$java9_ext/rt.jar")
162+
vlog "[process_args] sbt_global_dir = '$sbt_global_dir'"
163+
vlog "[process_args] java9_rt = '$java9_rt'"
152164
}
153165

154166
# Detect that we have java installed.
@@ -174,6 +186,21 @@ checkJava() {
174186
fi
175187
}
176188

189+
copyRt() {
190+
if [[ "$java_version" > "8" ]] && [[ ! -f "$java9_rt" ]]; then
191+
echo Copying runtime jar.
192+
execRunner "$java_cmd" \
193+
${JAVA_OPTS} \
194+
${SBT_OPTS:-$default_sbt_opts} \
195+
${java_args[@]} \
196+
-jar "$rtexport" \
197+
"${java9_rt}"
198+
fi
199+
if [[ "$java_version" > "8" ]]; then
200+
addJava "-Dscala.ext.dirs=${java9_ext}"
201+
fi
202+
}
203+
177204

178205
run() {
179206
# no jar? download it.
@@ -191,6 +218,9 @@ run() {
191218
# TODO - java check should be configurable...
192219
checkJava "1.6"
193220

221+
# Java 9 support
222+
copyRt
223+
194224
#If we're in cygwin, we should use the windows config, and terminal hacks
195225
if [[ "$CYGWIN_FLAG" == "true" ]]; then
196226
stty -icanon min 1 -echo > /dev/null 2>&1

0 commit comments

Comments
 (0)