Skip to content

Commit c5e8e70

Browse files
committed
BuildEnvironmentMojo: Download Java from imagej.net
1 parent bd20870 commit c5e8e70

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ Institute of Molecular Cell Biology and Genetics, and KNIME GmbH.</license.copyr
200200
<version>3.0.1</version>
201201
</dependency>
202202

203+
<dependency>
204+
<groupId>org.apache.commons</groupId>
205+
<artifactId>commons-compress</artifactId>
206+
</dependency>
207+
203208
<dependency>
204209
<groupId>org.codehaus.plexus</groupId>
205210
<artifactId>plexus-interpolation</artifactId>

src/main/java/org/scijava/maven/plugin/install/BuildEnvironmentMojo.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void execute() throws MojoExecutionException {
7474

7575
if (appDirectory == null) {
7676
if (hasIJ1Dependency(project)) getLog().info(
77-
"Property '" + appDirectoryProperty + "' unset; Skipping copy-jars");
77+
"Property '" + appDirectoryProperty + "' unset; Skipping build-environment");
7878
return;
7979
}
8080
final String interpolated = interpolate(appDirectory, project, session);
@@ -89,7 +89,7 @@ public void execute() throws MojoExecutionException {
8989
getLog().warn(
9090
"'" + appDirectory + "'" +
9191
(interpolated.equals(appDirectory) ? "" : " (" + appDirectory + ")") +
92-
" is not an SciJava application directory; Skipping copy-jars");
92+
" is not an SciJava application directory; Skipping build-environment");
9393
return;
9494
}
9595

@@ -128,5 +128,10 @@ public void execute() throws MojoExecutionException {
128128
throw new MojoExecutionException(
129129
"Couldn't resolve dependencies for artifact: " + e.getMessage(), e);
130130
}
131+
try {
132+
JavaDownloader.downloadJava(appDir);
133+
} catch (IOException e) {
134+
e.printStackTrace();
135+
}
131136
}
132137
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.scijava.maven.plugin.install;
2+
3+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
4+
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
5+
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
6+
import org.apache.commons.io.IOUtils;
7+
8+
import java.io.BufferedInputStream;
9+
import java.io.File;
10+
import java.io.FileOutputStream;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.net.MalformedURLException;
14+
import java.net.URL;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
18+
19+
public class JavaDownloader {
20+
21+
public static void downloadJava(File destinationDir) throws IOException {
22+
System.out.println("Downloading and unpacking JRE..");
23+
File javaFolder = new File(destinationDir, "java");
24+
javaFolder.mkdirs();
25+
URL javaURL = getJavaDownloadURL();
26+
decompress(javaURL.openStream(), javaFolder);
27+
File[] files = javaFolder.listFiles();
28+
if(files.length != 1) {
29+
System.err.println("Something went wrong during JRE download");
30+
return;
31+
}
32+
File jre = files[0];
33+
String jdkName = jre.getName().replace("jre", "jdk");
34+
Path newJrePath = Paths.get(javaFolder.getAbsolutePath(), getJavaDownloadPlatform(), jdkName, "jre");
35+
newJrePath.toFile().getParentFile().mkdirs();
36+
Files.move(jre.toPath(), newJrePath);
37+
System.out.println("JRE installed to " + newJrePath.toAbsolutePath());
38+
}
39+
40+
private static void decompress(InputStream in, File out) throws IOException {
41+
try (TarArchiveInputStream fin = new TarArchiveInputStream(
42+
new GzipCompressorInputStream(new BufferedInputStream(in)))){
43+
TarArchiveEntry entry;
44+
while ((entry = fin.getNextTarEntry()) != null) {
45+
if (entry.isDirectory()) {
46+
continue;
47+
}
48+
File curfile = new File(out, entry.getName());
49+
File parent = curfile.getParentFile();
50+
if (!parent.exists()) {
51+
parent.mkdirs();
52+
}
53+
IOUtils.copy(fin, new FileOutputStream(curfile));
54+
}
55+
}
56+
}
57+
58+
private static String getJavaDownloadPlatform() {
59+
final boolean is64bit =
60+
System.getProperty("os.arch", "").contains("64");
61+
final String osName = System.getProperty("os.name", "<unknown>");
62+
if (osName.equals("Linux")) return "linux" + (is64bit ? "-amd64" : "");
63+
if (osName.equals("Mac OS X")) return "macosx";
64+
if (osName.startsWith("Windows")) return "win" + (is64bit ? "64" : "32");
65+
throw new RuntimeException("No JRE for platform exists");
66+
}
67+
68+
private static URL getJavaDownloadURL() throws MalformedURLException {
69+
return new URL("https://downloads.imagej.net/java/" + getJavaDownloadPlatform() + ".tar.gz");
70+
}
71+
}

0 commit comments

Comments
 (0)