Coverage Report - org.apache.maven.plugin.toolchain.ToolchainMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ToolchainMojo
0%
0/61
0%
0/28
9.333
 
 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  0
     {
 71  0
     }
 72  
 
 73  
     public void execute()
 74  
         throws MojoExecutionException, MojoFailureException
 75  
     {
 76  0
         if ( toolchains != null )
 77  
         {
 78  0
             Iterator en = toolchains.getToolchainsTypes().iterator();
 79  0
             List nonMatchedTypes = new ArrayList();
 80  0
             while ( en.hasNext() )
 81  
             {
 82  
                 try
 83  
                 {
 84  0
                     String type = (String) en.next();
 85  0
                     getLog().info( "Type:" + type );
 86  0
                     Map params = toolchains.getParams( type );
 87  0
                     ToolchainPrivate[] tcs = getToolchains( type );
 88  0
                     boolean matched = false;
 89  0
                     for ( int i = 0; i < tcs.length; i++ )
 90  
                     {
 91  0
                         if ( tcs[i].matchesRequirements( params ) )
 92  
                         {
 93  0
                             getLog().info( "Toolchain (" + type + ") matched:" + tcs[i] );
 94  0
                             toolchainManager.storeToolchainToBuildContext( tcs[i], session );
 95  0
                             matched = true;
 96  0
                             break;
 97  
                         }
 98  
                     }
 99  0
                     if ( !matched )
 100  
                     {
 101  0
                         nonMatchedTypes.add( type );
 102  
                     }
 103  
                 }
 104  0
                 catch ( MisconfiguredToolchainException ex )
 105  
                 {
 106  0
                     throw new MojoExecutionException( "Misconfigured toolchains.", ex );
 107  0
                 }
 108  
             }
 109  0
             if ( !nonMatchedTypes.isEmpty() )
 110  
             {
 111  
                 //TODO add the default toolchain instance if defined??
 112  0
                 StringBuffer buff = new StringBuffer();
 113  0
                 buff.append( "Cannot find matching toolchain definitions for the following toolchain types:" );
 114  0
                 Iterator it = nonMatchedTypes.iterator();
 115  0
                 while ( it.hasNext() )
 116  
                 {
 117  0
                     String type = (String) it.next();
 118  0
                     buff.append( '\n' );
 119  0
                     buff.append( type );
 120  0
                     Map params = toolchains.getParams( type );
 121  0
                     if ( params.size() > 0 )
 122  
                     {
 123  0
                         Iterator it2 = params.keySet().iterator();
 124  0
                         buff.append( " [" );
 125  0
                         while ( it2.hasNext() )
 126  
                         {
 127  0
                             String string = (String) it2.next();
 128  0
                             buff.append( " " + string + "='" + params.get( string ) + "' " );
 129  0
                         }
 130  0
                         buff.append( ']' );
 131  
                     }
 132  0
                 }
 133  0
                 getLog().error( buff.toString() );
 134  0
                 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  0
     }
 143  
 
 144  
     private ToolchainPrivate[] getToolchains( String type )
 145  
         throws MojoExecutionException, MisconfiguredToolchainException
 146  
     {
 147  0
         Class managerClass = toolchainManager.getClass();
 148  
 
 149  
         try
 150  
         {
 151  
             try
 152  
             {
 153  
                 // try 3.x style API
 154  0
                 Method newMethod =
 155  0
                     managerClass.getMethod( "getToolchainsForType", new Class[] { String.class, MavenSession.class } );
 156  
 
 157  0
                 return (ToolchainPrivate[]) newMethod.invoke( toolchainManager, new Object[] { type, session } );
 158  
             }
 159  0
             catch ( NoSuchMethodException e )
 160  
             {
 161  
                 // try 2.x style API
 162  0
                 Method oldMethod = managerClass.getMethod( "getToolchainsForType", new Class[] { String.class } );
 163  
 
 164  0
                 return (ToolchainPrivate[]) oldMethod.invoke( toolchainManager, new Object[] { type } );
 165  
             }
 166  
         }
 167  0
         catch ( NoSuchMethodException e )
 168  
         {
 169  0
             throw new MojoExecutionException( "Incompatible toolchain API", e );
 170  
         }
 171  0
         catch ( IllegalAccessException e )
 172  
         {
 173  0
             throw new MojoExecutionException( "Incompatible toolchain API", e );
 174  
         }
 175  0
         catch ( InvocationTargetException e )
 176  
         {
 177  0
             Throwable cause = e.getCause();
 178  
 
 179  0
             if ( cause instanceof RuntimeException )
 180  
             {
 181  0
                 throw (RuntimeException) cause;
 182  
             }
 183  0
             if ( cause instanceof MisconfiguredToolchainException )
 184  
             {
 185  0
                 throw (MisconfiguredToolchainException) cause;
 186  
             }
 187  
 
 188  0
             throw new MojoExecutionException( "Incompatible toolchain API", e );
 189  
         }
 190  
     }
 191  
 
 192  
 }