1 package org.apache.maven.plugin.toolchain;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
40
41
42
43 public class ToolchainMojo
44 extends AbstractMojo
45 {
46
47
48
49
50
51 private ToolchainManagerPrivate toolchainManager;
52
53
54
55
56
57
58
59
60
61 private MavenSession session;
62
63
64
65
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
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
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
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
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 }