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 | 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 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
} |