View Javadoc
1   package org.apache.maven.toolchain.java;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  
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
37      extends DefaultToolchain
38      implements JavaToolchain
39  {
40      private String javaHome;
41  
42      public static final String KEY_JAVAHOME = "jdkHome"; //NOI18N
43  
44      JavaToolchainImpl( ToolchainModel model, Logger logger )
45      {
46          super( model, "jdk", logger );
47      }
48  
49      public String getJavaHome()
50      {
51          return javaHome;
52      }
53  
54      public void setJavaHome( String javaHome )
55      {
56          this.javaHome = javaHome;
57      }
58  
59      public String toString()
60      {
61          return "JDK[" + getJavaHome() + "]";
62      }
63  
64      public String findTool( String toolName )
65      {
66          File toRet = findTool( toolName, new File( FileUtils.normalize( getJavaHome() ) ) );
67          if ( toRet != null )
68          {
69              return toRet.getAbsolutePath();
70          }
71          return null;
72      }
73  
74      private static File findTool( String toolName, File installDir )
75      {
76          File bin = new File( installDir, "bin" ); // NOI18N
77          if ( bin.exists() )
78          {
79              boolean isWindows = Os.isFamily( "windows" ); // NOI18N
80              if ( isWindows )
81              {
82                  File tool = new File( bin, toolName + ".exe" );
83                  if ( tool.exists() )
84                  {
85                      return tool;
86                  }
87              }
88              File tool = new File( bin, toolName );
89              if ( tool.exists() )
90              {
91                  return tool;
92              }
93          }
94          return null;
95     }
96  }