From df06a3c5eb106984264f366534eb582f8fdacd98 Mon Sep 17 00:00:00 2001 From: Brett Leslie Porter Date: Wed, 13 Apr 2005 05:07:50 +0000 Subject: [PATCH] Initial revision git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163924 13f79535-47bb-0310-9956-ffa450edef68 --- maven-plugin-api/pom.xml | 23 ++ .../apache/maven/plugin/AbstractPlugin.java | 104 ++++++++ .../apache/maven/plugin/FailureResponse.java | 40 +++ .../java/org/apache/maven/plugin/Plugin.java | 38 +++ .../plugin/PluginExecutionException.java | 52 ++++ .../maven/plugin/PluginExecutionRequest.java | 49 ++++ .../maven/plugin/PluginExecutionResponse.java | 55 +++++ maven-plugin-descriptor/pom.xml | 22 ++ .../maven/plugin/descriptor/Dependency.java | 60 +++++ .../plugin/descriptor/MojoDescriptor.java | 229 ++++++++++++++++++ .../maven/plugin/descriptor/Parameter.java | 112 +++++++++ .../plugin/descriptor/PluginDescriptor.java | 90 +++++++ .../descriptor/PluginDescriptorBuilder.java | 186 ++++++++++++++ 13 files changed, 1060 insertions(+) create mode 100755 maven-plugin-api/pom.xml create mode 100644 maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractPlugin.java create mode 100755 maven-plugin-api/src/main/java/org/apache/maven/plugin/FailureResponse.java create mode 100755 maven-plugin-api/src/main/java/org/apache/maven/plugin/Plugin.java create mode 100755 maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionException.java create mode 100755 maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionRequest.java create mode 100755 maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionResponse.java create mode 100755 maven-plugin-descriptor/pom.xml create mode 100644 maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/Dependency.java create mode 100755 maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java create mode 100644 maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java create mode 100644 maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java create mode 100755 maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java diff --git a/maven-plugin-api/pom.xml b/maven-plugin-api/pom.xml new file mode 100755 index 0000000000..4705a66bf9 --- /dev/null +++ b/maven-plugin-api/pom.xml @@ -0,0 +1,23 @@ + + + org.apache.maven + maven + 2.0-SNAPSHOT + + 4.0.0 + maven-plugin-api + Maven Plugin API + + + org.apache.maven + maven-monitor + 2.0-SNAPSHOT + + + junit + junit + 3.8.1 + test + + + diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractPlugin.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractPlugin.java new file mode 100644 index 0000000000..ff59046bd2 --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractPlugin.java @@ -0,0 +1,104 @@ +package org.apache.maven.plugin; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.monitor.logging.Log; +import org.apache.maven.monitor.logging.SystemStreamLog; + +/** + * @version $Id$ + */ +public abstract class AbstractPlugin + implements Plugin +{ + private Log log; + + /** + * Default behaviour to mimic old behaviour. + * + * @deprecated + */ + public void execute( PluginExecutionRequest request ) + throws PluginExecutionException + { + PluginExecutionResponse response = new PluginExecutionResponse(); + try + { + execute( request, response ); + } + catch ( Exception e ) + { + throw new PluginExecutionException( e.getMessage(), e ); + } + if ( response.isExecutionFailure() ) + { + throw new PluginExecutionException( response.getFailureResponse().getSource(), + response.getFailureResponse().shortMessage(), + response.getFailureResponse().longMessage() ); + } + } + + /** + * @deprecated + */ + public void execute( PluginExecutionRequest request, PluginExecutionResponse response ) + throws Exception + { + throw new UnsupportedOperationException( + "If you are using the old technique, you must override execute(req,resp)" ); + } + + public void setLog( Log log ) + { + this.log = log; + } + + public Log getLog() + { + synchronized ( this ) + { + if ( log == null ) + { + log = new SystemStreamLog(); + } + } + + return log; + } + + public void execute() + throws PluginExecutionException + { + throw new PluginExecutionException( "You must override execute() if you implement the new paradigm" ); + } + + /** + * @todo remove - harcoding. + */ + public static String getDefaultPluginArtifactId( String id ) + { + return "maven-" + id + "-plugin"; + } + + /** + * @todo remove - harcoding. + */ + public static String getDefaultPluginGroupId() + { + return "org.apache.maven.plugins"; + } +} diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/FailureResponse.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/FailureResponse.java new file mode 100755 index 0000000000..c649c5096a --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/FailureResponse.java @@ -0,0 +1,40 @@ +package org.apache.maven.plugin; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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. + */ + +/** + * @author Jason van Zyl + * @version $Id$ + */ +public abstract class FailureResponse +{ + protected Object source; + + protected FailureResponse( Object source ) + { + this.source = source; + } + + public abstract String shortMessage(); + + public abstract String longMessage(); + + public Object getSource() + { + return source; + } +} diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/Plugin.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/Plugin.java new file mode 100755 index 0000000000..28cd389299 --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/Plugin.java @@ -0,0 +1,38 @@ +package org.apache.maven.plugin; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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.monitor.logging.Log; + +/** + * @author Jason van Zyl + * @version $Id$ + */ +public interface Plugin +{ + String ROLE = Plugin.class.getName(); + + void execute() + throws PluginExecutionException; + + /** @deprecated */ + void execute( PluginExecutionRequest request ) + throws PluginExecutionException; + + // TODO: not sure about this here, and may want a getLog on here as well/instead + void setLog( Log log ); +} diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionException.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionException.java new file mode 100755 index 0000000000..67b326a260 --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionException.java @@ -0,0 +1,52 @@ +package org.apache.maven.plugin; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +/** + * A failure or exception occuring during the execution of a plugin. + * + * @author Brett Porter + * @version $Id$ + */ +public class PluginExecutionException extends Exception +{ + private Object source; + + private String longMessage; + + public PluginExecutionException( Object source, String shortMessage, String longMessage ) + { + super( shortMessage ); + this.source = source; + this.longMessage = longMessage; + } + + public PluginExecutionException( String message, Exception cause ) + { + super( message, cause ); + } + + public PluginExecutionException( String message ) + { + super( message ); + } + + public String getLongMessage() + { + return longMessage; + } +} diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionRequest.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionRequest.java new file mode 100755 index 0000000000..459e91aac5 --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionRequest.java @@ -0,0 +1,49 @@ +package org.apache.maven.plugin; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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 java.util.Map; + +/** + * @author Jason van Zyl + * @version $Id$ + * @deprecated + */ +public class PluginExecutionRequest +{ + private Map parameters; + + public PluginExecutionRequest( Map parameters ) + { + this.parameters = parameters; + } + + public Map getParameters() + { + return parameters; + } + + public void setParameters( Map parameters ) + { + this.parameters = parameters; + } + + public Object getParameter( String key ) + { + return parameters.get( key ); + } +} diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionResponse.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionResponse.java new file mode 100755 index 0000000000..8211cd682c --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/PluginExecutionResponse.java @@ -0,0 +1,55 @@ +package org.apache.maven.plugin; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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. + */ + +/** + * @deprecated + * @author Jason van Zyl + * @version $Id$ + */ +public class PluginExecutionResponse +{ + private FailureResponse failureResponse = null; + + public boolean isExecutionFailure() + { + return failureResponse != null; + } + + public void setExecutionFailure( FailureResponse failureResponse ) + { + this.failureResponse = failureResponse; + } + + /** + * @deprecated please use {@link #setExecutionFailure(FailureResponse)} as there is no need to set executionFailure to false if there is a failure response + */ + public void setExecutionFailure( boolean executionFailure, FailureResponse failureResponse ) + { + if ( executionFailure == false ) + { + throw new IllegalArgumentException( "executionFailure should be true when passing a failureResponse" ); + } + + setExecutionFailure( failureResponse ); + } + + public FailureResponse getFailureResponse() + { + return failureResponse; + } +} diff --git a/maven-plugin-descriptor/pom.xml b/maven-plugin-descriptor/pom.xml new file mode 100755 index 0000000000..7d5354ba54 --- /dev/null +++ b/maven-plugin-descriptor/pom.xml @@ -0,0 +1,22 @@ + + + org.apache.maven + maven + 2.0-SNAPSHOT + + 4.0.0 + maven-plugin-descriptor + Maven Plugin Descriptor Model + + + org.apache.maven + maven-plugin-api + 2.0-SNAPSHOT + + + plexus + plexus-container-default + 1.0-alpha-2 + + + diff --git a/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/Dependency.java b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/Dependency.java new file mode 100644 index 0000000000..4d78e55e16 --- /dev/null +++ b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/Dependency.java @@ -0,0 +1,60 @@ +package org.apache.maven.plugin.descriptor; + +/* + * LICENSE + */ + +/** + * @author Trygve Laugstøl + * @version $Id$ + */ +public class Dependency +{ + private String groupId; + + private String artifactId; + + private String type; + + private String version; + + public String getGroupId() + { + return groupId; + } + + public void setGroupId( String groupId ) + { + this.groupId = groupId; + } + + public String getArtifactId() + { + return artifactId; + } + + public void setArtifactId( String artifactId ) + { + this.artifactId = artifactId; + } + + public String getType() + { + return type; + } + + public void setType( String type ) + { + this.type = type; + } + + public String getVersion() + { + return version; + } + + public void setVersion( String version ) + { + this.version = version; + } +} diff --git a/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java new file mode 100755 index 0000000000..5b36943e99 --- /dev/null +++ b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java @@ -0,0 +1,229 @@ +package org.apache.maven.plugin.descriptor; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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 java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class MojoDescriptor + implements Cloneable +{ + public static final String SINGLE_PASS_EXEC_STRATEGY = "once-per-session"; + + public static final String MULTI_PASS_EXEC_STRATEGY = "always"; + + private static final String DEFAULT_LANGUAGE = "java"; + + private String implementation; + + private String description; + + private String id; + + private List parameters; + + private Map parameterMap; + + private String instantiationStrategy = "per-lookup"; + + private String executionStrategy = SINGLE_PASS_EXEC_STRATEGY; + + private String goal; + + private String phase; + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + private List prereqs; + + private String requiresDependencyResolution = null; + + private boolean requiresProject = true; + + private String language = DEFAULT_LANGUAGE; + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + public String getRole() + { + return "org.apache.maven.plugin.Plugin"; + } + + public String getImplementation() + { + return implementation; + } + + public void setImplementation( String implementation ) + { + this.implementation = implementation; + } + + public String getDescription() + { + return description; + } + + public void setDescription( String description ) + { + this.description = description; + } + + public String getInstantiationStrategy() + { + return instantiationStrategy; + } + + public void setInstantiationStrategy( String instantiationStrategy ) + { + this.instantiationStrategy = instantiationStrategy; + } + + public String getLanguage() + { + return language; + } + + public void setLanguage( String language ) + { + this.language = language; + } + + public String getId() + { + return id; + } + + public void setId( String id ) + { + this.id = id; + } + + public List getParameters() + { + return parameters; + } + + public void setParameters( List parameters ) + { + this.parameters = parameters; + } + + public Map getParameterMap() + { + if ( parameterMap == null ) + { + parameterMap = new HashMap(); + + for ( Iterator iterator = parameters.iterator(); iterator.hasNext(); ) + { + Parameter pd = (Parameter) iterator.next(); + + parameterMap.put( pd.getName(), pd ); + } + } + + return parameterMap; + } + + // ---------------------------------------------------------------------- + // Dependency requirement + // ---------------------------------------------------------------------- + + public void setRequiresDependencyResolution( String requiresDependencyResolution ) + { + this.requiresDependencyResolution = requiresDependencyResolution; + } + + public String getRequiresDependencyResolution() + { + return requiresDependencyResolution; + } + + // ---------------------------------------------------------------------- + // Project requirement + // ---------------------------------------------------------------------- + + public void setRequiresProject( boolean requiresProject ) + { + this.requiresProject = requiresProject; + } + + public boolean requiresProject() + { + return requiresProject; + } + + // ---------------------------------------------------------------------- + // Prereqs + // ---------------------------------------------------------------------- + + public List getPrereqs() + { + return prereqs; + } + + public void setPrereqs( List prereqs ) + { + this.prereqs = prereqs; + } + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + public String getPhase() + { + return phase; + } + + public void setPhase( String phase ) + { + this.phase = phase; + } + + public String getGoal() + { + return goal; + } + + public void setGoal( String goal ) + { + this.goal = goal; + } + + public boolean alwaysExecute() + { + return MULTI_PASS_EXEC_STRATEGY.equals( executionStrategy ); + } + + public String getExecutionStrategy() + { + return executionStrategy; + } + + public void setExecutionStrategy( String executionStrategy ) + { + this.executionStrategy = executionStrategy; + } +} diff --git a/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java new file mode 100644 index 0000000000..6c709b4176 --- /dev/null +++ b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java @@ -0,0 +1,112 @@ +package org.apache.maven.plugin.descriptor; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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. + */ + +/** + * @author Jason van Zyl + * @version $Id$ + */ +public class Parameter +{ + private String name; + + private String type; + + private boolean required; + + private String validator; + + private String description; + + private String expression; + + private String defaultValue; + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + public String getName() + { + return name; + } + + public void setName( String name ) + { + this.name = name; + } + + public String getType() + { + return type; + } + + public void setType( String type ) + { + this.type = type; + } + + public boolean isRequired() + { + return required; + } + + public void setRequired( boolean required ) + { + this.required = required; + } + + public String getValidator() + { + return validator; + } + + public void setValidator( String validator ) + { + this.validator = validator; + } + + public String getDescription() + { + return description; + } + + public void setDescription( String description ) + { + this.description = description; + } + + public String getExpression() + { + return expression; + } + + public void setExpression( String expression ) + { + this.expression = expression; + } + + public String getDefaultValue() + { + return defaultValue; + } + + public void setDefaultValue( String defaultValue ) + { + this.defaultValue = defaultValue; + } +} diff --git a/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java new file mode 100644 index 0000000000..4c49c8f1f0 --- /dev/null +++ b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java @@ -0,0 +1,90 @@ +package org.apache.maven.plugin.descriptor; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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 java.util.LinkedList; +import java.util.List; + +/** + * @author Jason van Zyl + * @version $Id$ + */ +public class PluginDescriptor +{ + private List mojos; + + private String groupId; + + private String artifactId; + + private List dependencies; + + private boolean isolatedRealm; + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + public List getMojos() + { + return mojos; + } + + public void setMojos( List mojos ) + { + this.mojos = new LinkedList( mojos ); + } + + public String getGroupId() + { + return groupId; + } + + public void setGroupId( String groupId ) + { + this.groupId = groupId; + } + + public String getArtifactId() + { + return artifactId; + } + + public void setArtifactId( String artifactId ) + { + this.artifactId = artifactId; + } + + // ---------------------------------------------------------------------- + // Dependencies + // ---------------------------------------------------------------------- + + public List getDependencies() + { + return dependencies; + } + + public void setDependencies( List dependencies ) + { + this.dependencies = new LinkedList( dependencies ); + } + + public boolean isIsolatedRealm() + { + return isolatedRealm; + } +} diff --git a/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java new file mode 100755 index 0000000000..9e3071157c --- /dev/null +++ b/maven-plugin-descriptor/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java @@ -0,0 +1,186 @@ +package org.apache.maven.plugin.descriptor; + +import org.apache.maven.plugin.AbstractPlugin; +import org.codehaus.plexus.configuration.PlexusConfiguration; +import org.codehaus.plexus.configuration.PlexusConfigurationException; +import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; + +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; + + +/** + * @author Jason van Zyl + * @version $Id$ + */ +public class PluginDescriptorBuilder +{ + public PluginDescriptor build( Reader reader ) + throws PlexusConfigurationException + { + PlexusConfiguration c = buildConfiguration( reader ); + + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + + pluginDescriptor.setGroupId( AbstractPlugin.getDefaultPluginGroupId() ); + pluginDescriptor.setArtifactId( AbstractPlugin.getDefaultPluginArtifactId( c.getChild( "id" ).getValue() ) ); + + // ---------------------------------------------------------------------- + // Components + // ---------------------------------------------------------------------- + + PlexusConfiguration[] mojoConfigurations = c.getChild( "mojos" ).getChildren( "mojo" ); + + List mojos = new ArrayList(); + + for ( int i = 0; i < mojoConfigurations.length; i++ ) + { + PlexusConfiguration component = mojoConfigurations[i]; + + mojos.add( buildComponentDescriptor( component ) ); + } + + pluginDescriptor.setMojos( mojos ); + + // ---------------------------------------------------------------------- + // Dependencies + // ---------------------------------------------------------------------- + + PlexusConfiguration[] dependencyConfigurations = c.getChild( "dependencies" ).getChildren( "dependency" ); + + List dependencies = new ArrayList(); + + for ( int i = 0; i < dependencyConfigurations.length; i++ ) + { + PlexusConfiguration d = dependencyConfigurations[i]; + + Dependency cd = new Dependency(); + + cd.setArtifactId( d.getChild( "artifactId" ).getValue() ); + + cd.setGroupId( d.getChild( "groupId" ).getValue() ); + + cd.setType( d.getChild( "type" ).getValue() ); + + cd.setVersion( d.getChild( "version" ).getValue() ); + + dependencies.add( cd ); + } + + pluginDescriptor.setDependencies( dependencies ); + + return pluginDescriptor; + } + + public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c ) + throws PlexusConfigurationException + { + MojoDescriptor mojo = new MojoDescriptor(); + + mojo.setId( c.getChild( "id" ).getValue() ); + + mojo.setImplementation( c.getChild( "implementation" ).getValue() ); + + PlexusConfiguration langConfig = c.getChild( "language" ); + + if ( langConfig != null ) + { + mojo.setLanguage( langConfig.getValue() ); + } + + String phase = c.getChild( "phase" ).getValue(); + + if ( phase != null ) + { + mojo.setPhase( phase ); + } + + mojo.setInstantiationStrategy( c.getChild( "instantiationStrategy" ).getValue() ); + + mojo.setDescription( c.getChild( "description" ).getValue() ); + + String dependencyResolution = c.getChild( "requiresDependencyResolution" ).getValue(); + + if ( dependencyResolution != null ) + { + mojo.setRequiresDependencyResolution( dependencyResolution ); + } + + // ---------------------------------------------------------------------- + // Parameters + // ---------------------------------------------------------------------- + + PlexusConfiguration[] parameterConfigurations = c.getChild( "parameters" ).getChildren( "parameter" ); + + List parameters = new ArrayList(); + + for ( int i = 0; i < parameterConfigurations.length; i++ ) + { + PlexusConfiguration d = parameterConfigurations[i]; + + Parameter parameter = new Parameter(); + + parameter.setName( d.getChild( "name" ).getValue() ); + + parameter.setType( d.getChild( "type" ).getValue() ); + + String s = d.getChild( "required" ).getValue(); + + if ( s != null ) + { + parameter.setRequired( s.equals( "true" ) ? true : false ); + } + + parameter.setValidator( d.getChild( "validator" ).getValue() ); + + parameter.setDescription( d.getChild( "description" ).getValue() ); + + parameter.setExpression( d.getChild( "expression" ).getValue() ); + + parameter.setDefaultValue( d.getChild( "default" ).getValue() ); + + parameters.add( parameter ); + } + + mojo.setParameters( parameters ); + + // ---------------------------------------------------------------------- + // Prereqs + // ---------------------------------------------------------------------- + + PlexusConfiguration[] prereqConfigurations = c.getChild( "prereqs" ).getChildren( "prereq" ); + + List prereqs = new ArrayList(); + + for ( int i = 0; i < prereqConfigurations.length; i++ ) + { + PlexusConfiguration d = prereqConfigurations[i]; + + prereqs.add( d.getValue() ); + } + + mojo.setPrereqs( prereqs ); + + return mojo; + } + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + // TODO: catches Exception + public PlexusConfiguration buildConfiguration( Reader configuration ) + throws PlexusConfigurationException + { + try + { + return new XmlPlexusConfiguration( Xpp3DomBuilder.build( configuration ) ); + } + catch ( Exception e ) + { + throw new PlexusConfigurationException( "Error creating configuration", e ); + } + } +}