fix #420: fix detection of java version when JAVA_TOOL_OPTIONS is set · codehaus-plexus/plexus-compiler@739f5a8 (original) (raw)

File tree

Original file line number Diff line number Diff line change
@@ -6,3 +6,4 @@ bin
6 6 .idea
7 7 *.iml
8 8 .java-version
9 +*.bak
Original file line number Diff line number Diff line change
@@ -259,16 +259,17 @@ private String getOutOfProcessJavacVersion(String executable) throws CompilerExc
259 259 */
260 260 cli.addArguments(new String[] {"-version"}); //
261 261 CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
262 +CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
262 263 try {
263 -int exitCode = CommandLineUtils.executeCommandLine(cli, out, out);
264 +int exitCode = CommandLineUtils.executeCommandLine(cli, out, err);
264 265 if (exitCode != 0) {
265 266 throw new CompilerException("Could not retrieve version from " + executable + ". Exit code "
266 - + exitCode + ", Output: " + out.getOutput());
267 + + exitCode + ", Output: " + out.getOutput() + ", Error: " + err.getOutput());
267 268 }
268 269 } catch (CommandLineException e) {
269 270 throw new CompilerException("Error while executing the external compiler " + executable, e);
270 271 }
271 -version = extractMajorAndMinorVersion(out.getOutput());
272 +version = tryParseVersion(out.getOutput().trim());
272 273 VERSION_PER_EXECUTABLE.put(executable, version);
273 274 }
274 275 return version;
@@ -282,6 +283,19 @@ static String extractMajorAndMinorVersion(String text) {
282 283 return matcher.group();
283 284 }
284 285
286 +private String tryParseVersion(String version) {
287 +if (version.startsWith("javac ")) {
288 +version = version.substring(6);
289 +if (version.startsWith("1.")) {
290 +version = version.substring(0, 3);
291 + } else {
292 +version = version.substring(0, 2);
293 + }
294 +return version;
295 + }
296 +return null;
297 + }
298 +
285 299 @Override
286 300 public CompilerResult performCompile(CompilerConfiguration config) throws CompilerException {
287 301 File destinationDir = new File(config.getOutputLocation());
Original file line number Diff line number Diff line change
@@ -121,5 +121,6 @@ void testExtractMajorAndMinorVersion() {
121 121 assertEquals("11.0", JavacCompiler.extractMajorAndMinorVersion("javac 11.0.22"));
122 122 assertEquals("11.0", JavacCompiler.extractMajorAndMinorVersion("11.0.22"));
123 123 assertEquals("21", JavacCompiler.extractMajorAndMinorVersion("javac 21"));
124 +assertEquals("1.8", JavacCompiler.extractMajorAndMinorVersion("javac 1.8.0_432"));
124 125 }
125 126 }