View Javadoc
1   package org.apache.maven.plugins.toolchain;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Check that toolchains requirements are met by currently configured toolchains and
40   * store the selected toolchains in build context for later retrieval by other plugins.
41   *
42   * @author mkleint
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       * The current build session instance. This is used for toolchain manager API calls.
57       */
58      @Parameter( defaultValue = "${session}", readonly = true, required = true )
59      private MavenSession session;
60  
61      /**
62       * Toolchains requirements, specified by one
63       * <pre>  &lt;toolchain-type&gt;
64       *    &lt;param&gt;expected value&lt;/param&gt;
65       *    ...
66       *  &lt;/toolchain-type&gt;</pre>
67       * element for each required toolchain.
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              // should not happen since parameter is required...
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              // TODO add the default toolchain instance if defined??
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                     // useful because of MNG-5716
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                     // store matching toolchain to build context
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 }