|
17 | 17 | package org.jetbrains.kotlin.maven;
|
18 | 18 |
|
19 | 19 | import org.apache.maven.plugin.MojoExecutionException;
|
| 20 | +import org.apache.maven.plugin.MojoFailureException; |
20 | 21 | import org.apache.maven.plugins.annotations.*;
|
21 | 22 | import org.apache.maven.project.MavenProject;
|
22 | 23 | import org.jetbrains.annotations.NotNull;
|
| 24 | +import org.jetbrains.annotations.Nullable; |
23 | 25 | import org.jetbrains.kotlin.cli.common.CLICompiler;
|
24 | 26 | import org.jetbrains.kotlin.cli.common.ExitCode;
|
25 | 27 | import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
|
|
30 | 32 | import org.jetbrains.kotlin.maven.kapt.AnnotationProcessingManager;
|
31 | 33 |
|
32 | 34 | import java.io.File;
|
| 35 | +import java.io.IOException; |
| 36 | +import java.net.URL; |
33 | 37 | import java.util.*;
|
34 | 38 |
|
35 | 39 | import static com.intellij.openapi.util.text.StringUtil.join;
|
@@ -170,6 +174,20 @@ protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArgument
|
170 | 174 | }
|
171 | 175 | }
|
172 | 176 |
|
| 177 | + @Override |
| 178 | + public void execute() throws MojoExecutionException, MojoFailureException { |
| 179 | + if (args != null && args.contains("-Xuse-javac")) { |
| 180 | + try { |
| 181 | + URL toolsJar = getJdkToolsJarURL(); |
| 182 | + if (toolsJar != null) { |
| 183 | + project.getClassRealm().addURL(toolsJar); |
| 184 | + } |
| 185 | + } catch (IOException ex) {} |
| 186 | + } |
| 187 | + |
| 188 | + super.execute(); |
| 189 | + } |
| 190 | + |
173 | 191 | @Override
|
174 | 192 | protected ExitCode execCompiler(
|
175 | 193 | CLICompiler<K2JVMCompilerArguments> compiler,
|
@@ -215,4 +233,28 @@ private ExitCode runIncrementalCompiler(
|
215 | 233 | return ExitCode.OK;
|
216 | 234 | }
|
217 | 235 | }
|
| 236 | + |
| 237 | + @Nullable |
| 238 | + private URL getJdkToolsJarURL() throws IOException { |
| 239 | + String javaHomePath = System.getProperty("java.home"); |
| 240 | + if (javaHomePath == null || javaHomePath.isEmpty()) { |
| 241 | + return null; |
| 242 | + } |
| 243 | + File javaHome = new File(javaHomePath); |
| 244 | + File toolsJar = new File(javaHome, "lib/tools.jar"); |
| 245 | + if (toolsJar.exists()) { |
| 246 | + return toolsJar.getCanonicalFile().toURI().toURL(); |
| 247 | + } |
| 248 | + |
| 249 | + // We might be inside jre. |
| 250 | + if (javaHome.getName().equals("jre")) { |
| 251 | + toolsJar = new File(javaHome.getParent(), "lib/tools.jar"); |
| 252 | + if (toolsJar.exists()) { |
| 253 | + return toolsJar.getCanonicalFile().toURI().toURL(); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + return null; |
| 258 | + } |
| 259 | + |
218 | 260 | }
|
0 commit comments