22 */ |
22 */ |
23 |
23 |
24 import java.io.*; |
24 import java.io.*; |
25 import java.net.URL; |
25 import java.net.URL; |
26 import java.net.URLClassLoader; |
26 import java.net.URLClassLoader; |
|
27 import java.nio.file.Files; |
|
28 import java.nio.file.Path; |
27 import java.util.*; |
29 import java.util.*; |
|
30 import java.util.Map.Entry; |
|
31 import java.util.jar.JarFile; |
|
32 import java.util.jar.JarOutputStream; |
|
33 import java.util.logging.Level; |
|
34 import java.util.logging.Logger; |
28 import java.util.regex.*; |
35 import java.util.regex.*; |
|
36 import java.util.stream.Collectors; |
|
37 import java.util.zip.ZipEntry; |
|
38 |
29 import javax.annotation.processing.Processor; |
39 import javax.annotation.processing.Processor; |
30 import javax.tools.Diagnostic; |
40 import javax.tools.Diagnostic; |
31 import javax.tools.DiagnosticCollector; |
41 import javax.tools.DiagnosticCollector; |
32 import javax.tools.JavaCompiler; |
42 import javax.tools.JavaCompiler; |
33 import javax.tools.JavaCompiler.CompilationTask; |
43 import javax.tools.JavaCompiler.CompilationTask; |
208 List<String> opts = new ArrayList<String>(); |
218 List<String> opts = new ArrayList<String>(); |
209 if (!modulePathFiles.isEmpty()) { |
219 if (!modulePathFiles.isEmpty()) { |
210 File modulepathDir = new File(tempDir, "modulepath"); |
220 File modulepathDir = new File(tempDir, "modulepath"); |
211 modulepathDir.mkdirs(); |
221 modulepathDir.mkdirs(); |
212 clean(modulepathDir); |
222 clean(modulepathDir); |
213 List<String> sOpts = Arrays.asList("-d", modulepathDir.getPath(), |
223 boolean hasModuleInfo = |
214 "--module-source-path", new File(file, "modulepath").getAbsolutePath()); |
224 modulePathFiles.stream() |
215 new Jsr199Compiler(verbose).run(null, null, false, sOpts, modulePathFiles); |
225 .anyMatch(f -> f.getName().equalsIgnoreCase("module-info.java")); |
|
226 Path modulePath = new File(file, "modulepath").toPath().toAbsolutePath(); |
|
227 if (hasModuleInfo) { |
|
228 //ordinary modules |
|
229 List<String> sOpts = |
|
230 Arrays.asList("-d", modulepathDir.getPath(), |
|
231 "--module-source-path", modulePath.toString()); |
|
232 new Jsr199Compiler(verbose).run(null, null, false, sOpts, modulePathFiles); |
|
233 } else { |
|
234 //automatic modules: |
|
235 Map<String, List<Path>> module2Files = |
|
236 modulePathFiles.stream() |
|
237 .map(f -> f.toPath()) |
|
238 .collect(Collectors.groupingBy(p -> modulePath.relativize(p) |
|
239 .getName(0) |
|
240 .toString())); |
|
241 for (Entry<String, List<Path>> e : module2Files.entrySet()) { |
|
242 File scratchDir = new File(tempDir, "scratch"); |
|
243 scratchDir.mkdirs(); |
|
244 clean(scratchDir); |
|
245 List<String> sOpts = |
|
246 Arrays.asList("-d", scratchDir.getPath()); |
|
247 new Jsr199Compiler(verbose).run(null, |
|
248 null, |
|
249 false, |
|
250 sOpts, |
|
251 e.getValue().stream() |
|
252 .map(p -> p.toFile()) |
|
253 .collect(Collectors.toList())); |
|
254 try (JarOutputStream jarOut = |
|
255 new JarOutputStream(new FileOutputStream(new File(modulepathDir, e.getKey() + ".jar")))) { |
|
256 Files.find(scratchDir.toPath(), Integer.MAX_VALUE, (p, attr) -> attr.isRegularFile()) |
|
257 .forEach(p -> { |
|
258 try (InputStream in = Files.newInputStream(p)) { |
|
259 jarOut.putNextEntry(new ZipEntry(scratchDir.toPath() |
|
260 .relativize(p) |
|
261 .toString())); |
|
262 jarOut.write(in.readAllBytes()); |
|
263 } catch (IOException ex) { |
|
264 throw new IllegalStateException(ex); |
|
265 } |
|
266 }); |
|
267 } |
|
268 } |
|
269 } |
216 opts.add("--module-path"); |
270 opts.add("--module-path"); |
217 opts.add(modulepathDir.getAbsolutePath()); |
271 opts.add(modulepathDir.getAbsolutePath()); |
218 } |
272 } |
219 |
273 |
220 if (!classPathFiles.isEmpty()) { |
274 if (!classPathFiles.isEmpty()) { |