View Javadoc
1   /*
2    * Copyright 2017-2022 original authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package io.micronaut.build;
17  
18  import org.apache.maven.execution.MavenSession;
19  import org.apache.maven.toolchain.Toolchain;
20  import org.apache.maven.toolchain.ToolchainManager;
21  import org.codehaus.plexus.util.Os;
22  
23  import java.io.File;
24  
25  /**
26   * Utility methods for different mojos.
27   */
28  public final class MojoUtils {
29  
30      private static final String JAVA = "java";
31  
32      private MojoUtils() {
33      }
34  
35      public static String findJavaExecutable(ToolchainManager toolchainManager, MavenSession mavenSession) {
36          String executable;
37          Toolchain toolchain = toolchainManager.getToolchainFromBuildContext("jdk", mavenSession);
38          if (toolchain != null) {
39              executable = toolchain.findTool(JAVA);
40          } else {
41              File javaBinariesDir = new File(new File(System.getProperty("java.home")), "bin");
42              if (Os.isFamily(Os.FAMILY_UNIX)) {
43                  executable = new File(javaBinariesDir, JAVA).getAbsolutePath();
44              } else if (Os.isFamily(Os.FAMILY_WINDOWS)) {
45                  executable = new File(javaBinariesDir, "java.exe").getAbsolutePath();
46              } else {
47                  executable = JAVA;
48              }
49          }
50          return executable;
51      }
52  }