Skip to content

Commit 09e4ea5

Browse files
baratynskiymglukhikh
authored andcommitted
kotlin-maven-plugin: add tools.jar dependency if -Xuse-javac is specified
1 parent 5eea3b6 commit 09e4ea5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package org.jetbrains.kotlin.maven;
1818

1919
import org.apache.maven.plugin.MojoExecutionException;
20+
import org.apache.maven.plugin.MojoFailureException;
2021
import org.apache.maven.plugins.annotations.*;
2122
import org.apache.maven.project.MavenProject;
2223
import org.jetbrains.annotations.NotNull;
24+
import org.jetbrains.annotations.Nullable;
2325
import org.jetbrains.kotlin.cli.common.CLICompiler;
2426
import org.jetbrains.kotlin.cli.common.ExitCode;
2527
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
@@ -30,6 +32,8 @@
3032
import org.jetbrains.kotlin.maven.kapt.AnnotationProcessingManager;
3133

3234
import java.io.File;
35+
import java.io.IOException;
36+
import java.net.URL;
3337
import java.util.*;
3438

3539
import static com.intellij.openapi.util.text.StringUtil.join;
@@ -170,6 +174,20 @@ protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArgument
170174
}
171175
}
172176

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+
173191
@Override
174192
protected ExitCode execCompiler(
175193
CLICompiler<K2JVMCompilerArguments> compiler,
@@ -215,4 +233,28 @@ private ExitCode runIncrementalCompiler(
215233
return ExitCode.OK;
216234
}
217235
}
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+
218260
}

0 commit comments

Comments
 (0)