1 package org.apache.maven.plugins.toolchain;
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.plugin.MojoFailureException;
26 import org.apache.maven.plugins.annotations.Component;
27 import org.apache.maven.plugins.annotations.LifecyclePhase;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.apache.maven.toolchain.MisconfiguredToolchainException;
31 import org.apache.maven.toolchain.ToolchainManagerPrivate;
32 import org.apache.maven.toolchain.ToolchainPrivate;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Map;
37
38
39
40
41
42
43
44 @Mojo( name = "toolchain", defaultPhase = LifecyclePhase.VALIDATE,
45 configurator = "toolchains-requirement-configurator",
46 threadSafe = true )
47 public class ToolchainMojo
48 extends AbstractMojo
49 {
50 private static final Object LOCK = new Object();
51
52
53
54 @Component
55 private ToolchainManagerPrivate toolchainManagerPrivate;
56
57
58
59
60 @Parameter( defaultValue = "${session}", readonly = true, required = true )
61 private MavenSession session;
62
63
64
65
66
67
68
69
70
71 @Parameter( required = true )
72 private ToolchainsRequirement toolchains;
73
74 @Override
75 public void execute()
76 throws MojoExecutionException, MojoFailureException
77 {
78 if ( toolchains == null )
79 {
80
81 getLog().warn( "No toolchains requirements configured." );
82 return;
83 }
84
85 List<String> nonMatchedTypes = new ArrayList<>();
86
87 for ( Map.Entry<String, Map<String, String>> entry : toolchains.getToolchains().entrySet() )
88 {
89 String type = entry.getKey();
90
91 if ( !selectToolchain( type, entry.getValue() ) )
92 {
93 nonMatchedTypes.add( type );
94 }
95 }
96
97 if ( !nonMatchedTypes.isEmpty() )
98 {
99
100 StringBuilder buff = new StringBuilder();
101 buff.append( "Cannot find matching toolchain definitions for the following toolchain types:" );
102
103 for ( String type : nonMatchedTypes )
104 {
105 buff.append( System.lineSeparator() );
106 buff.append( getToolchainRequirementAsString( type, toolchains.getParams( type ) ) );
107 }
108
109 getLog().error( buff.toString() );
110
111 throw new MojoFailureException( buff.toString() + System.lineSeparator()
112 + "Please make sure you define the required toolchains in your ~/.m2/toolchains.xml file." );
113 }
114 }
115
116 protected String getToolchainRequirementAsString( String type, Map<String, String> params )
117 {
118 StringBuilder buff = new StringBuilder();
119
120 buff.append( type ).append( " [" );
121
122 if ( params.size() == 0 )
123 {
124 buff.append( " any" );
125 }
126 else
127 {
128 for ( Map.Entry<String, String> param : params.entrySet() )
129 {
130 buff.append( " " ).append( param.getKey() ).append( "='" ).append( param.getValue() );
131 buff.append( "'" );
132 }
133 }
134
135 buff.append( " ]" );
136
137 return buff.toString();
138 }
139
140 protected boolean selectToolchain( String type, Map<String, String> params )
141 throws MojoExecutionException
142 {
143 getLog().info( "Required toolchain: " + getToolchainRequirementAsString( type, params ) );
144 int typeFound = 0;
145
146 try
147 {
148 ToolchainPrivate[] tcs = getToolchains( type );
149
150 for ( ToolchainPrivate tc : tcs )
151 {
152 if ( !type.equals( tc.getType() ) )
153 {
154
155 continue;
156 }
157
158 typeFound++;
159
160 if ( tc.matchesRequirements( params ) )
161 {
162 getLog().info( "Found matching toolchain for type " + type + ": " + tc );
163
164
165 synchronized ( LOCK )
166 {
167 toolchainManagerPrivate.storeToolchainToBuildContext( tc, session );
168 }
169
170 return true;
171 }
172 }
173 }
174 catch ( MisconfiguredToolchainException ex )
175 {
176 throw new MojoExecutionException( "Misconfigured toolchains.", ex );
177 }
178
179 getLog().error( "No toolchain " + ( ( typeFound == 0 ) ? "found" : ( "matched from " + typeFound + " found" ) )
180 + " for type " + type );
181
182 return false;
183 }
184
185 private ToolchainPrivate[] getToolchains( String type )
186 throws MojoExecutionException, MisconfiguredToolchainException
187 {
188 return toolchainManagerPrivate.getToolchainsForType( type, session );
189 }
190
191 }