View Javadoc

1   package org.apache.maven.plugin.toolchain;
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.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.AbstractMojo;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  import org.apache.maven.toolchain.MisconfiguredToolchainException;
33  import org.apache.maven.toolchain.ToolchainManagerPrivate;
34  import org.apache.maven.toolchain.ToolchainPrivate;
35  
36  /**
37   * @goal toolchain
38   * @phase validate
39   * @configurator override
40   *
41   * @author mkleint
42   */
43  public class ToolchainMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       *
49       * @component
50       */
51      private ToolchainManagerPrivate toolchainManager;
52  
53      /**
54       * The current build session instance. This is used for
55       * toolchain manager API calls.
56       *
57       * @parameter expression="${session}"
58       * @required
59       * @readonly
60       */
61      private MavenSession session;
62  
63      /**
64       * @parameter
65       * @required
66       */
67      private Toolchains toolchains;
68  
69      public ToolchainMojo()
70      {
71      }
72  
73      public void execute()
74          throws MojoExecutionException, MojoFailureException
75      {
76          if ( toolchains != null )
77          {
78              Iterator en = toolchains.getToolchainsTypes().iterator();
79              List nonMatchedTypes = new ArrayList();
80              while ( en.hasNext() )
81              {
82                  try
83                  {
84                      String type = (String) en.next();
85                      getLog().info( "Type:" + type );
86                      Map params = toolchains.getParams( type );
87                      ToolchainPrivate[] tcs = getToolchains( type );
88                      boolean matched = false;
89                      for ( int i = 0; i < tcs.length; i++ )
90                      {
91                          if ( tcs[i].matchesRequirements( params ) )
92                          {
93                              getLog().info( "Toolchain (" + type + ") matched:" + tcs[i] );
94                              toolchainManager.storeToolchainToBuildContext( tcs[i], session );
95                              matched = true;
96                              break;
97                          }
98                      }
99                      if ( !matched )
100                     {
101                         nonMatchedTypes.add( type );
102                     }
103                 }
104                 catch ( MisconfiguredToolchainException ex )
105                 {
106                     throw new MojoExecutionException( "Misconfigured toolchains.", ex );
107                 }
108             }
109             if ( !nonMatchedTypes.isEmpty() )
110             {
111                 //TODO add the default toolchain instance if defined??
112                 StringBuffer buff = new StringBuffer();
113                 buff.append( "Cannot find matching toolchain definitions for the following toolchain types:" );
114                 Iterator it = nonMatchedTypes.iterator();
115                 while ( it.hasNext() )
116                 {
117                     String type = (String) it.next();
118                     buff.append( '\n' );
119                     buff.append( type );
120                     Map params = toolchains.getParams( type );
121                     if ( params.size() > 0 )
122                     {
123                         Iterator it2 = params.keySet().iterator();
124                         buff.append( " [" );
125                         while ( it2.hasNext() )
126                         {
127                             String string = (String) it2.next();
128                             buff.append( " " + string + "='" + params.get( string ) + "' " );
129                         }
130                         buff.append( ']' );
131                     }
132                 }
133                 getLog().error( buff.toString() );
134                 throw new MojoFailureException( buff.toString()
135                     + "\nPlease make sure you define the required toolchains in your ~/.m2/toolchains.xml file." );
136             }
137         }
138         else
139         {
140             //can that happen?
141         }
142     }
143 
144     private ToolchainPrivate[] getToolchains( String type )
145         throws MojoExecutionException, MisconfiguredToolchainException
146     {
147         Class managerClass = toolchainManager.getClass();
148 
149         try
150         {
151             try
152             {
153                 // try 3.x style API
154                 Method newMethod =
155                     managerClass.getMethod( "getToolchainsForType", new Class[] { String.class, MavenSession.class } );
156 
157                 return (ToolchainPrivate[]) newMethod.invoke( toolchainManager, new Object[] { type, session } );
158             }
159             catch ( NoSuchMethodException e )
160             {
161                 // try 2.x style API
162                 Method oldMethod = managerClass.getMethod( "getToolchainsForType", new Class[] { String.class } );
163 
164                 return (ToolchainPrivate[]) oldMethod.invoke( toolchainManager, new Object[] { type } );
165             }
166         }
167         catch ( NoSuchMethodException e )
168         {
169             throw new MojoExecutionException( "Incompatible toolchain API", e );
170         }
171         catch ( IllegalAccessException e )
172         {
173             throw new MojoExecutionException( "Incompatible toolchain API", e );
174         }
175         catch ( InvocationTargetException e )
176         {
177             Throwable cause = e.getCause();
178 
179             if ( cause instanceof RuntimeException )
180             {
181                 throw (RuntimeException) cause;
182             }
183             if ( cause instanceof MisconfiguredToolchainException )
184             {
185                 throw (MisconfiguredToolchainException) cause;
186             }
187 
188             throw new MojoExecutionException( "Incompatible toolchain API", e );
189         }
190     }
191 
192 }