blob: d1638067c1df8c049d7fac5437acbae31630ff82 [file] [log] [blame]
package org.apache.maven.plugins.toolchain;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.api.Session;
import org.apache.maven.api.Toolchain;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Component;
import org.apache.maven.api.plugin.annotations.LifecyclePhase;
import org.apache.maven.api.plugin.annotations.Mojo;
import org.apache.maven.api.plugin.annotations.Parameter;
import org.apache.maven.api.services.ToolchainManager;
import org.apache.maven.api.services.ToolchainManagerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Check that toolchains requirements are met by currently configured toolchains and
* store the selected toolchains in build context for later retrieval by other plugins.
*
* @author mkleint
*/
@Mojo( name = "toolchain", defaultPhase = LifecyclePhase.VALIDATE,
configurator = "toolchains-requirement-configurator" )
public class ToolchainMojo
implements org.apache.maven.api.plugin.Mojo
{
private Logger log = LoggerFactory.getLogger( getClass() );
/**
*/
@Component
private ToolchainManager toolchainManager;
/**
* The current build session instance. This is used for toolchain manager API calls.
*/
@Parameter( defaultValue = "${session}", readonly = true, required = true )
private Session session;
/**
* Toolchains requirements, specified by one
* <pre> &lt;toolchain-type&gt;
* &lt;param&gt;expected value&lt;/param&gt;
* ...
* &lt;/toolchain-type&gt;</pre>
* element for each required toolchain.
*/
@Parameter( required = true )
private ToolchainsRequirement toolchains;
@Override
public void execute()
throws MojoException
{
if ( toolchains == null )
{
// should not happen since parameter is required...
log.warn( "No toolchains requirements configured." );
return;
}
List<String> nonMatchedTypes = new ArrayList<>();
for ( Map.Entry<String, Map<String, String>> entry : toolchains.getToolchains().entrySet() )
{
String type = entry.getKey();
if ( !selectToolchain( type, entry.getValue() ) )
{
nonMatchedTypes.add( type );
}
}
if ( !nonMatchedTypes.isEmpty() )
{
// TODO add the default toolchain instance if defined??
StringBuilder buff = new StringBuilder();
buff.append( "Cannot find matching toolchain definitions for the following toolchain types:" );
for ( String type : nonMatchedTypes )
{
buff.append( System.lineSeparator() );
buff.append( getToolchainRequirementAsString( type, toolchains.getParams( type ) ) );
}
log.error( buff.toString() );
throw new MojoException( buff.toString() + System.lineSeparator()
+ "Please make sure you define the required toolchains in your ~/.m2/toolchains.xml file." );
}
}
protected String getToolchainRequirementAsString( String type, Map<String, String> params )
{
StringBuilder buff = new StringBuilder();
buff.append( type ).append( " [" );
if ( params.size() == 0 )
{
buff.append( " any" );
}
else
{
for ( Map.Entry<String, String> param : params.entrySet() )
{
buff.append( " " ).append( param.getKey() ).append( "='" ).append( param.getValue() );
buff.append( "'" );
}
}
buff.append( " ]" );
return buff.toString();
}
protected boolean selectToolchain( String type, Map<String, String> params )
throws MojoException
{
log.info( "Required toolchain: " + getToolchainRequirementAsString( type, params ) );
int typeFound = 0;
try
{
List<Toolchain> tcs = getToolchains( type );
for ( Toolchain tc : tcs )
{
if ( !type.equals( tc.getType() ) )
{
// useful because of MNG-5716
continue;
}
typeFound++;
if ( tc.matchesRequirements( params ) )
{
log.info( "Found matching toolchain for type " + type + ": " + tc );
// store matching toolchain to build context
toolchainManager.storeToolchainToBuildContext( session, tc );
return true;
}
}
}
catch ( ToolchainManagerException ex )
{
throw new MojoException( "Misconfigured toolchains.", ex );
}
log.error( "No toolchain " + ( ( typeFound == 0 ) ? "found" : ( "matched from " + typeFound + " found" ) )
+ " for type " + type );
return false;
}
private List<Toolchain> getToolchains( String type )
throws MojoException, ToolchainManagerException
{
return toolchainManager.getToolchainsForType( session, type );
}
}