1 /* |
1 /* |
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
45 /** |
45 /** |
46 * Compile all the java sources in {@code <source>/**} to |
46 * Compile all the java sources in {@code <source>/**} to |
47 * {@code <destination>/**}. The destination directory will be created if |
47 * {@code <destination>/**}. The destination directory will be created if |
48 * it doesn't exist. |
48 * it doesn't exist. |
49 * |
49 * |
|
50 * Equivalent to calling {@code compile(source, destination, true, options);}. |
|
51 * |
50 * All warnings/errors emitted by the compiler are output to System.out/err. |
52 * All warnings/errors emitted by the compiler are output to System.out/err. |
|
53 * |
|
54 * @param source Path to the source directory |
|
55 * @param destination Path to the destination directory |
|
56 * @param options Any options to pass to the compiler |
51 * |
57 * |
52 * @return true if the compilation is successful |
58 * @return true if the compilation is successful |
53 * |
59 * |
54 * @throws IOException |
60 * @throws IOException |
55 * if there is an I/O error scanning the source tree or |
61 * if there is an I/O error scanning the source tree or |
58 * if there is no system java compiler |
64 * if there is no system java compiler |
59 */ |
65 */ |
60 public static boolean compile(Path source, Path destination, String... options) |
66 public static boolean compile(Path source, Path destination, String... options) |
61 throws IOException |
67 throws IOException |
62 { |
68 { |
|
69 return compile(source, destination, true, options); |
|
70 } |
|
71 |
|
72 /** |
|
73 * Compile all the java sources in {@code <source>} and optionally its |
|
74 * subdirectories, to |
|
75 * {@code <destination>}. The destination directory will be created if |
|
76 * it doesn't exist. |
|
77 * |
|
78 * All warnings/errors emitted by the compiler are output to System.out/err. |
|
79 * |
|
80 * @param source Path to the source directory |
|
81 * @param destination Path to the destination directory |
|
82 * @param recurse If {@code true} recurse into any {@code source} subdirectories |
|
83 * to compile all java source files; else only compile those directly in |
|
84 * {@code source}. |
|
85 * @param options Any options to pass to the compiler |
|
86 * |
|
87 * @return true if the compilation is successful |
|
88 * |
|
89 * @throws IOException |
|
90 * if there is an I/O error scanning the source tree or |
|
91 * creating the destination directory |
|
92 * @throws UnsupportedOperationException |
|
93 * if there is no system java compiler |
|
94 */ |
|
95 |
|
96 public static boolean compile(Path source, Path destination, boolean recurse, String... options) |
|
97 throws IOException |
|
98 { |
63 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
99 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
64 if (compiler == null) { |
100 if (compiler == null) { |
65 // no compiler available |
101 // no compiler available |
66 throw new UnsupportedOperationException("Unable to get system java compiler. " |
102 throw new UnsupportedOperationException("Unable to get system java compiler. " |
67 + "Perhaps, jdk.compiler module is not available."); |
103 + "Perhaps, jdk.compiler module is not available."); |
68 } |
104 } |
69 StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null); |
105 StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null); |
70 |
106 |
71 List<Path> sources |
107 List<Path> sources |
72 = Files.find(source, Integer.MAX_VALUE, |
108 = Files.find(source, (recurse ? Integer.MAX_VALUE : 1), |
73 (file, attrs) -> (file.toString().endsWith(".java"))) |
109 (file, attrs) -> (file.toString().endsWith(".java"))) |
74 .collect(Collectors.toList()); |
110 .collect(Collectors.toList()); |
75 |
111 |
76 Files.createDirectories(destination); |
112 Files.createDirectories(destination); |
77 jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList()); |
113 jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList()); |