View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.toolchain.java;
20  
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  
25  import org.apache.maven.toolchain.DefaultToolchain;
26  import org.apache.maven.toolchain.model.ToolchainModel;
27  import org.apache.maven.utils.Os;
28  import org.slf4j.Logger;
29  
30  /**
31   * JDK toolchain implementation.
32   *
33   * @since 2.0.9, renamed from DefaultJavaToolChain in 3.2.4
34   */
35  public class JavaToolchainImpl extends DefaultToolchain implements JavaToolchain {
36      private String javaHome;
37  
38      public static final String KEY_JAVAHOME = "jdkHome"; // NOI18N
39  
40      JavaToolchainImpl(ToolchainModel model, Logger logger) {
41          super(model, "jdk", logger);
42      }
43  
44      public String getJavaHome() {
45          return javaHome;
46      }
47  
48      public void setJavaHome(String javaHome) {
49          this.javaHome = javaHome;
50      }
51  
52      public String toString() {
53          return "JDK[" + getJavaHome() + "]";
54      }
55  
56      public String findTool(String toolName) {
57          Path toRet = findTool(toolName, Paths.get(getJavaHome()).normalize());
58          if (toRet != null) {
59              return toRet.toAbsolutePath().toString();
60          }
61          return null;
62      }
63  
64      private static Path findTool(String toolName, Path installDir) {
65          Path bin = installDir.resolve("bin"); // NOI18N
66          if (Files.isDirectory(bin)) {
67              if (Os.IS_WINDOWS) {
68                  Path tool = bin.resolve(toolName + ".exe");
69                  if (Files.exists(tool)) {
70                      return tool;
71                  }
72              }
73              Path tool = bin.resolve(toolName);
74              if (Files.exists(tool)) {
75                  return tool;
76              }
77          }
78          return null;
79      }
80  }