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         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       * The current build session instance. This is used for toolchain manager API calls.
59       */
60      @Parameter( defaultValue = "${session}", readonly = true, required = true )
61      private MavenSession session;
62  
63      /**
64       * Toolchains requirements, specified by one
65       * <pre>  &lt;toolchain-type&gt;
66       *    &lt;param&gt;expected value&lt;/param&gt;
67       *    ...
68       *  &lt;/toolchain-type&gt;</pre>
69       * element for each required toolchain.
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              // should not happen since parameter is required...
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              // TODO add the default toolchain instance if defined??
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                     // useful because of MNG-5716
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                     // store matching toolchain to build context
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 }