Author: hboutemy Date: Wed Nov 5 01:06:08 2014 New Revision: 1636790 URL: http://svn.apache.org/r1636790 Log: [MTOOLCHAINS-6] updated sample custom toolchain to be compatible with Maven 2.0.9-3.2.3 and Maven 3.2.4+: see MNG-5718 and MNG-5720
Added: maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/CustomToolchainFactory.java - copied, changed from r1636475, maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/DefaultCustomToolchainFactory.java Removed: maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/DefaultCustomToolchainFactory.java Copied: maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/CustomToolchainFactory.java (from r1636475, maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/DefaultCustomToolchainFactory.java) URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/CustomToolchainFactory.java?p2=maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/CustomToolchainFactory.java&p1=maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/DefaultCustomToolchainFactory.java&r1=1636475&r2=1636790&rev=1636790&view=diff ============================================================================== --- maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/DefaultCustomToolchainFactory.java (original) +++ maven/plugins/trunk/maven-toolchains-plugin/src/it/custom-toolchain/src/main/java/org/apache/maven/plugins/toolchains/its/custom/CustomToolchainFactory.java Wed Nov 5 01:06:08 2014 @@ -19,8 +19,16 @@ package org.apache.maven.plugins.toolcha * under the License. */ +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.Properties; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.toolchain.MisconfiguredToolchainException; import org.apache.maven.toolchain.RequirementMatcherFactory; import org.apache.maven.toolchain.ToolchainFactory; @@ -33,21 +41,19 @@ import org.codehaus.plexus.util.FileUtil import org.codehaus.plexus.util.xml.Xpp3Dom; /** + * Custom toolchain factory, declared as <code>ToolchainFactory</code> Plexus component with <code>custom</code> + * hint. * * @author Hervé Boutemy */ @Component( role = ToolchainFactory.class, hint = "custom" ) -public class DefaultCustomToolchainFactory +public class CustomToolchainFactory implements ToolchainFactory { @Requirement private Logger logger; - public DefaultCustomToolchainFactory() - { - } - public ToolchainPrivate createToolchain( ToolchainModel model ) throws MisconfiguredToolchainException { @@ -55,39 +61,22 @@ public class DefaultCustomToolchainFacto { return null; } + DefaultCustomToolchain customToolchain = new DefaultCustomToolchain( model, logger ); - Xpp3Dom dom = (Xpp3Dom) model.getConfiguration(); - Xpp3Dom toolHome = dom.getChild( DefaultCustomToolchain.KEY_TOOLHOME ); - if ( toolHome == null ) - { - throw new MisconfiguredToolchainException( "Custom toolchain without the " - + DefaultCustomToolchain.KEY_TOOLHOME + " configuration element." ); - } - File normal = new File( FileUtils.normalize( toolHome.getValue() ) ); - if ( normal.exists() ) - { - customToolchain.setToolHome( FileUtils.normalize( toolHome.getValue() ) ); - } - else - { - // for this IT, don't really check the toolHome directory exists... - // throw new MisconfiguredToolchainException( "Non-existing tool home configuration at " - // + normal.getAbsolutePath() ); - customToolchain.setToolHome( FileUtils.normalize( toolHome.getValue() ) ); - } - // now populate the provides section. - dom = (Xpp3Dom) model.getProvides(); - Xpp3Dom[] provides = dom.getChildren(); - for ( Xpp3Dom provide : provides ) + // populate the provides section + Properties provides = getModelProperties( model, "provides" ); + + for ( Map.Entry<Object, Object> provide : provides.entrySet() ) { - String key = provide.getName(); - String value = provide.getValue(); + String key = (String) provide.getKey(); + String value = (String) provide.getValue(); if ( value == null ) { throw new MisconfiguredToolchainException( "Provides token '" + key + "' doesn't have any value configured." ); } + if ( "version".equals( key ) ) { customToolchain.addProvideToken( key, RequirementMatcherFactory.createVersionMatcher( value ) ); @@ -97,6 +86,30 @@ public class DefaultCustomToolchainFacto customToolchain.addProvideToken( key, RequirementMatcherFactory.createExactMatcher( value ) ); } } + + // populate the configuration section + Properties configuration = getModelProperties( model, "configuration" ); + + String toolHome = configuration.getProperty( DefaultCustomToolchain.KEY_TOOLHOME ); + if ( toolHome == null ) + { + throw new MisconfiguredToolchainException( "Custom toolchain without the " + + DefaultCustomToolchain.KEY_TOOLHOME + " configuration element." ); + } + + File normal = new File( FileUtils.normalize( toolHome ) ); + if ( normal.exists() ) + { + customToolchain.setToolHome( FileUtils.normalize( toolHome ) ); + } + else + { + // for this IT, don't really check the toolHome directory exists... + // throw new MisconfiguredToolchainException( "Non-existing tool home configuration at " + // + normal.getAbsolutePath() ); + customToolchain.setToolHome( FileUtils.normalize( toolHome ) ); + } + return customToolchain; } @@ -111,4 +124,61 @@ public class DefaultCustomToolchainFacto return logger; } + /** + * Get model properties in in a way compatible with toolchains descriptor version 1.0 + * (Maven 2.0.9 to 3.2.3, where it is represented as Object/DOM) and descriptor version 1.1 + * (Maven 3.2.4 and later, where it is represented as Properties). + * + * @param model the toolchain model as read from XML + * @param property the model XML element to get + * @return the properties defined in the corresponding element + * @see <a href="http://jira.codehaus.org/browse/MNG-5718">MNG-5718</a>, <a href="http://jira.codehaus.org/browse/MNG-5720">MNG-5720</a> + */ + protected Properties getModelProperties( ToolchainModel model, String property ) + { + Object value = getBeanProperty( model, property ); + if ( value instanceof Properties ) + { + return (Properties) value; + } + + Properties props = new Properties(); + Xpp3Dom dom = (Xpp3Dom) value; + Xpp3Dom[] children = dom.getChildren(); + for ( Xpp3Dom child : children ) + { + props.put( child.getName(), child.getValue() ); + } + + return props; + } + + protected Object getBeanProperty( Object obj, String property ) + { + try + { + Method method = new PropertyDescriptor( property, obj.getClass() ).getReadMethod(); + + return method.invoke( obj ); + } + catch ( IntrospectionException e ) + { + throw new RuntimeException( "Incompatible toolchain API", e ); + } + catch ( IllegalAccessException e ) + { + throw new RuntimeException( "Incompatible toolchain API", e ); + } + catch ( InvocationTargetException e ) + { + Throwable cause = e.getCause(); + + if ( cause instanceof RuntimeException ) + { + throw (RuntimeException) cause; + } + + throw new RuntimeException( "Incompatible toolchain API", e ); + } + } }