brett 2005/03/22 02:46:55
Modified: maven-core/src/main/java/org/apache/maven/plugin DefaultPluginManager.java maven-core/src/main/java/org/apache/maven/project/inheritance DefaultModelInheritanceAssembler.java maven-core/src/main/java/org/apache/maven/project/injection DefaultModelDefaultsInjector.java maven-core/src/main/resources/META-INF/plexus plexus.xml maven-core/src/test/java/org/apache/maven/project/canonical CanonicalProjectBuilderTest.java maven-model maven.mdo Log: clean up plugin configuration handling Revision Changes Path 1.65 +182 -129 maven-components/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java Index: DefaultPluginManager.java =================================================================== RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java,v retrieving revision 1.64 retrieving revision 1.65 diff -u -r1.64 -r1.65 --- DefaultPluginManager.java 21 Mar 2005 08:18:33 -0000 1.64 +++ DefaultPluginManager.java 22 Mar 2005 10:46:55 -0000 1.65 @@ -40,13 +40,14 @@ import org.codehaus.plexus.ArtifactEnabledContainer; import org.codehaus.plexus.PlexusConstants; import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.component.configurator.BasicComponentConfigurator; import org.codehaus.plexus.component.configurator.ComponentConfigurationException; import org.codehaus.plexus.component.configurator.ComponentConfigurator; import org.codehaus.plexus.component.discovery.ComponentDiscoveryEvent; import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener; import org.codehaus.plexus.component.repository.ComponentSetDescriptor; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.configuration.PlexusConfiguration; +import org.codehaus.plexus.configuration.PlexusConfigurationException; import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; import org.codehaus.plexus.context.Context; import org.codehaus.plexus.context.ContextException; @@ -59,7 +60,6 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; import java.lang.reflect.Field; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -89,6 +89,8 @@ protected MavenSettingsBuilder mavenSettingsBuilder; + protected ComponentConfigurator configurator; + public DefaultPluginManager() { mojoDescriptors = new HashMap(); @@ -136,7 +138,8 @@ private Set pluginsInProcess = new HashSet(); - public void processPluginDescriptor( MavenPluginDescriptor mavenPluginDescriptor ) throws CycleDetectedException + public void processPluginDescriptor( MavenPluginDescriptor mavenPluginDescriptor ) + throws CycleDetectedException { if ( pluginsInProcess.contains( mavenPluginDescriptor.getPluginId() ) ) { @@ -207,7 +210,8 @@ } // TODO: don't throw Exception - public void verifyPluginForGoal( String goalName, MavenSession session ) throws Exception + public void verifyPluginForGoal( String goalName, MavenSession session ) + throws Exception { String pluginId = getPluginId( goalName ); @@ -216,7 +220,8 @@ } // TODO: don't throw Exception - public void verifyPlugin( String groupId, String artifactId, MavenSession session ) throws Exception + public void verifyPlugin( String groupId, String artifactId, MavenSession session ) + throws Exception { if ( !isPluginInstalled( groupId, artifactId ) ) { @@ -263,7 +268,7 @@ // TODO: more hard coding here... Artifact pluginArtifact = artifactFactory.createArtifact( "maven", artifactId, version, null, - "maven-plugin", "jar", null ); + "maven-plugin", null ); addPlugin( pluginArtifact, session ); } @@ -278,7 +283,8 @@ } // TODO: don't throw Exception - protected void addPlugin( Artifact pluginArtifact, MavenSession session ) throws Exception + protected void addPlugin( Artifact pluginArtifact, MavenSession session ) + throws Exception { ArtifactResolver artifactResolver = null; MavenProjectBuilder mavenProjectBuilder = null; @@ -314,7 +320,8 @@ // Plugin execution // ---------------------------------------------------------------------- - public void executeMojo( MavenSession session, String goalName ) throws PluginExecutionException + public void executeMojo( MavenSession session, String goalName ) + throws PluginExecutionException { try { @@ -388,13 +395,19 @@ // intentionally ignored } + // TODO: can probable refactor these a little when only the new plugin technique is in place + PlexusConfiguration configuration = getProjectDefinedPluginConfiguration( session.getProject(), + mojoDescriptor.getId() ); + + Map map = getPluginConfigurationFromExpressions( mojoDescriptor, configuration, session ); + if ( newMojoTechnique ) { - populateParameters( plugin, mojoDescriptor, session ); + populatePluginFields( plugin, configuration, map ); } else { - request = new PluginExecutionRequest( createParameters( mojoDescriptor, session ) ); + request = createPluginRequest( configuration, map ); } // !! This is ripe for refactoring to an aspect. @@ -449,7 +462,8 @@ } // TODO: don't throw Exception - private void releaseComponents( MojoDescriptor goal, PluginExecutionRequest request ) throws Exception + private void releaseComponents( MojoDescriptor goal, PluginExecutionRequest request ) + throws Exception { if ( request != null && request.getParameters() != null ) { @@ -475,76 +489,77 @@ // Mojo Parameter Handling // ---------------------------------------------------------------------- - private void populateParameters( Plugin plugin, MojoDescriptor mojoDescriptor, MavenSession session ) + private static PluginExecutionRequest createPluginRequest( PlexusConfiguration configuration, Map map ) throws PluginConfigurationException { - // TODO: merge eventually, just to avoid reuse - // TODO: probably want to use the plexus component configurator... then do the additional processing in - // createParameters afterwards. Not sure how we might find files that are nested in other objects... perhaps - // we add a "needs translation" to the mojo so such types can be translated (implementing some interface) and - // address their own file objects - Map values = createParameters( mojoDescriptor, session ); - - List parameters = mojoDescriptor.getParameters(); - - Xpp3Dom dom = new Xpp3Dom( mojoDescriptor.getId() ); - - for ( Iterator i = parameters.iterator(); i.hasNext(); ) + try { - Parameter param = (Parameter) i.next(); - String name = param.getName(); - Object value = values.get( name ); - - // TODO:Still not complete robust - need to merge in the processing in createParameters - if ( value instanceof String ) - { - Xpp3Dom d = new Xpp3Dom( name ); - d.setValue( (String) value ); - dom.addChild( d ); - } - else + Map parameters = new HashMap(); + PlexusConfiguration[] children = configuration.getChildren(); + for ( int i = 0; i < children.length; i++ ) { - Class clazz = plugin.getClass(); - try - { - Field f = clazz.getDeclaredField( name ); - boolean accessible = f.isAccessible(); - if ( !accessible ) - { - f.setAccessible( true ); - } - - f.set( plugin, value ); - - if ( !accessible ) - { - f.setAccessible( false ); - } - } - catch ( NoSuchFieldException e ) - { - throw new PluginConfigurationException( "Unable to set field '" + name + "' on '" + clazz + "'" ); - } - catch ( IllegalAccessException e ) - { - throw new PluginConfigurationException( "Unable to set field '" + name + "' on '" + clazz + "'" ); - } + PlexusConfiguration child = children[i]; + parameters.put( child.getName(), child.getValue() ); } + map = CollectionUtils.mergeMaps( map, parameters ); + } + catch ( PlexusConfigurationException e ) + { + throw new PluginConfigurationException( "Unable to construct map from plugin configuration", e ); } + return new PluginExecutionRequest( map ); + } - // TODO: should be a component - ComponentConfigurator configurator = new BasicComponentConfigurator(); + private void populatePluginFields( Plugin plugin, PlexusConfiguration configuration, Map map ) + throws PluginConfigurationException + { try { - configurator.configureComponent( plugin, new XmlPlexusConfiguration( dom ) ); + configurator.configureComponent( plugin, configuration ); } catch ( ComponentConfigurationException e ) { throw new PluginConfigurationException( "Unable to parse the created DOM for plugin configuration", e ); } + + // Configuration does not store objects, so the non-String fields are configured here + // TODO: we don't have converters, so something things that -are- strings are not configured properly (eg String -> File from an expression) + for ( Iterator i = map.keySet().iterator(); i.hasNext(); ) + { + String key = (String) i.next(); + Object value = map.get( key ); + + Class clazz = plugin.getClass(); + try + { + Field f = clazz.getDeclaredField( key ); + boolean accessible = f.isAccessible(); + if ( !accessible ) + { + f.setAccessible( true ); + } + + f.set( plugin, value ); + + if ( !accessible ) + { + f.setAccessible( false ); + } + } + catch ( NoSuchFieldException e1 ) + { + throw new PluginConfigurationException( "Unable to set field '" + key + "' on '" + clazz + "'" ); + } + catch ( IllegalAccessException e11 ) + { + throw new PluginConfigurationException( "Unable to set field '" + key + "' on '" + clazz + "'" ); + } + } } - public Map createParameters( MojoDescriptor goal, MavenSession session ) throws PluginConfigurationException + private Map getPluginConfigurationFromExpressions( MojoDescriptor goal, PlexusConfiguration configuration, + MavenSession session ) + throws PluginConfigurationException { List parameters = goal.getParameters(); @@ -556,65 +571,57 @@ String key = parameter.getName(); - String expression = parameter.getExpression(); + if ( configuration.getChild( key, false ) == null ) + { + String expression = parameter.getExpression(); - Object value = PluginParameterExpressionEvaluator.evaluate( expression, session ); + Object value = PluginParameterExpressionEvaluator.evaluate( expression, session ); - getLogger().debug( "Evaluated mojo parameter expression: \'" + expression + "\' to: " + value ); + getLogger().debug( "Evaluated mojo parameter expression: \'" + expression + "\' to: " + value ); - if ( value == null ) - { - if ( parameter.getDefaultValue() != null ) + if ( value == null ) { - value = PluginParameterExpressionEvaluator.evaluate( parameter.getDefaultValue(), session ); + if ( parameter.getDefaultValue() != null ) + { + value = PluginParameterExpressionEvaluator.evaluate( parameter.getDefaultValue(), session ); + } } - } - - map.put( key, value ); - } - - if ( session.getProject() != null ) - { - map = mergeProjectDefinedPluginConfiguration( session.getProject(), goal.getId(), map ); - } - - for ( int i = 0; i < parameters.size(); i++ ) - { - Parameter parameter = (Parameter) parameters.get( i ); - - String key = parameter.getName(); - Object value = map.get( key ); + // ---------------------------------------------------------------------- + // We will perform a basic check here for parameters values that are + // required. Required parameters can't be null so we throw an + // Exception in the case where they are. We probably want some + // pluggable + // mechanism here but this will catch the most obvious of + // misconfigurations. + // ---------------------------------------------------------------------- - // ---------------------------------------------------------------------- - // We will perform a basic check here for parameters values that are - // required. Required parameters can't be null so we throw an - // Exception in the case where they are. We probably want some - // pluggable - // mechanism here but this will catch the most obvious of - // misconfigurations. - // ---------------------------------------------------------------------- + if ( value == null && parameter.isRequired() ) + { + throw new PluginConfigurationException( createPluginParameterRequiredMessage( goal, parameter ) ); + } - if ( value == null && parameter.isRequired() ) - { - throw new PluginConfigurationException( createPluginParameterRequiredMessage( goal, parameter ) ); - } + String type = parameter.getType(); - String type = parameter.getType(); + // TODO: Not sure how we might find files that are nested in other objects... perhaps + // we add a "needs translation" to the mojo so such types can be translated (implementing some interface) and + // address their own file objects + if ( type != null && ( type.equals( "File" ) || type.equals( "java.io.File" ) ) ) + { + value = pathTranslator.alignToBaseDirectory( (String) value, + session.getProject().getFile().getParentFile() ); + } - if ( type != null && ( type.equals( "File" ) || type.equals( "java.io.File" ) ) ) - { - value = pathTranslator.alignToBaseDirectory( (String) value, session.getProject().getFile() - .getParentFile() ); map.put( key, value ); } } - return map; } - public static Map mergeProjectDefinedPluginConfiguration( MavenProject project, String goalId, Map map ) + private static PlexusConfiguration getProjectDefinedPluginConfiguration( MavenProject project, String goalId ) { + Xpp3Dom dom = null; + // ---------------------------------------------------------------------- // I would like to be able to lookup the Plugin object using a key but // we have a limitation in modello that will be remedied shortly. So @@ -625,14 +632,14 @@ { String pluginId = getPluginId( goalId ); - for ( Iterator iterator = project.getPlugins().iterator(); iterator.hasNext(); ) + for ( Iterator iterator = project.getPlugins().iterator(); iterator.hasNext() && dom == null; ) { org.apache.maven.model.Plugin plugin = (org.apache.maven.model.Plugin) iterator.next(); // TODO: groupID not handled if ( pluginId.equals( plugin.getArtifactId() ) ) { - map = CollectionUtils.mergeMaps( plugin.getConfiguration(), map ); + dom = (Xpp3Dom) plugin.getConfiguration(); // TODO: much less of this magic is needed - make the mojoDescriptor just store the first and second part int index = goalId.indexOf( ':' ); @@ -644,26 +651,78 @@ Goal goal = (Goal) j.next(); if ( goal.getId().equals( goalName ) ) { - map = CollectionUtils.mergeMaps( goal.getConfiguration(), map ); + Xpp3Dom goalConfiguration = copyXpp3Dom( (Xpp3Dom) goal.getConfiguration() ); + mergeXpp3Dom( goalConfiguration, dom ); + dom = goalConfiguration; break; } } } - - return map; } } } - return map; + PlexusConfiguration configuration; + if ( dom == null ) + { + configuration = new XmlPlexusConfiguration( "configuration" ); + } + else + { + configuration = new XmlPlexusConfiguration( dom ); + } + + return configuration; + } + + private static void mergeXpp3Dom( Xpp3Dom dominant, Xpp3Dom recessive ) + { + // TODO: how to merge lists rather than override? + // TODO: share this as some sort of assembler, implement a walk interface? + Xpp3Dom[] children = recessive.getChildren(); + for ( int i = 0; i < children.length; i++ ) + { + Xpp3Dom child = children[i]; + Xpp3Dom childDom = dominant.getChild( child.getName() ); + if ( childDom != null ) + { + mergeXpp3Dom( childDom, child ); + } + else + { + dominant.addChild( copyXpp3Dom( child ) ); + } + } + } + + private static Xpp3Dom copyXpp3Dom( Xpp3Dom src ) + { + // TODO: into Xpp3Dom as a copy constructor + Xpp3Dom dom = new Xpp3Dom( src.getName() ); + dom.setValue( src.getValue() ); + + String[] attributeNames = src.getAttributeNames(); + for ( int i = 0; i < attributeNames.length; i++ ) + { + String attributeName = attributeNames[i]; + dom.setAttribute( attributeName, src.getAttribute( attributeName ) ); + } + + Xpp3Dom[] children = src.getChildren(); + for ( int i = 0; i < children.length; i++ ) + { + dom.addChild( copyXpp3Dom( children[i] ) ); + } + + return dom; } public static String createPluginParameterRequiredMessage( MojoDescriptor mojo, Parameter parameter ) { StringBuffer message = new StringBuffer(); - message.append( "The '" + parameter.getName() ).append( "' parameter is required for the execution of the " ) - .append( mojo.getId() ).append( " mojo and cannot be null." ); + message.append( "The '" + parameter.getName() ).append( "' parameter is required for the execution of the " ).append( + mojo.getId() ).append( " mojo and cannot be null." ); return message.toString(); } @@ -672,7 +731,8 @@ // Lifecycle // ---------------------------------------------------------------------- - public void contextualize( Context context ) throws ContextException + public void contextualize( Context context ) + throws ContextException { container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); } @@ -680,18 +740,11 @@ public void initialize() { // TODO: configure this from bootstrap or scan lib - artifactFilter = new ExclusionSetFilter( new String[] { - "maven-core", - "maven-artifact", - "maven-model", - "maven-settings", - "maven-monitor", - "maven-plugin", - "plexus-container-api", - "plexus-container-default", - "plexus-artifact-container", - "wagon-provider-api", - "classworlds" } ); + artifactFilter = new ExclusionSetFilter( new String[]{"maven-core", "maven-artifact", "maven-model", + "maven-settings", "maven-monitor", "maven-plugin", + "plexus-container-api", "plexus-container-default", + "plexus-artifact-container", "wagon-provider-api", + "classworlds"} ); } @@ -700,7 +753,7 @@ // ---------------------------------------------------------------------- private void resolveTransitiveDependencies( MavenSession context, ArtifactResolver artifactResolver, - MavenProjectBuilder mavenProjectBuilder ) + MavenProjectBuilder mavenProjectBuilder ) throws ArtifactResolutionException { MavenProject project = context.getProject(); 1.24 +3 -10 maven-components/maven-core/src/main/java/org/apache/maven/project/inheritance/DefaultModelInheritanceAssembler.java Index: DefaultModelInheritanceAssembler.java =================================================================== RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/project/inheritance/DefaultModelInheritanceAssembler.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- DefaultModelInheritanceAssembler.java 16 Mar 2005 01:00:32 -0000 1.23 +++ DefaultModelInheritanceAssembler.java 22 Mar 2005 10:46:55 -0000 1.24 @@ -31,7 +31,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.TreeMap; /** @@ -265,16 +264,10 @@ } else { - Boolean disabled = childGoal.isDisabled(); - if ( disabled == null ) + // TODO: configuration not currently merged + if ( childGoal.getConfiguration() == null ) { - childGoal.setDisabled( parentGoal.isDisabled() ); - - Properties conf = new Properties( childGoal.getConfiguration() ); - - conf.putAll( parentGoal.getConfiguration() ); - - childGoal.setConfiguration( conf ); + childGoal.setConfiguration( parentGoal.getConfiguration() ); } } } 1.7 +10 -17 maven-components/maven-core/src/main/java/org/apache/maven/project/injection/DefaultModelDefaultsInjector.java Index: DefaultModelDefaultsInjector.java =================================================================== RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/project/injection/DefaultModelDefaultsInjector.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- DefaultModelDefaultsInjector.java 10 Mar 2005 23:01:37 -0000 1.6 +++ DefaultModelDefaultsInjector.java 22 Mar 2005 10:46:55 -0000 1.7 @@ -27,7 +27,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.TreeMap; /** @@ -92,12 +91,6 @@ plugin.setVersion( def.getVersion() ); } - Boolean disabled = plugin.isDisabled(); - if ( disabled == null ) - { - plugin.setDisabled( def.isDisabled() ); - } - Map goalMap = new TreeMap(); List pluginGoals = plugin.getGoals(); @@ -125,22 +118,22 @@ } else { - Properties conf = defaultGoal.getConfiguration(); - - conf.putAll( localGoal.getConfiguration() ); - - localGoal.setConfiguration( conf ); + // TODO: merge + if ( localGoal.getConfiguration() == null ) + { + localGoal.setConfiguration( defaultGoal.getConfiguration() ); + } } } } plugin.setGoals( new ArrayList( goalMap.values() ) ); - Properties props = new Properties( def.getConfiguration() ); - - props.putAll( plugin.getConfiguration() ); - - plugin.setConfiguration( props ); + // TODO: merge + if ( plugin.getConfiguration() == null ) + { + plugin.setConfiguration( def.getConfiguration() ); + } } private void injectDependencyDefaults( List dependencies, DependencyManagement dependencyManagement ) 1.8 +3 -0 maven-components/maven-core/src/main/resources/META-INF/plexus/plexus.xml Index: plexus.xml =================================================================== RCS file: /home/cvs/maven-components/maven-core/src/main/resources/META-INF/plexus/plexus.xml,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- plexus.xml 16 Mar 2005 06:29:33 -0000 1.7 +++ plexus.xml 22 Mar 2005 10:46:55 -0000 1.8 @@ -30,6 +30,9 @@ <requirement> <role>org.apache.maven.settings.MavenSettingsBuilder</role> </requirement> + <requirement> + <role>org.codehaus.plexus.component.configurator.ComponentConfigurator</role> + </requirement> </requirements> </component> <component> 1.8 +9 -8 maven-components/maven-core/src/test/java/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.java Index: CanonicalProjectBuilderTest.java =================================================================== RCS file: /home/cvs/maven-components/maven-core/src/test/java/org/apache/maven/project/canonical/CanonicalProjectBuilderTest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- CanonicalProjectBuilderTest.java 15 Mar 2005 21:41:06 -0000 1.7 +++ CanonicalProjectBuilderTest.java 22 Mar 2005 10:46:55 -0000 1.8 @@ -20,10 +20,10 @@ import org.apache.maven.model.Goal; import org.apache.maven.model.Plugin; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.xml.Xpp3Dom; import java.io.File; import java.util.List; -import java.util.Properties; /** * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a> @@ -61,13 +61,14 @@ assertEquals( "1.0", plugin.getVersion() ); - Properties properties = plugin.getConfiguration(); + Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration(); - assertEquals( "src/conf/plexus.conf", properties.getProperty( "plexusConfiguration" ) ); + assertEquals( "src/conf/plexus.conf", configuration.getChild( "plexusConfiguration" ).getValue() ); - assertEquals( "src/conf/plexus.properties", properties.getProperty( "plexusConfigurationPropertiesFile" ) ); + assertEquals( "src/conf/plexus.properties", + configuration.getChild( "plexusConfigurationPropertiesFile" ).getValue() ); - assertEquals( "Continuum", properties.getProperty( "plexusApplicationName" ) ); + assertEquals( "Continuum", configuration.getChild( "plexusApplicationName" ).getValue() ); // ---------------------------------------------------------------------- // Goal specific configuration @@ -79,9 +80,9 @@ assertEquals( "plexus:runtime", g0.getId() ); - Properties goalProperties = g0.getConfiguration(); + configuration = (Xpp3Dom) g0.getConfiguration(); - assertEquals( "ContinuumPro", goalProperties.getProperty( "plexusApplicationName" ) ); + assertEquals( "ContinuumPro", configuration.getChild( "plexusApplicationName" ).getValue() ); // Plugin1 [antlr] } 1.92 +2 -22 maven-components/maven-model/maven.mdo Index: maven.mdo =================================================================== RCS file: /home/cvs/maven-components/maven-model/maven.mdo,v retrieving revision 1.91 retrieving revision 1.92 diff -u -r1.91 -r1.92 --- maven.mdo 22 Mar 2005 09:58:19 -0000 1.91 +++ maven.mdo 22 Mar 2005 10:46:55 -0000 1.92 @@ -1976,18 +1976,8 @@ <type>String</type> </field> <field> - <name>disabled</name> - <version>4.0.0</version> - <type>String</type> - <!-- defaultValue>false</defaultValue --> - </field> - <field> <name>configuration</name> - <type>Properties</type> - <association xml.mapStyle="inline"> - <type>String</type> - <multiplicity>*</multiplicity> - </association> + <type>DOM</type> </field> <field> <name>goals</name> @@ -2009,18 +1999,8 @@ <type>String</type> </field> <field> - <name>disabled</name> - <version>4.0.0</version> - <type>String</type> - <!-- defaultValue>false</defaultValue --> - </field> - <field> <name>configuration</name> - <type>Properties</type> - <association xml.mapStyle="inline"> - <type>String</type> - <multiplicity>*</multiplicity> - </association> + <type>DOM</type> </field> </fields> </class>