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.io.File;
22  
23  import org.apache.maven.toolchain.DefaultToolchain;
24  import org.apache.maven.toolchain.model.ToolchainModel;
25  import org.codehaus.plexus.util.FileUtils;
26  import org.codehaus.plexus.util.Os;
27  import org.slf4j.Logger;
28  
29  /**
30   * JDK toolchain implementation.
31   *
32   * @author Milos Kleint
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          File toRet = findTool(toolName, new File(FileUtils.normalize(getJavaHome())));
58          if (toRet != null) {
59              return toRet.getAbsolutePath();
60          }
61          return null;
62      }
63  
64      private static File findTool(String toolName, File installDir) {
65          File bin = new File(installDir, "bin"); // NOI18N
66          if (bin.exists()) {
67              boolean isWindows = Os.isFamily("windows"); // NOI18N
68              if (isWindows) {
69                  File tool = new File(bin, toolName + ".exe");
70                  if (tool.exists()) {
71                      return tool;
72                  }
73              }
74              File tool = new File(bin, toolName);
75              if (tool.exists()) {
76                  return tool;
77              }
78          }
79          return null;
80      }
81  }