|
| 1 | +/* |
| 2 | + * Copyright 2019, ATTO Technology, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. ATTO Technology designates this |
| 9 | + * particular file as subject to the "Classpath" exception as provided |
| 10 | + * by ATTO Technology in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +package com.oracle.appbundler; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.ArrayList; |
| 29 | + |
| 30 | +import org.apache.tools.ant.BuildException; |
| 31 | +import org.apache.tools.ant.taskdefs.ExecTask; |
| 32 | + |
| 33 | +/** |
| 34 | + * Class representing a module that will be passed to jlink to build the bundled |
| 35 | + * JVM. |
| 36 | + */ |
| 37 | +public class JLink { |
| 38 | + private String runtime = null; |
| 39 | + private ArrayList<String> jmods = new ArrayList<>(); |
| 40 | + private ArrayList<String> arguments = new ArrayList<>(); |
| 41 | + private ExecTask exec = new ExecTask(); |
| 42 | + |
| 43 | + public JLink() { |
| 44 | + exec.init(); |
| 45 | + } |
| 46 | + |
| 47 | + public String getRuntime() { |
| 48 | + return runtime; |
| 49 | + } |
| 50 | + |
| 51 | + public void setRuntime(String runtime) { |
| 52 | + this.runtime = runtime; |
| 53 | + } |
| 54 | + |
| 55 | + public void setTask(AppBundlerTask task) { |
| 56 | + exec.bindToOwner(task); |
| 57 | + } |
| 58 | + |
| 59 | + /* Provide canonical path so that runtime can be specified via a |
| 60 | + * version-agnostic path (relative link, e.g. `current-jre`) while |
| 61 | + * still preserving the original runtime directory name, e.g. |
| 62 | + * `jre1.8.0_45.jre`. |
| 63 | + */ |
| 64 | + public File getDir() { |
| 65 | + File dir = new File(runtime); |
| 66 | + try { |
| 67 | + return dir.getCanonicalFile(); |
| 68 | + } catch (IOException e) { |
| 69 | + return dir; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void addConfiguredJMod(JMod jmod) throws BuildException { |
| 74 | + String name = jmod.getName(); |
| 75 | + |
| 76 | + if (name == null) { |
| 77 | + throw new BuildException("Name is required."); |
| 78 | + } |
| 79 | + |
| 80 | + jmods.add(name); |
| 81 | + } |
| 82 | + |
| 83 | + public void addConfiguredArgument(Argument argument) throws BuildException { |
| 84 | + String value = argument.getValue(); |
| 85 | + |
| 86 | + if (value == null) { |
| 87 | + throw new BuildException("Value is required."); |
| 88 | + } |
| 89 | + |
| 90 | + arguments.add(value); |
| 91 | + } |
| 92 | + |
| 93 | + public void copyTo(File targetDir) throws IOException { |
| 94 | + File runtimeHomeDirectory = getDir(); |
| 95 | + File runtimeContentsDirectory = runtimeHomeDirectory.getParentFile(); |
| 96 | + File runtimeDirectory = runtimeContentsDirectory.getParentFile(); |
| 97 | + |
| 98 | + // Create root plug-in directory |
| 99 | + File pluginDirectory = new File(targetDir, runtimeDirectory.getName()); |
| 100 | + pluginDirectory.mkdir(); |
| 101 | + |
| 102 | + // Create Contents directory |
| 103 | + File pluginContentsDirectory = new File(pluginDirectory, runtimeContentsDirectory.getName()); |
| 104 | + pluginContentsDirectory.mkdir(); |
| 105 | + |
| 106 | + // Copy MacOS directory |
| 107 | + File runtimeMacOSDirectory = new File(runtimeContentsDirectory, "MacOS"); |
| 108 | + AppBundlerTask.copy(runtimeMacOSDirectory, new File(pluginContentsDirectory, runtimeMacOSDirectory.getName())); |
| 109 | + |
| 110 | + |
| 111 | + // Copy Info.plist file |
| 112 | + File runtimeInfoPlistFile = new File(runtimeContentsDirectory, "Info.plist"); |
| 113 | + AppBundlerTask.copy(runtimeInfoPlistFile, new File(pluginContentsDirectory, runtimeInfoPlistFile.getName())); |
| 114 | + |
| 115 | + // Copy included contents of Home directory |
| 116 | + File pluginHomeDirectory = new File(pluginContentsDirectory, runtimeHomeDirectory.getName()); |
| 117 | + |
| 118 | + exec.setExecutable(runtimeHomeDirectory.getAbsolutePath() + "/bin/jlink"); |
| 119 | + exec.setFailIfExecutionFails(true); |
| 120 | + exec.setFailonerror(true); |
| 121 | + for(String s : this.arguments) { |
| 122 | + exec.createArg().setValue(s); |
| 123 | + } |
| 124 | + |
| 125 | + exec.createArg().setValue("--no-man-pages"); |
| 126 | + exec.createArg().setValue("--no-header-files"); |
| 127 | + exec.createArg().setValue("--strip-native-commands"); /* no bin directory */ |
| 128 | + exec.createArg().setValue("--add-modules"); |
| 129 | + exec.createArg().setValue(String.join(",", jmods)); |
| 130 | + exec.createArg().setValue("--output"); |
| 131 | + exec.createArg().setValue(pluginHomeDirectory.getAbsolutePath()); |
| 132 | + |
| 133 | + exec.execute(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public String toString() { |
| 138 | + return runtime; |
| 139 | + } |
| 140 | +} |
0 commit comments