diff --git a/.gitignore b/.gitignore index 50192f92..3671f034 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ bin .idea *.iml .java-version +*.bak diff --git a/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java b/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java index 3188c71c..073fafde 100644 --- a/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java +++ b/plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java @@ -259,16 +259,17 @@ private String getOutOfProcessJavacVersion(String executable) throws CompilerExc */ cli.addArguments(new String[] {"-version"}); // CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer(); + CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); try { - int exitCode = CommandLineUtils.executeCommandLine(cli, out, out); + int exitCode = CommandLineUtils.executeCommandLine(cli, out, err); if (exitCode != 0) { throw new CompilerException("Could not retrieve version from " + executable + ". Exit code " - + exitCode + ", Output: " + out.getOutput()); + + exitCode + ", Output: " + out.getOutput() + ", Error: " + err.getOutput()); } } catch (CommandLineException e) { throw new CompilerException("Error while executing the external compiler " + executable, e); } - version = extractMajorAndMinorVersion(out.getOutput()); + version = tryParseVersion(out.getOutput().trim()); VERSION_PER_EXECUTABLE.put(executable, version); } return version; @@ -282,6 +283,19 @@ static String extractMajorAndMinorVersion(String text) { return matcher.group(); } + private String tryParseVersion(String version) { + if (version.startsWith("javac ")) { + version = version.substring(6); + if (version.startsWith("1.")) { + version = version.substring(0, 3); + } else { + version = version.substring(0, 2); + } + return version; + } + return null; + } + @Override public CompilerResult performCompile(CompilerConfiguration config) throws CompilerException { File destinationDir = new File(config.getOutputLocation()); diff --git a/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavacCompilerTest.java b/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavacCompilerTest.java index ccd572d4..78393b6a 100644 --- a/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavacCompilerTest.java +++ b/plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavacCompilerTest.java @@ -121,5 +121,6 @@ void testExtractMajorAndMinorVersion() { assertEquals("11.0", JavacCompiler.extractMajorAndMinorVersion("javac 11.0.22")); assertEquals("11.0", JavacCompiler.extractMajorAndMinorVersion("11.0.22")); assertEquals("21", JavacCompiler.extractMajorAndMinorVersion("javac 21")); + assertEquals("1.8", JavacCompiler.extractMajorAndMinorVersion("javac 1.8.0_432")); } }