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 org.apache.maven.execution.MavenSession;
23 import org.apache.maven.plugin.AbstractMojo;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.toolchain.ToolchainManagerPrivate;
26 import org.apache.maven.toolchain.ToolchainPrivate;
27
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.util.Arrays;
35 import java.util.Iterator;
36 import java.util.Properties;
37
38
39
40
41
42 public class CoreItMojo
43 extends AbstractMojo
44 {
45
46
47
48
49 private ToolchainManagerPrivate toolchainManager;
50
51
52
53
54
55
56
57
58 private MavenSession session;
59
60
61
62
63
64
65 private File outputFile;
66
67
68
69
70
71
72 private String type;
73
74
75
76
77
78
79 private String tool;
80
81
82
83
84
85
86 private int selected;
87
88 public void execute()
89 throws MojoExecutionException
90 {
91 ToolchainPrivate[] tcs = getToolchains();
92
93 getLog().info( "[MAVEN-CORE-IT-LOG] Toolchains in plugin: " + Arrays.asList( tcs ) );
94
95 if ( selected >= 0 )
96 {
97 if ( selected < tcs.length )
98 {
99 ToolchainPrivate toolchain = tcs[selected];
100 toolchainManager.storeToolchainToBuildContext( toolchain, session );
101 }
102 else
103 {
104 getLog().warn(
105 "[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<ToolchainPrivate> i = Arrays.<ToolchainPrivate>asList( tcs ).iterator(); i.hasNext(); count++ )
113 {
114 ToolchainPrivate toolchain = 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<? extends ToolchainManagerPrivate> 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 | InvocationTargetException | IllegalAccessException e )
174 {
175 throw new MojoExecutionException( "Incompatible toolchain API", e );
176 }
177 }
178
179 }