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.artifact.versioning.ArtifactVersion;
24  import org.apache.maven.toolchain.DefaultToolchain;
25  import org.apache.maven.toolchain.model.ToolchainModel;
26  import org.codehaus.plexus.logging.Logger;
27  import org.codehaus.plexus.util.FileUtils;
28  import org.codehaus.plexus.util.Os;
29  
30  /**
31   * JDK toolchain implementation.
32   *
33   * @author Milos Kleint
34   * @since 2.0.9, renamed from DefaultJavaToolChain in 3.2.4
35   */
36  public class JavaToolchainImpl extends DefaultToolchain implements JavaToolchain {
37      private String javaHome;
38  
39      private ArtifactVersion javaVersion;
40  
41      public static final String KEY_JAVAHOME = "jdkHome"; // NOI18N
42  
43      JavaToolchainImpl(ToolchainModel model, Logger logger) {
44          super(model, "jdk", logger);
45      }
46  
47      public String getJavaHome() {
48          return javaHome;
49      }
50  
51      public void setJavaHome(String javaHome) {
52          this.javaHome = javaHome;
53      }
54  
55      @Override
56      public ArtifactVersion getJavaVersion() {
57          return javaVersion;
58      }
59  
60      public void setJavaVersion(ArtifactVersion javaVersion) {
61          this.javaVersion = javaVersion;
62      }
63  
64      public String toString() {
65          return "JDK[" + getJavaHome() + "]";
66      }
67  
68      public String findTool(String toolName) {
69          File toRet = findTool(toolName, new File(FileUtils.normalize(getJavaHome())));
70          if (toRet != null) {
71              return toRet.getAbsolutePath();
72          }
73          return null;
74      }
75  
76      private static File findTool(String toolName, File installDir) {
77          File bin = new File(installDir, "bin"); // NOI18N
78          if (bin.exists()) {
79              boolean isWindows = Os.isFamily("windows"); // NOI18N
80              if (isWindows) {
81                  File tool = new File(bin, toolName + ".exe");
82                  if (tool.exists()) {
83                      return tool;
84                  }
85              }
86              File tool = new File(bin, toolName);
87              if (tool.exists()) {
88                  return tool;
89              }
90          }
91          return null;
92      }
93  }