1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.util.Arrays;
29 import java.util.Iterator;
30 import java.util.Properties;
31
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.toolchain.MisconfiguredToolchainException;
36 import org.apache.maven.toolchain.ToolchainManagerPrivate;
37 import org.apache.maven.toolchain.ToolchainPrivate;
38
39
40
41
42
43 public class CoreItMojo
44 extends AbstractMojo
45 {
46
47
48
49
50 private ToolchainManagerPrivate toolchainManager;
51
52
53
54
55
56
57
58
59 private MavenSession session;
60
61
62
63
64
65
66 private File outputFile;
67
68
69
70
71
72
73 private String type;
74
75
76
77
78
79
80 private String tool;
81
82
83
84
85
86
87 private int selected;
88
89 public void execute()
90 throws MojoExecutionException
91 {
92 ToolchainPrivate[] tcs = getToolchains();
93
94 getLog().info( "[MAVEN-CORE-IT-LOG] Toolchains in plugin: " + Arrays.asList( tcs ) );
95
96 if ( selected >= 0 )
97 {
98 if ( selected < tcs.length )
99 {
100 ToolchainPrivate toolchain = tcs[selected];
101 toolchainManager.storeToolchainToBuildContext( toolchain, session );
102 }
103 else
104 {
105 getLog().warn( "[MAVEN-CORE-IT-LOG] Toolchain #" + selected + " can't be selected, found only " + tcs.length );
106 }
107 }
108
109 Properties properties = new Properties();
110
111 int count = 1;
112 for ( Iterator i = Arrays.asList( tcs ).iterator(); i.hasNext(); count++ )
113 {
114 ToolchainPrivate toolchain = (ToolchainPrivate) i.next();
115
116 String foundTool = toolchain.findTool( tool );
117 if ( foundTool != null )
118 {
119 properties.setProperty( "tool." + count, foundTool );
120 }
121 }
122
123 OutputStream out = null;
124 try
125 {
126 outputFile.getParentFile().mkdirs();
127 out = new FileOutputStream( outputFile );
128 properties.store( out, "MAVEN-CORE-IT-LOG" );
129 }
130 catch ( IOException e )
131 {
132 throw new MojoExecutionException( e.getMessage(), e );
133 }
134 finally
135 {
136 if ( out != null )
137 {
138 try
139 {
140 out.close();
141 }
142 catch ( IOException e )
143 {
144
145 }
146 }
147 }
148 }
149
150 private ToolchainPrivate[] getToolchains()
151 throws MojoExecutionException
152 {
153 Class managerClass = toolchainManager.getClass();
154
155 try
156 {
157 try
158 {
159
160 Method oldMethod = managerClass.getMethod( "getToolchainsForType", new Class[] { String.class } );
161
162 return (ToolchainPrivate[]) oldMethod.invoke( toolchainManager, new Object[] { type } );
163 }
164 catch ( NoSuchMethodException e )
165 {
166
167 Method newMethod =
168 managerClass.getMethod( "getToolchainsForType", new Class[] { String.class, MavenSession.class } );
169
170 return (ToolchainPrivate[]) newMethod.invoke( toolchainManager, new Object[] { type, session } );
171 }
172 }
173 catch ( NoSuchMethodException e )
174 {
175 throw new MojoExecutionException( "Incompatible toolchain API", e );
176 }
177 catch ( IllegalAccessException e )
178 {
179 throw new MojoExecutionException( "Incompatible toolchain API", e );
180 }
181 catch ( InvocationTargetException e )
182 {
183 throw new MojoExecutionException( "Incompatible toolchain API", e );
184 }
185 }
186
187 }