|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
22 | 22 | *
|
23 | 23 | */
|
24 | 24 |
|
25 |
| -/* |
26 |
| - * @summary Simple jar builder |
27 |
| - * Input: jarName className1 className2 ... |
28 |
| - * do not specify extensions, just the names |
29 |
| - * E.g. prot_domain ProtDomainA ProtDomainB |
30 |
| - * Output: A jar containing compiled classes, placed in a test classes folder |
31 |
| - * @library /open/test/lib |
32 |
| - */ |
33 |
| - |
34 |
| -import jdk.test.lib.JDKToolFinder; |
35 |
| -import jdk.test.lib.cds.CDSTestUtils; |
36 |
| -import jdk.test.lib.compiler.CompilerUtils; |
37 |
| -import jdk.test.lib.process.OutputAnalyzer; |
38 |
| -import jdk.test.lib.process.ProcessTools; |
39 |
| -import jdk.test.lib.helpers.ClassFileInstaller; |
40 |
| -import java.io.File; |
41 |
| -import java.nio.file.Path; |
42 |
| -import java.util.ArrayList; |
43 |
| -import java.util.spi.ToolProvider; |
44 |
| - |
45 |
| -public class JarBuilder { |
46 |
| - // to turn DEBUG on via command line: -DJarBuilder.DEBUG=[true, TRUE] |
47 |
| - private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("JarBuilder.DEBUG", "false")); |
48 |
| - private static final String classDir = System.getProperty("test.classes"); |
49 |
| - private static final ToolProvider JAR = ToolProvider.findFirst("jar") |
50 |
| - .orElseThrow(() -> new RuntimeException("ToolProvider for jar not found")); |
51 |
| - |
52 |
| - public static String getJarFilePath(String jarName) { |
53 |
| - return CDSTestUtils.getOutputDir() + File.separator + jarName + ".jar"; |
54 |
| - } |
55 |
| - |
56 |
| - // jar all files under dir, with manifest file man, with an optional versionArgs |
57 |
| - // for generating a multi-release jar. |
58 |
| - // The jar command is as follows: |
59 |
| - // jar cmf \ |
60 |
| - // <path to output jar> <path to the manifest file>\ |
61 |
| - // -C <path to the base classes> .\ |
62 |
| - // --release 9 -C <path to the versioned classes> . |
63 |
| - // the last line begins with "--release" corresponds to the optional versionArgs. |
64 |
| - public static String build(String jarName, File dir, String man, String ...versionArgs) |
65 |
| - throws Exception { |
66 |
| - ArrayList<String> args = new ArrayList<String>(); |
67 |
| - if (man != null) { |
68 |
| - args.add("cfm"); |
69 |
| - } else { |
70 |
| - args.add("cf"); |
71 |
| - } |
72 |
| - String jarFile = getJarFilePath(jarName); |
73 |
| - args.add(jarFile); |
74 |
| - if (man != null) { |
75 |
| - args.add(man); |
76 |
| - } |
77 |
| - args.add("-C"); |
78 |
| - args.add(dir.getAbsolutePath()); |
79 |
| - args.add("."); |
80 |
| - for (String verArg : versionArgs) { |
81 |
| - args.add(verArg); |
82 |
| - } |
83 |
| - createJar(args); |
84 |
| - return jarFile; |
85 |
| - } |
| 25 | +import jdk.test.lib.cds.CDSJarUtils; |
86 | 26 |
|
87 |
| - public static String build(String jarName, String ...classNames) |
88 |
| - throws Exception { |
89 |
| - |
90 |
| - return createSimpleJar(classDir, getJarFilePath(jarName), classNames); |
91 |
| - } |
92 |
| - |
93 |
| - public static String build(boolean classesInWorkDir, String jarName, String ...classNames) |
94 |
| - throws Exception { |
95 |
| - if (classesInWorkDir) { |
96 |
| - return createSimpleJar(".", getJarFilePath(jarName), classNames); |
97 |
| - } else { |
98 |
| - return build(jarName, classNames); |
99 |
| - } |
100 |
| - } |
101 |
| - |
102 |
| - |
103 |
| - public static String buildWithManifest(String jarName, String manifest, |
104 |
| - String jarClassesDir, String ...classNames) throws Exception { |
105 |
| - String jarPath = getJarFilePath(jarName); |
106 |
| - ArrayList<String> args = new ArrayList<String>(); |
107 |
| - args.add("cvfm"); |
108 |
| - args.add(jarPath); |
109 |
| - args.add(System.getProperty("test.src") + File.separator + "test-classes" |
110 |
| - + File.separator + manifest); |
111 |
| - addClassArgs(args, jarClassesDir, classNames); |
112 |
| - createJar(args); |
113 |
| - |
114 |
| - return jarPath; |
115 |
| - } |
116 |
| - |
117 |
| - |
118 |
| - // Execute: jar uvf $jarFile -C $dir . |
119 |
| - static void update(String jarFile, String dir) throws Exception { |
120 |
| - String jarExe = JDKToolFinder.getJDKTool("jar"); |
121 |
| - |
122 |
| - ArrayList<String> args = new ArrayList<>(); |
123 |
| - args.add(jarExe); |
124 |
| - args.add("uvf"); |
125 |
| - args.add(jarFile); |
126 |
| - args.add("-C"); |
127 |
| - args.add(dir); |
128 |
| - args.add("."); |
129 |
| - |
130 |
| - executeProcess(args.toArray(new String[1])); |
131 |
| - } |
132 |
| - |
133 |
| - private static String createSimpleJar(String jarclassDir, String jarName, |
134 |
| - String[] classNames) throws Exception { |
135 |
| - |
136 |
| - ArrayList<String> args = new ArrayList<String>(); |
137 |
| - args.add("cf"); |
138 |
| - args.add(jarName); |
139 |
| - addClassArgs(args, jarclassDir, classNames); |
140 |
| - createJar(args); |
141 |
| - |
142 |
| - return jarName; |
143 |
| - } |
144 |
| - |
145 |
| - private static void addClassArgs(ArrayList<String> args, String jarclassDir, |
146 |
| - String[] classNames) { |
147 |
| - |
148 |
| - for (String name : classNames) { |
149 |
| - args.add("-C"); |
150 |
| - args.add(jarclassDir); |
151 |
| - args.add(name + ".class"); |
152 |
| - } |
153 |
| - } |
154 | 27 |
|
| 28 | +/* |
| 29 | + * This class is deprecated and should not be used by any new test cases. Use CDSJarUtils |
| 30 | + * and jdk.test.lib.cds.CDSModulePackager instead. |
| 31 | + */ |
| 32 | +public class JarBuilder extends CDSJarUtils { |
155 | 33 | public static void createModularJar(String jarPath,
|
156 |
| - String classesDir, |
157 |
| - String mainClass) throws Exception { |
158 |
| - ArrayList<String> argList = new ArrayList<String>(); |
159 |
| - argList.add("--create"); |
160 |
| - argList.add("--file=" + jarPath); |
161 |
| - if (mainClass != null) { |
162 |
| - argList.add("--main-class=" + mainClass); |
163 |
| - } |
164 |
| - argList.add("-C"); |
165 |
| - argList.add(classesDir); |
166 |
| - argList.add("."); |
167 |
| - createJar(argList); |
| 34 | + String classesDir, |
| 35 | + String mainClass) throws Exception { |
| 36 | + createModularJarWithManifest(jarPath, classesDir, mainClass, null); |
168 | 37 | }
|
169 | 38 |
|
170 | 39 | public static void createModularJarWithManifest(String jarPath,
|
171 | 40 | String classesDir,
|
172 | 41 | String mainClass,
|
173 | 42 | String manifest) throws Exception {
|
174 |
| - ArrayList<String> argList = new ArrayList<String>(); |
175 |
| - argList.add("--create"); |
176 |
| - argList.add("--file=" + jarPath); |
177 |
| - if (mainClass != null) { |
178 |
| - argList.add("--main-class=" + mainClass); |
179 |
| - } |
180 |
| - argList.add("--manifest=" + manifest); |
181 |
| - argList.add("-C"); |
182 |
| - argList.add(classesDir); |
183 |
| - argList.add("."); |
184 |
| - createJar(argList); |
185 |
| - } |
186 |
| - |
187 |
| - private static void createJar(ArrayList<String> args) { |
188 |
| - if (DEBUG) printIterable("createJar args: ", args); |
189 |
| - |
190 |
| - if (JAR.run(System.out, System.err, args.toArray(new String[1])) != 0) { |
191 |
| - throw new RuntimeException("jar operation failed"); |
192 |
| - } |
193 |
| - } |
194 |
| - |
195 |
| - // Many AppCDS tests use the same simple "hello.jar" which contains |
196 |
| - // simple Hello.class and does not specify additional attributes. |
197 |
| - // For this common use case, use this method to get the jar path. |
198 |
| - // The method will check if the jar already exists |
199 |
| - // (created by another test or test run), and will create the jar |
200 |
| - // if it does not exist |
201 |
| - public static String getOrCreateHelloJar() throws Exception { |
202 |
| - String jarPath = getJarFilePath("hello"); |
203 |
| - |
204 |
| - File jarFile = new File(jarPath); |
205 |
| - if (jarFile.exists()) { |
206 |
| - return jarPath; |
207 |
| - } else { |
208 |
| - return build("hello", "Hello"); |
209 |
| - } |
210 |
| - } |
211 |
| - |
212 |
| - public static void compile(String dstPath, String source, String... extraArgs) throws Exception { |
213 |
| - ArrayList<String> args = new ArrayList<String>(); |
214 |
| - args.add(JDKToolFinder.getCompileJDKTool("javac")); |
215 |
| - args.add("-d"); |
216 |
| - args.add(dstPath); |
217 |
| - if (extraArgs != null) { |
218 |
| - for (String s : extraArgs) { |
219 |
| - args.add(s); |
220 |
| - } |
221 |
| - } |
222 |
| - args.add(source); |
223 |
| - |
224 |
| - if (DEBUG) printIterable("compile args: ", args); |
225 |
| - |
226 |
| - ProcessBuilder pb = new ProcessBuilder(args); |
227 |
| - OutputAnalyzer output = new OutputAnalyzer(pb.start()); |
228 |
| - output.shouldHaveExitValue(0); |
229 |
| - } |
230 |
| - |
231 |
| - public static void compileModule(Path src, |
232 |
| - Path dest, |
233 |
| - String modulePathArg // arg to --module-path |
234 |
| - ) throws Exception { |
235 |
| - boolean compiled = false; |
236 |
| - if (modulePathArg == null) { |
237 |
| - compiled = CompilerUtils.compile(src, dest); |
238 |
| - } else { |
239 |
| - compiled = CompilerUtils.compile(src, dest, |
240 |
| - "--module-path", modulePathArg); |
241 |
| - } |
242 |
| - if (!compiled) { |
243 |
| - throw new RuntimeException("module did not compile"); |
244 |
| - } |
245 |
| - } |
246 |
| - |
247 |
| - static final String keyTool = JDKToolFinder.getJDKTool("keytool"); |
248 |
| - static final String jarSigner = JDKToolFinder.getJDKTool("jarsigner"); |
249 |
| - |
250 |
| - public static void signJarWithDisabledAlgorithm(String jarName) throws Exception { |
251 |
| - String keyName = "key_with_disabled_alg"; |
252 |
| - executeProcess(keyTool, |
253 |
| - "-genkey", "-keystore", "./keystore", "-alias", keyName, |
254 |
| - "-storepass", "abc123", "-keypass", "abc123", "-keyalg", "dsa", |
255 |
| - "-sigalg", "SHA1withDSA", "-keysize", "512", "-dname", "CN=jvmtest2") |
256 |
| - .shouldHaveExitValue(0); |
257 |
| - |
258 |
| - doSigning(jarName, keyName); |
259 |
| - } |
260 |
| - |
261 |
| - public static void signJar(String jarName) throws Exception { |
262 |
| - String keyName = "mykey"; |
263 |
| - executeProcess(keyTool, |
264 |
| - "-genkey", "-keystore", "./keystore", "-alias", keyName, |
265 |
| - "-storepass", "abc123", "-keypass", "abc123", "-keyalg", "dsa", |
266 |
| - "-dname", "CN=jvmtest") |
267 |
| - .shouldHaveExitValue(0); |
268 |
| - |
269 |
| - doSigning(jarName, keyName); |
270 |
| - } |
271 |
| - |
272 |
| - private static void doSigning(String jarName, String keyName) throws Exception { |
273 |
| - executeProcess(jarSigner, |
274 |
| - "-keystore", "./keystore", "-storepass", "abc123", "-keypass", |
275 |
| - "abc123", "-signedjar", getJarFilePath("signed_" + jarName), |
276 |
| - getJarFilePath(jarName), keyName) |
277 |
| - .shouldHaveExitValue(0); |
278 |
| - } |
279 |
| - |
280 |
| - private static OutputAnalyzer executeProcess(String... cmds) |
281 |
| - throws Exception { |
282 |
| - |
283 |
| - JarBuilder.printArray("executeProcess: ", cmds); |
284 |
| - return ProcessTools.executeProcess(new ProcessBuilder(cmds)); |
285 |
| - } |
286 |
| - |
287 |
| - // diagnostic |
288 |
| - public static void printIterable(String msg, Iterable<String> l) { |
289 |
| - StringBuilder sum = new StringBuilder(); |
290 |
| - for (String s : l) { |
291 |
| - sum.append(s).append(' '); |
292 |
| - } |
293 |
| - System.out.println(msg + sum.toString()); |
294 |
| - } |
295 |
| - |
296 |
| - public static void printArray(String msg, String[] l) { |
297 |
| - StringBuilder sum = new StringBuilder(); |
298 |
| - for (String s : l) { |
299 |
| - sum.append(s).append(' '); |
300 |
| - } |
301 |
| - System.out.println(msg + sum.toString()); |
| 43 | + CDSJarUtils.buildFromDirectory(jarPath, classesDir, |
| 44 | + JarOptions.of() |
| 45 | + .setMainClass(mainClass) |
| 46 | + .setManifest(manifest)); |
302 | 47 | }
|
303 | 48 | }
|
0 commit comments