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