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   * @deprecated Use {@link org.apache.maven.api.services.ToolchainFactory} instead.
35   */
36  @Deprecated(since = "4.0.0")
37  public class JavaToolchainImpl extends DefaultToolchain implements JavaToolchain {
38      private String javaHome;
39  
40      public static final String KEY_JAVAHOME = "jdkHome"; // NOI18N
41  
42      JavaToolchainImpl(ToolchainModel model, Logger logger) {
43          super(model, "jdk", logger);
44      }
45  
46      public String getJavaHome() {
47          return javaHome;
48      }
49  
50      public void setJavaHome(String javaHome) {
51          this.javaHome = javaHome;
52      }
53  
54      public String toString() {
55          return "JDK[" + getJavaHome() + "]";
56      }
57  
58      public String findTool(String toolName) {
59          Path toRet = findTool(toolName, Paths.get(getJavaHome()).normalize());
60          if (toRet != null) {
61              return toRet.toAbsolutePath().toString();
62          }
63          return null;
64      }
65  
66      private static Path findTool(String toolName, Path installDir) {
67          Path bin = installDir.resolve("bin"); // NOI18N
68          if (Files.isDirectory(bin)) {
69              if (Os.IS_WINDOWS) {
70                  Path tool = bin.resolve(toolName + ".exe");
71                  if (Files.exists(tool)) {
72                      return tool;
73                  }
74              }
75              Path tool = bin.resolve(toolName);
76              if (Files.exists(tool)) {
77                  return tool;
78              }
79          }
80          return null;
81      }
82  }