1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.tools.jlink.internal.packager; 27 28 29 import jdk.tools.jlink.builder.DefaultImageBuilder; 30 import jdk.tools.jlink.internal.Jlink; 31 import jdk.tools.jlink.internal.JlinkTask; 32 import jdk.tools.jlink.plugin.Plugin; 33 34 import java.io.File; 35 import java.io.IOException; 36 import java.lang.module.ModuleFinder; 37 import java.nio.file.Path; 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.List; 41 import java.util.Map; 42 import java.util.Set; 43 44 /** 45 * AppRuntimeImageBuilder is a private API used only by the Java Packager to generate 46 * a Java runtime image using jlink. AppRuntimeImageBuilder encapsulates the 47 * arguments that jlink requires to generate this image. To create the image call the 48 * build() method. 49 */ 50 public final class AppRuntimeImageBuilder { 51 private Path outputDir = null; 52 private Map<String, String> launchers = Collections.emptyMap(); 53 private List<Path> modulePath = null; 54 private Set<String> addModules = null; 55 private Set<String> limitModules = null; 56 private String excludeFileList = null; 57 private Map<String, String> userArguments = null; 58 private Boolean stripNativeCommands = null; 59 60 public AppRuntimeImageBuilder() {} 61 62 public void setOutputDir(Path value) { 63 outputDir = value; 64 } 65 66 public void setLaunchers(Map<String, String> value) { 67 launchers = value; 68 } 69 70 public void setModulePath(List<Path> value) { 71 modulePath = value; 72 } 73 74 public void setAddModules(Set<String> value) { 75 addModules = value; 76 } 77 78 public void setLimitModules(Set<String> value) { 79 limitModules = value; 80 } 81 82 public void setExcludeFileList(String value) { 83 excludeFileList = value; 84 } 85 86 public void setStripNativeCommands(boolean value) { 87 stripNativeCommands = value; 88 } 89 90 public void setUserArguments(Map<String, String> value) { 91 userArguments = value; 92 } 93 94 public void build() throws IOException { 95 // jlink main arguments 96 Jlink.JlinkConfiguration jlinkConfig = new Jlink.JlinkConfiguration( 97 new File("").toPath(), // Unused 98 modulePath, addModules, limitModules); 99 100 // plugin configuration 101 List<Plugin> plugins = new ArrayList<Plugin>(); 102 103 if (stripNativeCommands) { 104 plugins.add(Jlink.newPlugin( 105 "strip-native-commands", 106 Collections.singletonMap("strip-native-commands", "on"), 107 null)); 108 } 109 110 if (excludeFileList != null && !excludeFileList.isEmpty()) { 111 plugins.add(Jlink.newPlugin( 112 "exclude-files", 113 Collections.singletonMap("exclude-files", excludeFileList), 114 null)); 115 } 116 117 // add user supplied jlink arguments 118 for (Map.Entry<String, String> entry : userArguments.entrySet()) { 119 String key = entry.getKey(); 120 String value = entry.getValue(); 121 plugins.add(Jlink.newPlugin(key, 122 Collections.singletonMap(key, value), 123 null)); 124 } 125 126 // build the image 127 Jlink.PluginsConfiguration pluginConfig = new Jlink.PluginsConfiguration( 128 plugins, new DefaultImageBuilder(outputDir, launchers), null); 129 Jlink jlink = new Jlink(); 130 jlink.build(jlinkConfig, pluginConfig); 131 } 132 133 /* 134 * Returns a ModuleFinder that limits observability to the given root 135 * modules, their transitive dependences, plus a set of other modules. 136 */ 137 public static ModuleFinder moduleFinder(List<Path> modulepaths, 138 Set<String> roots, 139 Set<String> otherModules) { 140 return JlinkTask.newModuleFinder(modulepaths, roots, otherModules); 141 } 142 }