3434import java .util .logging .Logger ;
3535import org .apache .maven .artifact .Artifact ;
3636import org .apache .maven .artifact .versioning .OverConstrainedVersionException ;
37+ import org .apache .maven .execution .MavenSession ;
3738import org .apache .maven .model .Dependency ;
3839import org .apache .maven .model .Plugin ;
3940import org .apache .maven .plugin .AbstractMojo ;
4243import org .apache .maven .plugins .annotations .Component ;
4344import org .apache .maven .plugins .annotations .Parameter ;
4445import org .apache .maven .project .MavenProject ;
46+ import org .apache .maven .toolchain .Toolchain ;
47+ import org .apache .maven .toolchain .ToolchainManager ;
4548import org .codehaus .plexus .util .Os ;
4649import org .codehaus .plexus .util .cli .CommandLineException ;
4750import org .codehaus .plexus .util .cli .CommandLineUtils ;
@@ -131,6 +134,15 @@ abstract class AbstractJaxwsMojo extends AbstractMojo {
131134 @ Parameter
132135 private File executable ;
133136
137+ @ Component
138+ private ToolchainManager toolchainManager ;
139+
140+ @ Parameter (defaultValue = "false" )
141+ private boolean useJdkToolchainExecutable ;
142+
143+ @ Parameter (defaultValue = "${session}" , readonly = true , required = true )
144+ protected MavenSession session ;
145+
134146 /**
135147 * The entry point to Aether, i.e. the component doing all the work.
136148 *
@@ -340,7 +352,7 @@ protected void exec(List<String> args) throws MojoExecutionException {
340352 throw new MojoExecutionException ("Cannot execute: " + executable .getAbsolutePath ());
341353 }
342354 } else {
343- cmd .setExecutable (new File (new File (System . getProperty ( "java.home" ), "bin" ), getJavaExec ()).getAbsolutePath ());
355+ cmd .setExecutable (new File (new File (getJavaHome ( ), "bin" ), getJavaExec ()).getAbsolutePath ());
344356 // add additional JVM options
345357 if (vmArgs != null ) {
346358 for (String arg : vmArgs ) {
@@ -441,10 +453,12 @@ private String[] getCP() throws DependencyResolutionException {
441453 throw new RuntimeException (ex );
442454 }
443455 sb .append (File .pathSeparator );
456+
457+ String javaHome = getJavaHome ();
444458 //don't forget tools.jar
445- File toolsJar = new File (System . getProperty ( "java.home" ) , "../lib/tools.jar" );
459+ File toolsJar = new File (javaHome , "../lib/tools.jar" );
446460 if (!toolsJar .exists ()) {
447- toolsJar = new File (System . getProperty ( "java.home" ) , "lib/tools.jar" );
461+ toolsJar = new File (javaHome , "lib/tools.jar" );
448462 }
449463 if (toolsJar .exists ()) {
450464 sb .append (toolsJar .getAbsolutePath ());
@@ -458,6 +472,27 @@ private String[] getCP() throws DependencyResolutionException {
458472 private String getJavaExec () {
459473 return isWindows () ? "java.exe" : "java" ;
460474 }
475+
476+ private String getJavaHome () {
477+ String javaHome = System .getProperty ("java.home" );
478+ if (getJdkToolchain () != null ) {
479+ File javaExecutable = new File (getJdkToolchain ().findTool ("java" ));
480+ javaHome = javaExecutable .getParentFile ().getParent ();
481+ getLog ().info ("got java home from maven toolchain: " + javaHome );
482+ }
483+ else {
484+ getLog ().info ("couldnt get a javahome from maven toolchain, defaulting to the java.home System property : " + javaHome );
485+ }
486+ return javaHome ;
487+ }
488+
489+ private Toolchain getJdkToolchain () {
490+ Toolchain tc = null ;
491+ if (this .toolchainManager != null ) {
492+ tc = this .toolchainManager .getToolchainFromBuildContext ("jdk" , this .session );
493+ }
494+ return tc ;
495+ }
461496
462497 private File createPathFile (String cp ) throws IOException {
463498 File f = File .createTempFile ("jax-ws-mvn-plugin-cp" , ".txt" );
0 commit comments