From f3a5e9b2bbc9e52cd89e0ed38a0bb259a36da91f Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 24 Oct 2022 13:21:30 +0200 Subject: [PATCH 1/2] [MNG-7588] generate reader and some beans automatically for plugin descriptor WIP --- maven-plugin-api/pom.xml | 72 +++- .../InvalidPluginDescriptorException.java | 7 +- .../plugin/descriptor/MojoDescriptor.java | 376 +++++++++++++++++- .../maven/plugin/descriptor/Parameter.java | 207 ---------- .../plugin/descriptor/PluginDescriptor.java | 216 +++++++++- .../descriptor/PluginDescriptorBuilder.java | 148 +------ .../maven/plugin/descriptor/Requirement.java | 72 ---- maven-plugin-api/src/main/mdo/plugin.mdo | 76 +++- .../plugin/descriptor/MojoDescriptorTest.java | 8 +- .../PluginDescriptorBuilderTest.java | 2 +- .../PluginDescriptorXpp3ReaderTest.java | 142 +++++++ 11 files changed, 882 insertions(+), 444 deletions(-) delete mode 100644 maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java delete mode 100644 maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Requirement.java create mode 100644 maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorXpp3ReaderTest.java diff --git a/maven-plugin-api/pom.xml b/maven-plugin-api/pom.xml index 198a90c10271..f2b7b3a9c697 100644 --- a/maven-plugin-api/pom.xml +++ b/maven-plugin-api/pom.xml @@ -61,20 +61,13 @@ under the License. org.codehaus.modello modello-maven-plugin - 2.0.0 - - - src/main/mdo/lifecycle.mdo - - 1.0.0 - modello - none + none - modello-site-docs + modello-site-docs-plugin-1.1 pre-site @@ -83,6 +76,18 @@ under the License. 1.1.0 + + modello-site-docs-lifecycle + pre-site + + + + src/main/mdo/lifecycle.mdo + + 1.0.0 + + + @@ -90,7 +95,7 @@ under the License. modello-plugin-velocity - velocity + velocity-lifecycle generate-sources velocity @@ -112,6 +117,53 @@ under the License. + + velocity-plugin + generate-sources + + velocity + + + 1.1.0 + + src/main/mdo/plugin.mdo + + + + + + + packageModelV3=org.apache.maven.plugin.descriptor + packageModelV4=org.apache.maven.plugin.descriptor + packageToolV4=org.apache.maven.plugin.descriptor.io.xpp3 + + + + + + + maven-clean-plugin + + + remove-superfluous-generated-classes + generate-sources + + clean + + + true + + + target/generated-sources/modello/org/apache/maven/plugin/descriptor + + + MojoDescriptor.java + PluginDescriptor.java + + + + + diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/InvalidPluginDescriptorException.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/InvalidPluginDescriptorException.java index f63fd26ce469..12f9655c5215 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/InvalidPluginDescriptorException.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/InvalidPluginDescriptorException.java @@ -19,18 +19,19 @@ * under the License. */ -import org.codehaus.plexus.configuration.PlexusConfigurationException; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** * InvalidPluginDescriptorException */ public class InvalidPluginDescriptorException - extends PlexusConfigurationException + extends XmlPullParserException { public InvalidPluginDescriptorException( String message, Throwable cause ) { - super( message, cause ); + super( message ); + this.initCause( cause ); } public InvalidPluginDescriptorException( String message ) diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java index 80829332a972..ea6c3d40d3cd 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java @@ -20,15 +20,20 @@ */ import java.util.ArrayList; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.NotThreadSafe; +import org.apache.maven.api.xml.Dom; import org.apache.maven.plugin.Mojo; import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; +import org.codehaus.plexus.util.xml.Xpp3Dom; /** * The bean containing the Mojo descriptor.
@@ -36,8 +41,8 @@ * * https://maven.apache.org/developers/mojo-api-specification.html * - * TODO is there a need for the delegation of MavenMojoDescriptor to this? - * Why not just extend ComponentDescriptor here? + * This file is not generated from the underlying Modello model as it needs to extend {@link ComponentDescriptor} + * which is not based on a model. */ public class MojoDescriptor extends ComponentDescriptor @@ -200,12 +205,13 @@ public List getParameters() * @param parameters the new list of parameters * @throws DuplicateParameterException if any */ - public void setParameters( List parameters ) + public void setParameters( Collection parameters ) throws DuplicateParameterException { this.parameters.clear(); for ( Parameter parameter : parameters ) { + // enrich parameter with information from configuration (already available?) addParameter( parameter ); } } @@ -223,10 +229,24 @@ public void addParameter( Parameter parameter ) + " has been declared multiple times in mojo with goal: " + getGoal() + " (implementation: " + getImplementation() + ")" ); } - + if ( mojoConfiguration != null ) + { + setParameterValuesFromMojoConfiguration( parameter ); + } parameters.add( parameter ); } + private boolean setParameterValuesFromMojoConfiguration( Parameter parameter ) + { + PlexusConfiguration paramConfig = mojoConfiguration.getChild( parameter.getName(), false ); + if ( paramConfig != null ) + { + parameter.setExpression( paramConfig.getValue( null ) ); + parameter.setDefaultValue( paramConfig.getAttribute( "default-value" ) ); + return true; + } + return false; + } /** * @return the list parameters as a Map (keyed by {@link Parameter#getName()}) that is built from * {@link #parameters} list on each call. In other words, the map returned is built on fly and is a copy. @@ -452,6 +472,10 @@ public PlexusConfiguration getMojoConfiguration() public void setMojoConfiguration( PlexusConfiguration mojoConfiguration ) { this.mojoConfiguration = mojoConfiguration; + for ( Parameter parameter : parameters ) + { + setParameterValuesFromMojoConfiguration( parameter ); + } } /** {@inheritDoc} */ @@ -668,6 +692,17 @@ public void setV4Api( boolean v4Api ) this.v4Api = v4Api; } + public final void setMojoConfiguration( final Xpp3Dom configuration ) + { + setMojoConfiguration( new XmlPlexusConfiguration( configuration ) ); + } + + + public void setRequirements( Collection requirements ) + { + requirements.stream().map( Requirement::toComponentRequirement ).forEach( this::addRequirement ); + } + /** * Creates a shallow copy of this mojo descriptor. */ @@ -684,4 +719,337 @@ public MojoDescriptor clone() } } + + /** + * Creates a new MojoDescriptor instance. + * Equivalent to {@code newInstance( true )}. + * @throws DuplicateParameterException + * @see #newInstance(boolean) + */ + @Nonnull + public static MojoDescriptor newInstance() throws DuplicateParameterException + { + return newInstance( true ); + } + + /** + * Creates a new MojoDescriptor instance using default values or not. + * Equivalent to {@code newBuilder( withDefaults ).build()}. + * @throws DuplicateParameterException + */ + @Nonnull + public static MojoDescriptor newInstance( boolean withDefaults ) throws DuplicateParameterException + { + return newBuilder( withDefaults ).build(); + } + + /** + * Creates a new MojoDescriptor builder instance. + * Equivalent to {@code newBuilder( true )}. + * @see #newBuilder(boolean) + */ + @Nonnull + public static Builder newBuilder() + { + return newBuilder( true ); + } + + /** + * Creates a new MojoDescriptor builder instance using default values or not. + */ + @Nonnull + public static Builder newBuilder( boolean withDefaults ) + { + return new Builder( withDefaults ); + } + + /** + * Builder class used to create MojoDescriptor instances. + * @see #with() + * @see #newBuilder() + */ + @NotThreadSafe + public static class Builder + { + String goal; + String description; + String implementation; + String language; + String phase; + String executePhase; + String executeGoal; + String executeLifecycle; + String requiresDependencyResolution; + String requiresDependencyCollection; + Boolean requiresDirectInvocation; + Boolean requiresProject; + Boolean requiresReports; + Boolean requiresOnline; + Boolean aggregator; + Boolean inheritedByDefault; + Boolean threadSafe; + Boolean v4Api; + String instantiationStrategy; + String executionStrategy; + String since; + String deprecated; + String configurator; + String composer; + Collection parameters; + Dom configuration; + Collection requirements; + + Builder( boolean withDefaults ) + { + if ( withDefaults ) + { + this.language = "java"; + this.requiresDependencyResolution = "runtime"; + this.requiresDirectInvocation = false; + this.requiresProject = true; + this.requiresReports = false; + this.requiresOnline = false; + this.aggregator = false; + this.inheritedByDefault = true; + this.threadSafe = false; + this.v4Api = false; + this.instantiationStrategy = "per-lookup"; + this.executionStrategy = "once-per-session"; + } + } + + @Nonnull + public Builder goal( String goal ) + { + this.goal = goal; + return this; + } + + @Nonnull + public Builder description( String description ) + { + this.description = description; + return this; + } + + @Nonnull + public Builder implementation( String implementation ) + { + this.implementation = implementation; + return this; + } + + @Nonnull + public Builder language( String language ) + { + this.language = language; + return this; + } + + @Nonnull + public Builder phase( String phase ) + { + this.phase = phase; + return this; + } + + @Nonnull + public Builder executePhase( String executePhase ) + { + this.executePhase = executePhase; + return this; + } + + @Nonnull + public Builder executeGoal( String executeGoal ) + { + this.executeGoal = executeGoal; + return this; + } + + @Nonnull + public Builder executeLifecycle( String executeLifecycle ) + { + this.executeLifecycle = executeLifecycle; + return this; + } + + @Nonnull + public Builder requiresDependencyResolution( String requiresDependencyResolution ) + { + this.requiresDependencyResolution = requiresDependencyResolution; + return this; + } + + @Nonnull + public Builder requiresDependencyCollection( String requiresDependencyCollection ) + { + this.requiresDependencyCollection = requiresDependencyCollection; + return this; + } + + @Nonnull + public Builder requiresDirectInvocation( boolean requiresDirectInvocation ) + { + this.requiresDirectInvocation = requiresDirectInvocation; + return this; + } + + @Nonnull + public Builder requiresProject( boolean requiresProject ) + { + this.requiresProject = requiresProject; + return this; + } + + @Nonnull + public Builder requiresReports( boolean requiresReports ) + { + this.requiresReports = requiresReports; + return this; + } + + @Nonnull + public Builder requiresOnline( boolean requiresOnline ) + { + this.requiresOnline = requiresOnline; + return this; + } + + @Nonnull + public Builder aggregator( boolean aggregator ) + { + this.aggregator = aggregator; + return this; + } + + @Nonnull + public Builder inheritedByDefault( boolean inheritedByDefault ) + { + this.inheritedByDefault = inheritedByDefault; + return this; + } + + @Nonnull + public Builder threadSafe( boolean threadSafe ) + { + this.threadSafe = threadSafe; + return this; + } + + @Nonnull + public Builder v4Api( boolean v4Api ) + { + this.v4Api = v4Api; + return this; + } + + @Nonnull + public Builder instantiationStrategy( String instantiationStrategy ) + { + this.instantiationStrategy = instantiationStrategy; + return this; + } + + @Nonnull + public Builder executionStrategy( String executionStrategy ) + { + this.executionStrategy = executionStrategy; + return this; + } + + @Nonnull + public Builder since( String since ) + { + this.since = since; + return this; + } + + @Nonnull + public Builder deprecated( String deprecated ) + { + this.deprecated = deprecated; + return this; + } + + @Nonnull + public Builder configurator( String configurator ) + { + this.configurator = configurator; + return this; + } + + @Nonnull + public Builder composer( String composer ) + { + this.composer = composer; + return this; + } + + @Nonnull + public Builder parameters( Collection parameters ) + { + this.parameters = parameters; + return this; + } + + @Nonnull + public Builder configuration( Dom configuration ) + { + this.configuration = configuration; + return this; + } + + @Nonnull + public Builder requirements( Collection requirements ) + { + this.requirements = requirements; + return this; + } + + @Nonnull + public MojoDescriptor build() throws DuplicateParameterException + { + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setGoal( goal ); + mojoDescriptor.setDescription( description ); + mojoDescriptor.setImplementation( implementation ); + mojoDescriptor.setLanguage( language ); + mojoDescriptor.setPhase( phase ); + mojoDescriptor.setExecutePhase( executePhase ); + mojoDescriptor.setExecuteGoal( executeGoal ); + mojoDescriptor.setExecuteLifecycle( executeLifecycle ); + mojoDescriptor.setDependencyResolutionRequired( requiresDependencyResolution ); + mojoDescriptor.setDependencyCollectionRequired( requiresDependencyCollection ); + mojoDescriptor.setDirectInvocationOnly( requiresDirectInvocation ); + mojoDescriptor.setProjectRequired( requiresProject ); + mojoDescriptor.setRequiresReports( requiresReports ); + mojoDescriptor.setOnlineRequired( requiresOnline ); + mojoDescriptor.setAggregator( aggregator ); + mojoDescriptor.setInheritedByDefault( inheritedByDefault ); + mojoDescriptor.setThreadSafe( threadSafe ); + mojoDescriptor.setV4Api( v4Api ); + mojoDescriptor.setInstantiationStrategy( instantiationStrategy ); + mojoDescriptor.setExecutionStrategy( executionStrategy ); + mojoDescriptor.setSince( since ); + mojoDescriptor.setDeprecated( deprecated ); + mojoDescriptor.setComponentConfigurator( configurator ); + mojoDescriptor.setComponentComposer( composer ); + if ( parameters != null ) + { + mojoDescriptor.setParameters( parameters ); + } + if ( configuration != null ) + { + mojoDescriptor.setMojoConfiguration( new Xpp3Dom( configuration ) ); + } + if ( requirements != null ) + { + mojoDescriptor.setRequirements( requirements ); + } + return mojoDescriptor; + + } + } + } diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java deleted file mode 100644 index 14fd2f0ba9f0..000000000000 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java +++ /dev/null @@ -1,207 +0,0 @@ -package org.apache.maven.plugin.descriptor; - -/* - * 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. - */ - -/** - * @author Jason van Zyl - */ -public class Parameter - implements Cloneable -{ - private String alias; - - private String name; - - private String type; - - private boolean required; - - private boolean editable = true; - - private String description; - - private String expression; - - private String deprecated; - - private String defaultValue; - - private String implementation; - - private Requirement requirement; - - private String since; - - // ---------------------------------------------------------------------- - // - // ---------------------------------------------------------------------- - - 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 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 getDeprecated() - { - return deprecated; - } - - public void setDeprecated( String deprecated ) - { - this.deprecated = deprecated; - } - - public int hashCode() - { - return name.hashCode(); - } - - public boolean equals( Object other ) - { - return ( other instanceof Parameter ) && getName().equals( ( (Parameter) other ).getName() ); - } - - public String getAlias() - { - return alias; - } - - public void setAlias( String alias ) - { - this.alias = alias; - } - - public boolean isEditable() - { - return editable; - } - - public void setEditable( boolean editable ) - { - this.editable = editable; - } - - public void setDefaultValue( String defaultValue ) - { - this.defaultValue = defaultValue; - } - - public String getDefaultValue() - { - return defaultValue; - } - - public String toString() - { - return "Mojo parameter [name: '" + getName() + "'; alias: '" + getAlias() + "']"; - } - - public Requirement getRequirement() - { - return requirement; - } - - public void setRequirement( Requirement requirement ) - { - this.requirement = requirement; - } - - public String getImplementation() - { - return implementation; - } - - public void setImplementation( String implementation ) - { - this.implementation = implementation; - } - - public String getSince() - { - return since; - } - - public void setSince( String since ) - { - this.since = since; - } - - /** - * Creates a shallow copy of this parameter. - */ - @Override - public Parameter clone() - { - try - { - return (Parameter) super.clone(); - } - catch ( CloneNotSupportedException e ) - { - throw new UnsupportedOperationException( e ); - } - } - -} diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java index 8c799419c3c3..ccc2b9f0ae74 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java @@ -19,6 +19,9 @@ * under the License. */ + +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.NotThreadSafe; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.model.Plugin; @@ -26,6 +29,7 @@ import org.apache.maven.plugin.lifecycle.LifecycleConfiguration; import org.apache.maven.plugin.lifecycle.io.xpp3.LifecycleMappingsXpp3Reader; import org.codehaus.plexus.classworlds.realm.ClassRealm; +import org.codehaus.plexus.component.repository.ComponentDependency; import org.codehaus.plexus.component.repository.ComponentSetDescriptor; import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -37,13 +41,18 @@ import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /** + * This file is not generated from the underlying Modello model as it needs to extend {@link ComponentSetDescriptor} + * which is not based on a model. + * * @author Jason van Zyl */ public class PluginDescriptor @@ -88,6 +97,7 @@ public class PluginDescriptor private Map lifecycleMappings; + private List dependencies; // ---------------------------------------------------------------------- // // ---------------------------------------------------------------------- @@ -448,7 +458,7 @@ public PluginDescriptor clone() } } - public void addMojos( List mojos ) + public void setMojos( Collection mojos ) throws DuplicateMojoDescriptorException { for ( MojoDescriptor mojoDescriptor : mojos ) @@ -458,4 +468,208 @@ public void addMojos( List mojos ) } + + public void setModelEncoding( String inputEncoding ) + { + // TODO Auto-generated method stub + + } + + public void setDependencies2( Collection dependencies2 ) + { + dependencies2.stream().map( Dependency::toComponentDependency ).forEach( this::addDependency ); + } + + public final List getDependencies2() + { + return dependencies.stream().map( Dependency::toComponentDependency ).collect( Collectors.toList() ); + } + + + /** + * Creates a new PluginDescriptor instance. + * Equivalent to {@code newInstance( true )}. + * @throws DuplicateMojoDescriptorException + * @see #newInstance(boolean) + */ + @Nonnull + public static PluginDescriptor newInstance() throws DuplicateMojoDescriptorException + { + return newInstance( true ); + } + + /** + * Creates a new PluginDescriptor instance using default values or not. + * Equivalent to {@code newBuilder( withDefaults ).build()}. + * @throws DuplicateMojoDescriptorException + */ + @Nonnull + public static PluginDescriptor newInstance( boolean withDefaults ) throws DuplicateMojoDescriptorException + { + return newBuilder( withDefaults ).build(); + } + + /** + * Creates a new PluginDescriptor builder instance. + * Equivalent to {@code newBuilder( true )}. + * @see #newBuilder(boolean) + */ + @Nonnull + public static Builder newBuilder() + { + return newBuilder( true ); + } + + /** + * Creates a new PluginDescriptor builder instance using default values or not. + */ + @Nonnull + public static Builder newBuilder( boolean withDefaults ) + { + return new Builder( withDefaults ); + } + + + /** + * Builder class used to create PluginDescriptor instances. + * @see #with() + * @see #newBuilder() + */ + @NotThreadSafe + public static class Builder + { + String modelEncoding; + String name; + String description; + String groupId; + String artifactId; + String version; + String goalPrefix; + Boolean isolatedRealm; + Boolean inheritedByDefault; + String requiredJavaVersion; + String requiredMavenVersion; + Collection mojos; + Collection dependencies; + + Builder( boolean withDefaults ) + { + if ( withDefaults ) + { + this.isolatedRealm = false; + this.inheritedByDefault = true; + } + } + + @Nonnull + public Builder modelEncoding( String modelEncoding ) + { + this.modelEncoding = modelEncoding; + return this; + } + + @Nonnull + public Builder name( String name ) + { + this.name = name; + return this; + } + + @Nonnull + public Builder description( String description ) + { + this.description = description; + return this; + } + + @Nonnull + public Builder groupId( String groupId ) + { + this.groupId = groupId; + return this; + } + + @Nonnull + public Builder artifactId( String artifactId ) + { + this.artifactId = artifactId; + return this; + } + + @Nonnull + public Builder version( String version ) + { + this.version = version; + return this; + } + + @Nonnull + public Builder goalPrefix( String goalPrefix ) + { + this.goalPrefix = goalPrefix; + return this; + } + + @Nonnull + public Builder isolatedRealm( boolean isolatedRealm ) + { + this.isolatedRealm = isolatedRealm; + return this; + } + + @Nonnull + public Builder inheritedByDefault( boolean inheritedByDefault ) + { + this.inheritedByDefault = inheritedByDefault; + return this; + } + + @Nonnull + public Builder requiredJavaVersion( String requiredJavaVersion ) + { + this.requiredJavaVersion = requiredJavaVersion; + return this; + } + + @Nonnull + public Builder requiredMavenVersion( String requiredMavenVersion ) + { + this.requiredMavenVersion = requiredMavenVersion; + return this; + } + + @Nonnull + public Builder mojos( Collection mojos ) + { + this.mojos = mojos; + return this; + } + + @Nonnull + public Builder dependencies( Collection dependencies2 ) + { + this.dependencies = dependencies2; + return this; + } + + + @Nonnull + public PluginDescriptor build() throws DuplicateMojoDescriptorException + { + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + pluginDescriptor.setName( name ); + pluginDescriptor.setDescription( description ); + pluginDescriptor.setGroupId( groupId ); + pluginDescriptor.setArtifactId( artifactId ); + pluginDescriptor.setVersion( version ); + pluginDescriptor.setGoalPrefix( goalPrefix ); + pluginDescriptor.setIsolatedRealm( isolatedRealm ); + pluginDescriptor.setInheritedByDefault( inheritedByDefault ); + pluginDescriptor.setRequiredJavaVersion( requiredJavaVersion ); + pluginDescriptor.setRequiredMavenVersion( requiredMavenVersion ); + pluginDescriptor.setMojos( mojos ); + pluginDescriptor.setDependencies2( dependencies ); + return pluginDescriptor; + } + } } diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java index 98021fa0c922..a960ab192952 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java @@ -21,21 +21,16 @@ import java.io.IOException; import java.io.Reader; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import org.apache.maven.internal.xml.XmlPlexusConfiguration; -import org.apache.maven.internal.xml.Xpp3DomBuilder; -import org.codehaus.plexus.component.repository.ComponentDependency; -import org.codehaus.plexus.component.repository.ComponentRequirement; -import org.codehaus.plexus.configuration.PlexusConfiguration; + +import org.apache.maven.plugin.descriptor.io.xpp3.PluginDescriptorXpp3Reader; import org.codehaus.plexus.configuration.PlexusConfigurationException; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** + * @deprecated Use {@link PluginDescriptorXpp3Reader} instead. * @author Jason van Zyl */ +@Deprecated public class PluginDescriptorBuilder { public PluginDescriptor build( Reader reader ) @@ -47,140 +42,17 @@ public PluginDescriptor build( Reader reader ) public PluginDescriptor build( Reader reader, String source ) throws PlexusConfigurationException { - return build( source, buildConfiguration( reader ) ); - } - - private PluginDescriptor build( String source, PlexusConfiguration c ) - throws PlexusConfigurationException - { - PluginDescriptor pluginDescriptor = new PluginDescriptor(); - - pluginDescriptor.setSource( source ); - pluginDescriptor.setGroupId( extractGroupId( c ) ); - pluginDescriptor.setArtifactId( extractArtifactId( c ) ); - pluginDescriptor.setVersion( extractVersion( c ) ); - pluginDescriptor.setGoalPrefix( extractGoalPrefix( c ) ); - - pluginDescriptor.setName( extractName( c ) ); - pluginDescriptor.setDescription( extractDescription( c ) ); - - pluginDescriptor.setIsolatedRealm( extractIsolatedRealm( c ) ); - pluginDescriptor.setInheritedByDefault( extractInheritedByDefault( c ) ); - pluginDescriptor.setRequiredJavaVersion( extractRequiredJavaVersion( c ).orElse( null ) ); - pluginDescriptor.setRequiredMavenVersion( extractRequiredMavenVersion( c ).orElse( null ) ); - - pluginDescriptor.addMojos( extractMojos( c, pluginDescriptor ) ); - - pluginDescriptor.setDependencies( extractComponentDependencies( c ) ); - - return pluginDescriptor; - } - - private String extractGroupId( PlexusConfiguration c ) - { - return c.getChild( "groupId" ).getValue(); - } - - private String extractArtifactId( PlexusConfiguration c ) - { - return c.getChild( "artifactId" ).getValue(); - } - - private String extractVersion( PlexusConfiguration c ) - { - return c.getChild( "version" ).getValue(); - } - - private String extractGoalPrefix( PlexusConfiguration c ) - { - return c.getChild( "goalPrefix" ).getValue(); - } - - private String extractName( PlexusConfiguration c ) - { - return c.getChild( "name" ).getValue(); - } - - private String extractDescription( PlexusConfiguration c ) - { - return c.getChild( "description" ).getValue(); - } - - private List extractMojos( PlexusConfiguration c, PluginDescriptor pluginDescriptor ) - throws PlexusConfigurationException - { - List mojos = new ArrayList<>(); - - PlexusConfiguration[] mojoConfigurations = c.getChild( "mojos" ).getChildren( "mojo" ); - - for ( PlexusConfiguration component : mojoConfigurations ) - { - mojos.add( buildComponentDescriptor( component, pluginDescriptor ) ); - - } - return mojos; - } - - private boolean extractInheritedByDefault( PlexusConfiguration c ) - { - String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue(); - - if ( inheritedByDefault != null ) - { - return Boolean.parseBoolean( inheritedByDefault ); - } - return false; - } - - private boolean extractIsolatedRealm( PlexusConfiguration c ) - { - String isolatedRealm = c.getChild( "isolatedRealm" ).getValue(); - - if ( isolatedRealm != null ) + try { - return Boolean.parseBoolean( isolatedRealm ); + return new PluginDescriptorXpp3Reader().read( reader, false ); } - return false; - } - - private Optional extractRequiredJavaVersion( PlexusConfiguration c ) - { - return Optional.ofNullable( c.getChild( "requiredJavaVersion" ) ).map( PlexusConfiguration::getValue ); - } - - private Optional extractRequiredMavenVersion( PlexusConfiguration c ) - { - return Optional.ofNullable( c.getChild( "requiredMavenVersion" ) ).map( PlexusConfiguration::getValue ); - } - - private List extractComponentDependencies( PlexusConfiguration c ) - { - - PlexusConfiguration[] dependencyConfigurations = c.getChild( "dependencies" ).getChildren( "dependency" ); - - List dependencies = new ArrayList<>(); - - for ( PlexusConfiguration d : dependencyConfigurations ) + catch ( IOException | XmlPullParserException e ) { - dependencies.add( extractComponentDependency( d ) ); + throw new PlexusConfigurationException( "Error reading plugin descriptor", e ); } - return dependencies; - } - - private ComponentDependency extractComponentDependency( PlexusConfiguration d ) - { - ComponentDependency cd = new ComponentDependency(); - - cd.setArtifactId( extractArtifactId( d ) ); - - cd.setGroupId( extractGroupId( d ) ); - - cd.setType( d.getChild( "type" ).getValue() ); - - cd.setVersion( extractVersion( d ) ); - return cd; } +/* @SuppressWarnings( "checkstyle:methodlength" ) public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor ) throws PlexusConfigurationException @@ -427,5 +299,5 @@ public PlexusConfiguration buildConfiguration( Reader configuration ) { throw new PlexusConfigurationException( e.getMessage(), e ); } - } + }*/ } diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Requirement.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Requirement.java deleted file mode 100644 index 1526b039eab4..000000000000 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Requirement.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.apache.maven.plugin.descriptor; - -/* - * 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. - */ - -/** - * Describes a component requirement. - * - * @author Brett Porter - */ -public class Requirement - implements Cloneable -{ - private final String role; - - private final String roleHint; - - public Requirement( String role ) - { - this.role = role; - this.roleHint = null; - } - - public Requirement( String role, String roleHint ) - { - this.role = role; - this.roleHint = roleHint; - } - - public String getRole() - { - return role; - } - - public String getRoleHint() - { - return roleHint; - } - - /** - * Creates a shallow copy of this requirement. - */ - @Override - public Requirement clone() - { - try - { - return (Requirement) super.clone(); - } - catch ( CloneNotSupportedException e ) - { - throw new UnsupportedOperationException( e ); - } - } - -} diff --git a/maven-plugin-api/src/main/mdo/plugin.mdo b/maven-plugin-api/src/main/mdo/plugin.mdo index ddb2fd62a301..392f1a35d5ad 100644 --- a/maven-plugin-api/src/main/mdo/plugin.mdo +++ b/maven-plugin-api/src/main/mdo/plugin.mdo @@ -34,7 +34,7 @@ under the License. package - plugin descriptor XML documentation (no java generation) + org.apache.maven.plugin.descriptor @@ -370,7 +370,7 @@ under the License. Parameter 1.0.0+ - A phase mapping definition. + A parameter definition. @@ -455,6 +455,37 @@ under the License. ]]> + + + 1.1.0+ + Add setters/getters for expression and default value coming from the mojo's configuration + + + + @@ -512,6 +543,26 @@ under the License. The field name which has this requirement. + + + 1.1.0+ + Conversion to Plexus Component Requirement + + + + @@ -547,6 +598,27 @@ under the License. The type of dependency. + + + 1.1.0+ + Conversion to Plexus Component Dependency + + + + diff --git a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java index 0aec7d372222..4f2c94550dcb 100644 --- a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java +++ b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java @@ -32,18 +32,14 @@ public class MojoDescriptorTest public void getParameterMap() throws DuplicateParameterException { MojoDescriptor mojoDescriptor = new MojoDescriptor(); - Parameter param1 = new Parameter(); - param1.setName( "param1" ); - param1.setDefaultValue( "value1" ); + Parameter param1 = Parameter.newBuilder().name( "param1" ).build(); mojoDescriptor.addParameter( param1 ); assertEquals( 1, mojoDescriptor.getParameters().size() ); assertEquals( mojoDescriptor.getParameters().size(), mojoDescriptor.getParameterMap().size() ); - Parameter param2 = new Parameter(); - param2.setName( "param2" ); - param2.setDefaultValue( "value2" ); + Parameter param2 = Parameter.newBuilder().name( "param2" ).build(); mojoDescriptor.addParameter( param2 ); assertEquals( 2, mojoDescriptor.getParameters().size() ); diff --git a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilderTest.java b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilderTest.java index 88d249069c9c..17fe8b67d40f 100644 --- a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilderTest.java +++ b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilderTest.java @@ -131,7 +131,7 @@ public void testBuildReader() md = pd.getMojos().get( 1 ); assertEquals( "war", md.getGoal() ); - assertNull( md.getDependencyResolutionRequired() ); + assertEquals( "runtime", md.getDependencyResolutionRequired() ); // default assertNull( md.getDependencyCollectionRequired() ); assertTrue( md.isThreadSafe() ); } diff --git a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorXpp3ReaderTest.java b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorXpp3ReaderTest.java new file mode 100644 index 000000000000..a840b2b4b5d9 --- /dev/null +++ b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorXpp3ReaderTest.java @@ -0,0 +1,142 @@ +package org.apache.maven.plugin.descriptor; + +/* + * 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 java.io.IOException; +import java.io.InputStream; +import java.util.Objects; + +import org.codehaus.plexus.component.repository.ComponentDependency; +import org.codehaus.plexus.component.repository.ComponentRequirement; +import org.codehaus.plexus.configuration.PlexusConfiguration; +import org.codehaus.plexus.configuration.PlexusConfigurationException; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests {@link PluginDescriptorBuilder}. + * + * @author Benjamin Bentmann + */ +public class PluginDescriptorXpp3ReaderTest +{ + + private PluginDescriptor build( String resource ) + throws IOException, PlexusConfigurationException, XmlPullParserException + { + try (InputStream input = Objects.requireNonNull( getClass().getResourceAsStream( resource ) ) ) + { + // must not be strict in order to disregard no longer supported elements + return new org.apache.maven.plugin.descriptor.io.xpp3.PluginDescriptorXpp3Reader().read( input, false ); + } + } + + @Test + public void testBuildReader() + throws Exception + { + PluginDescriptor pd = build( "/plugin.xml" ); + + assertEquals( "org.apache.maven.plugins", pd.getGroupId() ); + assertEquals( "maven-jar-plugin", pd.getArtifactId() ); + assertEquals( "2.3-SNAPSHOT", pd.getVersion() ); + assertEquals( "jar", pd.getGoalPrefix() ); + assertEquals( "plugin-description", pd.getDescription() ); + assertFalse( pd.isIsolatedRealm() ); + assertTrue( pd.isInheritedByDefault() ); + assertEquals( 2, pd.getMojos().size() ); + assertEquals( 1, pd.getDependencies().size() ); + + MojoDescriptor md = pd.getMojos().get( 0 ); + + assertEquals( "jar", md.getGoal() ); + assertEquals( "mojo-description", md.getDescription() ); + assertEquals( "runtime", md.getDependencyResolutionRequired() ); + assertEquals( "test", md.getDependencyCollectionRequired() ); + assertFalse( md.isAggregator() ); + assertFalse( md.isDirectInvocationOnly() ); + assertTrue( md.isInheritedByDefault() ); + assertFalse( md.isOnlineRequired() ); + assertTrue( md.isProjectRequired() ); + assertFalse( md.isThreadSafe() ); + assertEquals( "package", md.getPhase() ); + assertEquals( "org.apache.maven.plugin.jar.JarMojo", md.getImplementation() ); + assertEquals( "antrun", md.getComponentConfigurator() ); + assertEquals( "java", md.getLanguage() ); + assertEquals( "per-lookup", md.getInstantiationStrategy() ); + assertEquals( "some-goal", md.getExecuteGoal() ); + assertEquals( "generate-sources", md.getExecutePhase() ); + assertEquals( "cobertura", md.getExecuteLifecycle() ); + assertEquals( "2.2", md.getSince() ); + assertEquals( "deprecated-mojo", md.getDeprecated() ); + assertEquals( 1, md.getRequirements().size() ); + assertEquals( 1, md.getParameters().size() ); + + assertNotNull( md.getMojoConfiguration() ); + assertEquals( 1, md.getMojoConfiguration().getChildCount() ); + + PlexusConfiguration pc = md.getMojoConfiguration().getChild( 0 ); + + assertEquals( "${jar.finalName}", pc.getValue() ); + assertEquals( "${project.build.finalName}", pc.getAttribute( "default-value" ) ); + assertEquals( "java.lang.String", pc.getAttribute( "implementation" ) ); + + Parameter mp = md.getParameters().get( 0 ); + + assertEquals( "finalName", mp.getName() ); + assertEquals( "jarName", mp.getAlias() ); + assertEquals( "java.lang.String", mp.getType() ); + assertEquals( "java.lang.String", mp.getImplementation() ); + assertTrue( mp.isEditable() ); + assertFalse( mp.isRequired() ); + assertEquals( "parameter-description", mp.getDescription() ); + assertEquals( "deprecated-parameter", mp.getDeprecated() ); + assertEquals( "${jar.finalName}", mp.getExpression() ); + assertEquals( "${project.build.finalName}", mp.getDefaultValue() ); + assertEquals( "3.0.0", mp.getSince() ); + + ComponentRequirement cr = md.getRequirements().get( 0 ); + + assertEquals( "org.codehaus.plexus.archiver.Archiver", cr.getRole() ); + assertEquals( "jar", cr.getRoleHint() ); + assertEquals( "jarArchiver", cr.getFieldName() ); + + ComponentDependency cd = pd.getDependencies().get( 0 ); + + assertEquals( "org.apache.maven", cd.getGroupId() ); + assertEquals( "maven-plugin-api", cd.getArtifactId() ); + assertEquals( "2.0.6", cd.getVersion() ); + assertEquals( "jar", cd.getType() ); + + md = pd.getMojos().get( 1 ); + + assertEquals( "war", md.getGoal() ); + assertEquals( "runtime", md.getDependencyResolutionRequired() ); // default + assertNull( md.getDependencyCollectionRequired() ); + assertTrue( md.isThreadSafe() ); + } + +} From 4c718595740814bd1f3dc214e7b93a9011892c2f Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Wed, 9 Nov 2022 11:57:58 +0100 Subject: [PATCH 2/2] generate immutable descriptor in new package --- maven-plugin-api/pom.xml | 31 +- .../{lifecycle => }/ImmutableCollections.java | 6 +- .../InvalidPluginDescriptorException.java | 7 +- .../plugin/descriptor/MojoDescriptor.java | 377 +----------------- .../maven/plugin/descriptor/Parameter.java | 207 ++++++++++ .../plugin/descriptor/PluginDescriptor.java | 217 +--------- .../descriptor/PluginDescriptorBuilder.java | 147 ++++++- .../maven/plugin/descriptor/Requirement.java | 72 ++++ maven-plugin-api/src/main/mdo/model.vm | 1 + .../plugin/descriptor/MojoDescriptorTest.java | 49 --- .../PluginDescriptorBuilderTest.java | 2 +- .../PluginDescriptorXpp3ReaderTest.java | 142 ------- 12 files changed, 435 insertions(+), 823 deletions(-) rename maven-plugin-api/src/main/java/org/apache/maven/plugin/{lifecycle => }/ImmutableCollections.java (99%) create mode 100644 maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java create mode 100644 maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Requirement.java delete mode 100644 maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java delete mode 100644 maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorXpp3ReaderTest.java diff --git a/maven-plugin-api/pom.xml b/maven-plugin-api/pom.xml index f2b7b3a9c697..dffcf9d4dec4 100644 --- a/maven-plugin-api/pom.xml +++ b/maven-plugin-api/pom.xml @@ -133,39 +133,14 @@ under the License. - packageModelV3=org.apache.maven.plugin.descriptor - packageModelV4=org.apache.maven.plugin.descriptor - packageToolV4=org.apache.maven.plugin.descriptor.io.xpp3 + packageModelV3=org.apache.maven.plugin.immutabledescriptor + packageModelV4=org.apache.maven.plugin.immutabledescriptor + packageToolV4=org.apache.maven.plugin.immutabledescriptor.io.xpp3 - - maven-clean-plugin - - - remove-superfluous-generated-classes - generate-sources - - clean - - - true - - - target/generated-sources/modello/org/apache/maven/plugin/descriptor - - - MojoDescriptor.java - PluginDescriptor.java - - - - - - - diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/lifecycle/ImmutableCollections.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/ImmutableCollections.java similarity index 99% rename from maven-plugin-api/src/main/java/org/apache/maven/plugin/lifecycle/ImmutableCollections.java rename to maven-plugin-api/src/main/java/org/apache/maven/plugin/ImmutableCollections.java index 875b0b22fa27..4ed841bcd725 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/lifecycle/ImmutableCollections.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/ImmutableCollections.java @@ -1,4 +1,4 @@ -package org.apache.maven.plugin.lifecycle; +package org.apache.maven.plugin; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -38,7 +38,7 @@ import java.util.function.Predicate; import java.util.function.UnaryOperator; -class ImmutableCollections +public class ImmutableCollections { private static final List EMPTY_LIST = new AbstractImmutableList() @@ -88,7 +88,7 @@ public int size() } }; - static List copy( Collection collection ) + public static List copy( Collection collection ) { if ( collection == null ) { diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/InvalidPluginDescriptorException.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/InvalidPluginDescriptorException.java index 12f9655c5215..f63fd26ce469 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/InvalidPluginDescriptorException.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/InvalidPluginDescriptorException.java @@ -19,19 +19,18 @@ * under the License. */ -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.codehaus.plexus.configuration.PlexusConfigurationException; /** * InvalidPluginDescriptorException */ public class InvalidPluginDescriptorException - extends XmlPullParserException + extends PlexusConfigurationException { public InvalidPluginDescriptorException( String message, Throwable cause ) { - super( message ); - this.initCause( cause ); + super( message, cause ); } public InvalidPluginDescriptorException( String message ) diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java index ea6c3d40d3cd..24e1ff3d15f4 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/MojoDescriptor.java @@ -20,20 +20,15 @@ */ import java.util.ArrayList; -import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import org.apache.maven.api.annotations.Nonnull; -import org.apache.maven.api.annotations.NotThreadSafe; -import org.apache.maven.api.xml.Dom; import org.apache.maven.plugin.Mojo; import org.codehaus.plexus.component.repository.ComponentDescriptor; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; -import org.codehaus.plexus.util.xml.Xpp3Dom; /** * The bean containing the Mojo descriptor.
@@ -41,9 +36,10 @@ * * https://maven.apache.org/developers/mojo-api-specification.html * - * This file is not generated from the underlying Modello model as it needs to extend {@link ComponentDescriptor} - * which is not based on a model. + * TODO is there a need for the delegation of MavenMojoDescriptor to this? + * Why not just extend ComponentDescriptor here? */ +@Deprecated public class MojoDescriptor extends ComponentDescriptor implements Cloneable @@ -205,13 +201,12 @@ public List getParameters() * @param parameters the new list of parameters * @throws DuplicateParameterException if any */ - public void setParameters( Collection parameters ) + public void setParameters( List parameters ) throws DuplicateParameterException { this.parameters.clear(); for ( Parameter parameter : parameters ) { - // enrich parameter with information from configuration (already available?) addParameter( parameter ); } } @@ -229,24 +224,10 @@ public void addParameter( Parameter parameter ) + " has been declared multiple times in mojo with goal: " + getGoal() + " (implementation: " + getImplementation() + ")" ); } - if ( mojoConfiguration != null ) - { - setParameterValuesFromMojoConfiguration( parameter ); - } + parameters.add( parameter ); } - private boolean setParameterValuesFromMojoConfiguration( Parameter parameter ) - { - PlexusConfiguration paramConfig = mojoConfiguration.getChild( parameter.getName(), false ); - if ( paramConfig != null ) - { - parameter.setExpression( paramConfig.getValue( null ) ); - parameter.setDefaultValue( paramConfig.getAttribute( "default-value" ) ); - return true; - } - return false; - } /** * @return the list parameters as a Map (keyed by {@link Parameter#getName()}) that is built from * {@link #parameters} list on each call. In other words, the map returned is built on fly and is a copy. @@ -472,10 +453,6 @@ public PlexusConfiguration getMojoConfiguration() public void setMojoConfiguration( PlexusConfiguration mojoConfiguration ) { this.mojoConfiguration = mojoConfiguration; - for ( Parameter parameter : parameters ) - { - setParameterValuesFromMojoConfiguration( parameter ); - } } /** {@inheritDoc} */ @@ -692,17 +669,6 @@ public void setV4Api( boolean v4Api ) this.v4Api = v4Api; } - public final void setMojoConfiguration( final Xpp3Dom configuration ) - { - setMojoConfiguration( new XmlPlexusConfiguration( configuration ) ); - } - - - public void setRequirements( Collection requirements ) - { - requirements.stream().map( Requirement::toComponentRequirement ).forEach( this::addRequirement ); - } - /** * Creates a shallow copy of this mojo descriptor. */ @@ -719,337 +685,4 @@ public MojoDescriptor clone() } } - - /** - * Creates a new MojoDescriptor instance. - * Equivalent to {@code newInstance( true )}. - * @throws DuplicateParameterException - * @see #newInstance(boolean) - */ - @Nonnull - public static MojoDescriptor newInstance() throws DuplicateParameterException - { - return newInstance( true ); - } - - /** - * Creates a new MojoDescriptor instance using default values or not. - * Equivalent to {@code newBuilder( withDefaults ).build()}. - * @throws DuplicateParameterException - */ - @Nonnull - public static MojoDescriptor newInstance( boolean withDefaults ) throws DuplicateParameterException - { - return newBuilder( withDefaults ).build(); - } - - /** - * Creates a new MojoDescriptor builder instance. - * Equivalent to {@code newBuilder( true )}. - * @see #newBuilder(boolean) - */ - @Nonnull - public static Builder newBuilder() - { - return newBuilder( true ); - } - - /** - * Creates a new MojoDescriptor builder instance using default values or not. - */ - @Nonnull - public static Builder newBuilder( boolean withDefaults ) - { - return new Builder( withDefaults ); - } - - /** - * Builder class used to create MojoDescriptor instances. - * @see #with() - * @see #newBuilder() - */ - @NotThreadSafe - public static class Builder - { - String goal; - String description; - String implementation; - String language; - String phase; - String executePhase; - String executeGoal; - String executeLifecycle; - String requiresDependencyResolution; - String requiresDependencyCollection; - Boolean requiresDirectInvocation; - Boolean requiresProject; - Boolean requiresReports; - Boolean requiresOnline; - Boolean aggregator; - Boolean inheritedByDefault; - Boolean threadSafe; - Boolean v4Api; - String instantiationStrategy; - String executionStrategy; - String since; - String deprecated; - String configurator; - String composer; - Collection parameters; - Dom configuration; - Collection requirements; - - Builder( boolean withDefaults ) - { - if ( withDefaults ) - { - this.language = "java"; - this.requiresDependencyResolution = "runtime"; - this.requiresDirectInvocation = false; - this.requiresProject = true; - this.requiresReports = false; - this.requiresOnline = false; - this.aggregator = false; - this.inheritedByDefault = true; - this.threadSafe = false; - this.v4Api = false; - this.instantiationStrategy = "per-lookup"; - this.executionStrategy = "once-per-session"; - } - } - - @Nonnull - public Builder goal( String goal ) - { - this.goal = goal; - return this; - } - - @Nonnull - public Builder description( String description ) - { - this.description = description; - return this; - } - - @Nonnull - public Builder implementation( String implementation ) - { - this.implementation = implementation; - return this; - } - - @Nonnull - public Builder language( String language ) - { - this.language = language; - return this; - } - - @Nonnull - public Builder phase( String phase ) - { - this.phase = phase; - return this; - } - - @Nonnull - public Builder executePhase( String executePhase ) - { - this.executePhase = executePhase; - return this; - } - - @Nonnull - public Builder executeGoal( String executeGoal ) - { - this.executeGoal = executeGoal; - return this; - } - - @Nonnull - public Builder executeLifecycle( String executeLifecycle ) - { - this.executeLifecycle = executeLifecycle; - return this; - } - - @Nonnull - public Builder requiresDependencyResolution( String requiresDependencyResolution ) - { - this.requiresDependencyResolution = requiresDependencyResolution; - return this; - } - - @Nonnull - public Builder requiresDependencyCollection( String requiresDependencyCollection ) - { - this.requiresDependencyCollection = requiresDependencyCollection; - return this; - } - - @Nonnull - public Builder requiresDirectInvocation( boolean requiresDirectInvocation ) - { - this.requiresDirectInvocation = requiresDirectInvocation; - return this; - } - - @Nonnull - public Builder requiresProject( boolean requiresProject ) - { - this.requiresProject = requiresProject; - return this; - } - - @Nonnull - public Builder requiresReports( boolean requiresReports ) - { - this.requiresReports = requiresReports; - return this; - } - - @Nonnull - public Builder requiresOnline( boolean requiresOnline ) - { - this.requiresOnline = requiresOnline; - return this; - } - - @Nonnull - public Builder aggregator( boolean aggregator ) - { - this.aggregator = aggregator; - return this; - } - - @Nonnull - public Builder inheritedByDefault( boolean inheritedByDefault ) - { - this.inheritedByDefault = inheritedByDefault; - return this; - } - - @Nonnull - public Builder threadSafe( boolean threadSafe ) - { - this.threadSafe = threadSafe; - return this; - } - - @Nonnull - public Builder v4Api( boolean v4Api ) - { - this.v4Api = v4Api; - return this; - } - - @Nonnull - public Builder instantiationStrategy( String instantiationStrategy ) - { - this.instantiationStrategy = instantiationStrategy; - return this; - } - - @Nonnull - public Builder executionStrategy( String executionStrategy ) - { - this.executionStrategy = executionStrategy; - return this; - } - - @Nonnull - public Builder since( String since ) - { - this.since = since; - return this; - } - - @Nonnull - public Builder deprecated( String deprecated ) - { - this.deprecated = deprecated; - return this; - } - - @Nonnull - public Builder configurator( String configurator ) - { - this.configurator = configurator; - return this; - } - - @Nonnull - public Builder composer( String composer ) - { - this.composer = composer; - return this; - } - - @Nonnull - public Builder parameters( Collection parameters ) - { - this.parameters = parameters; - return this; - } - - @Nonnull - public Builder configuration( Dom configuration ) - { - this.configuration = configuration; - return this; - } - - @Nonnull - public Builder requirements( Collection requirements ) - { - this.requirements = requirements; - return this; - } - - @Nonnull - public MojoDescriptor build() throws DuplicateParameterException - { - MojoDescriptor mojoDescriptor = new MojoDescriptor(); - mojoDescriptor.setGoal( goal ); - mojoDescriptor.setDescription( description ); - mojoDescriptor.setImplementation( implementation ); - mojoDescriptor.setLanguage( language ); - mojoDescriptor.setPhase( phase ); - mojoDescriptor.setExecutePhase( executePhase ); - mojoDescriptor.setExecuteGoal( executeGoal ); - mojoDescriptor.setExecuteLifecycle( executeLifecycle ); - mojoDescriptor.setDependencyResolutionRequired( requiresDependencyResolution ); - mojoDescriptor.setDependencyCollectionRequired( requiresDependencyCollection ); - mojoDescriptor.setDirectInvocationOnly( requiresDirectInvocation ); - mojoDescriptor.setProjectRequired( requiresProject ); - mojoDescriptor.setRequiresReports( requiresReports ); - mojoDescriptor.setOnlineRequired( requiresOnline ); - mojoDescriptor.setAggregator( aggregator ); - mojoDescriptor.setInheritedByDefault( inheritedByDefault ); - mojoDescriptor.setThreadSafe( threadSafe ); - mojoDescriptor.setV4Api( v4Api ); - mojoDescriptor.setInstantiationStrategy( instantiationStrategy ); - mojoDescriptor.setExecutionStrategy( executionStrategy ); - mojoDescriptor.setSince( since ); - mojoDescriptor.setDeprecated( deprecated ); - mojoDescriptor.setComponentConfigurator( configurator ); - mojoDescriptor.setComponentComposer( composer ); - if ( parameters != null ) - { - mojoDescriptor.setParameters( parameters ); - } - if ( configuration != null ) - { - mojoDescriptor.setMojoConfiguration( new Xpp3Dom( configuration ) ); - } - if ( requirements != null ) - { - mojoDescriptor.setRequirements( requirements ); - } - return mojoDescriptor; - - } - } - } diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java new file mode 100644 index 000000000000..14fd2f0ba9f0 --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Parameter.java @@ -0,0 +1,207 @@ +package org.apache.maven.plugin.descriptor; + +/* + * 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. + */ + +/** + * @author Jason van Zyl + */ +public class Parameter + implements Cloneable +{ + private String alias; + + private String name; + + private String type; + + private boolean required; + + private boolean editable = true; + + private String description; + + private String expression; + + private String deprecated; + + private String defaultValue; + + private String implementation; + + private Requirement requirement; + + private String since; + + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + 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 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 getDeprecated() + { + return deprecated; + } + + public void setDeprecated( String deprecated ) + { + this.deprecated = deprecated; + } + + public int hashCode() + { + return name.hashCode(); + } + + public boolean equals( Object other ) + { + return ( other instanceof Parameter ) && getName().equals( ( (Parameter) other ).getName() ); + } + + public String getAlias() + { + return alias; + } + + public void setAlias( String alias ) + { + this.alias = alias; + } + + public boolean isEditable() + { + return editable; + } + + public void setEditable( boolean editable ) + { + this.editable = editable; + } + + public void setDefaultValue( String defaultValue ) + { + this.defaultValue = defaultValue; + } + + public String getDefaultValue() + { + return defaultValue; + } + + public String toString() + { + return "Mojo parameter [name: '" + getName() + "'; alias: '" + getAlias() + "']"; + } + + public Requirement getRequirement() + { + return requirement; + } + + public void setRequirement( Requirement requirement ) + { + this.requirement = requirement; + } + + public String getImplementation() + { + return implementation; + } + + public void setImplementation( String implementation ) + { + this.implementation = implementation; + } + + public String getSince() + { + return since; + } + + public void setSince( String since ) + { + this.since = since; + } + + /** + * Creates a shallow copy of this parameter. + */ + @Override + public Parameter clone() + { + try + { + return (Parameter) super.clone(); + } + catch ( CloneNotSupportedException e ) + { + throw new UnsupportedOperationException( e ); + } + } + +} diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java index ccc2b9f0ae74..c60309a90100 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptor.java @@ -19,9 +19,6 @@ * under the License. */ - -import org.apache.maven.api.annotations.Nonnull; -import org.apache.maven.api.annotations.NotThreadSafe; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.model.Plugin; @@ -29,7 +26,6 @@ import org.apache.maven.plugin.lifecycle.LifecycleConfiguration; import org.apache.maven.plugin.lifecycle.io.xpp3.LifecycleMappingsXpp3Reader; import org.codehaus.plexus.classworlds.realm.ClassRealm; -import org.codehaus.plexus.component.repository.ComponentDependency; import org.codehaus.plexus.component.repository.ComponentSetDescriptor; import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -41,20 +37,16 @@ import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; /** - * This file is not generated from the underlying Modello model as it needs to extend {@link ComponentSetDescriptor} - * which is not based on a model. - * * @author Jason van Zyl */ +@Deprecated public class PluginDescriptor extends ComponentSetDescriptor implements Cloneable @@ -97,7 +89,6 @@ public class PluginDescriptor private Map lifecycleMappings; - private List dependencies; // ---------------------------------------------------------------------- // // ---------------------------------------------------------------------- @@ -458,7 +449,7 @@ public PluginDescriptor clone() } } - public void setMojos( Collection mojos ) + public void addMojos( List mojos ) throws DuplicateMojoDescriptorException { for ( MojoDescriptor mojoDescriptor : mojos ) @@ -468,208 +459,4 @@ public void setMojos( Collection mojos ) } - - public void setModelEncoding( String inputEncoding ) - { - // TODO Auto-generated method stub - - } - - public void setDependencies2( Collection dependencies2 ) - { - dependencies2.stream().map( Dependency::toComponentDependency ).forEach( this::addDependency ); - } - - public final List getDependencies2() - { - return dependencies.stream().map( Dependency::toComponentDependency ).collect( Collectors.toList() ); - } - - - /** - * Creates a new PluginDescriptor instance. - * Equivalent to {@code newInstance( true )}. - * @throws DuplicateMojoDescriptorException - * @see #newInstance(boolean) - */ - @Nonnull - public static PluginDescriptor newInstance() throws DuplicateMojoDescriptorException - { - return newInstance( true ); - } - - /** - * Creates a new PluginDescriptor instance using default values or not. - * Equivalent to {@code newBuilder( withDefaults ).build()}. - * @throws DuplicateMojoDescriptorException - */ - @Nonnull - public static PluginDescriptor newInstance( boolean withDefaults ) throws DuplicateMojoDescriptorException - { - return newBuilder( withDefaults ).build(); - } - - /** - * Creates a new PluginDescriptor builder instance. - * Equivalent to {@code newBuilder( true )}. - * @see #newBuilder(boolean) - */ - @Nonnull - public static Builder newBuilder() - { - return newBuilder( true ); - } - - /** - * Creates a new PluginDescriptor builder instance using default values or not. - */ - @Nonnull - public static Builder newBuilder( boolean withDefaults ) - { - return new Builder( withDefaults ); - } - - - /** - * Builder class used to create PluginDescriptor instances. - * @see #with() - * @see #newBuilder() - */ - @NotThreadSafe - public static class Builder - { - String modelEncoding; - String name; - String description; - String groupId; - String artifactId; - String version; - String goalPrefix; - Boolean isolatedRealm; - Boolean inheritedByDefault; - String requiredJavaVersion; - String requiredMavenVersion; - Collection mojos; - Collection dependencies; - - Builder( boolean withDefaults ) - { - if ( withDefaults ) - { - this.isolatedRealm = false; - this.inheritedByDefault = true; - } - } - - @Nonnull - public Builder modelEncoding( String modelEncoding ) - { - this.modelEncoding = modelEncoding; - return this; - } - - @Nonnull - public Builder name( String name ) - { - this.name = name; - return this; - } - - @Nonnull - public Builder description( String description ) - { - this.description = description; - return this; - } - - @Nonnull - public Builder groupId( String groupId ) - { - this.groupId = groupId; - return this; - } - - @Nonnull - public Builder artifactId( String artifactId ) - { - this.artifactId = artifactId; - return this; - } - - @Nonnull - public Builder version( String version ) - { - this.version = version; - return this; - } - - @Nonnull - public Builder goalPrefix( String goalPrefix ) - { - this.goalPrefix = goalPrefix; - return this; - } - - @Nonnull - public Builder isolatedRealm( boolean isolatedRealm ) - { - this.isolatedRealm = isolatedRealm; - return this; - } - - @Nonnull - public Builder inheritedByDefault( boolean inheritedByDefault ) - { - this.inheritedByDefault = inheritedByDefault; - return this; - } - - @Nonnull - public Builder requiredJavaVersion( String requiredJavaVersion ) - { - this.requiredJavaVersion = requiredJavaVersion; - return this; - } - - @Nonnull - public Builder requiredMavenVersion( String requiredMavenVersion ) - { - this.requiredMavenVersion = requiredMavenVersion; - return this; - } - - @Nonnull - public Builder mojos( Collection mojos ) - { - this.mojos = mojos; - return this; - } - - @Nonnull - public Builder dependencies( Collection dependencies2 ) - { - this.dependencies = dependencies2; - return this; - } - - - @Nonnull - public PluginDescriptor build() throws DuplicateMojoDescriptorException - { - PluginDescriptor pluginDescriptor = new PluginDescriptor(); - pluginDescriptor.setName( name ); - pluginDescriptor.setDescription( description ); - pluginDescriptor.setGroupId( groupId ); - pluginDescriptor.setArtifactId( artifactId ); - pluginDescriptor.setVersion( version ); - pluginDescriptor.setGoalPrefix( goalPrefix ); - pluginDescriptor.setIsolatedRealm( isolatedRealm ); - pluginDescriptor.setInheritedByDefault( inheritedByDefault ); - pluginDescriptor.setRequiredJavaVersion( requiredJavaVersion ); - pluginDescriptor.setRequiredMavenVersion( requiredMavenVersion ); - pluginDescriptor.setMojos( mojos ); - pluginDescriptor.setDependencies2( dependencies ); - return pluginDescriptor; - } - } } diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java index a960ab192952..460c329b4681 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilder.java @@ -21,13 +21,19 @@ import java.io.IOException; import java.io.Reader; - -import org.apache.maven.plugin.descriptor.io.xpp3.PluginDescriptorXpp3Reader; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.apache.maven.internal.xml.XmlPlexusConfiguration; +import org.apache.maven.internal.xml.Xpp3DomBuilder; +import org.codehaus.plexus.component.repository.ComponentDependency; +import org.codehaus.plexus.component.repository.ComponentRequirement; +import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** - * @deprecated Use {@link PluginDescriptorXpp3Reader} instead. * @author Jason van Zyl */ @Deprecated @@ -42,17 +48,140 @@ public PluginDescriptor build( Reader reader ) public PluginDescriptor build( Reader reader, String source ) throws PlexusConfigurationException { - try + return build( source, buildConfiguration( reader ) ); + } + + private PluginDescriptor build( String source, PlexusConfiguration c ) + throws PlexusConfigurationException + { + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + + pluginDescriptor.setSource( source ); + pluginDescriptor.setGroupId( extractGroupId( c ) ); + pluginDescriptor.setArtifactId( extractArtifactId( c ) ); + pluginDescriptor.setVersion( extractVersion( c ) ); + pluginDescriptor.setGoalPrefix( extractGoalPrefix( c ) ); + + pluginDescriptor.setName( extractName( c ) ); + pluginDescriptor.setDescription( extractDescription( c ) ); + + pluginDescriptor.setIsolatedRealm( extractIsolatedRealm( c ) ); + pluginDescriptor.setInheritedByDefault( extractInheritedByDefault( c ) ); + pluginDescriptor.setRequiredJavaVersion( extractRequiredJavaVersion( c ).orElse( null ) ); + pluginDescriptor.setRequiredMavenVersion( extractRequiredMavenVersion( c ).orElse( null ) ); + + pluginDescriptor.addMojos( extractMojos( c, pluginDescriptor ) ); + + pluginDescriptor.setDependencies( extractComponentDependencies( c ) ); + + return pluginDescriptor; + } + + private String extractGroupId( PlexusConfiguration c ) + { + return c.getChild( "groupId" ).getValue(); + } + + private String extractArtifactId( PlexusConfiguration c ) + { + return c.getChild( "artifactId" ).getValue(); + } + + private String extractVersion( PlexusConfiguration c ) + { + return c.getChild( "version" ).getValue(); + } + + private String extractGoalPrefix( PlexusConfiguration c ) + { + return c.getChild( "goalPrefix" ).getValue(); + } + + private String extractName( PlexusConfiguration c ) + { + return c.getChild( "name" ).getValue(); + } + + private String extractDescription( PlexusConfiguration c ) + { + return c.getChild( "description" ).getValue(); + } + + private List extractMojos( PlexusConfiguration c, PluginDescriptor pluginDescriptor ) + throws PlexusConfigurationException + { + List mojos = new ArrayList<>(); + + PlexusConfiguration[] mojoConfigurations = c.getChild( "mojos" ).getChildren( "mojo" ); + + for ( PlexusConfiguration component : mojoConfigurations ) { - return new PluginDescriptorXpp3Reader().read( reader, false ); + mojos.add( buildComponentDescriptor( component, pluginDescriptor ) ); + } - catch ( IOException | XmlPullParserException e ) + return mojos; + } + + private boolean extractInheritedByDefault( PlexusConfiguration c ) + { + String inheritedByDefault = c.getChild( "inheritedByDefault" ).getValue(); + + if ( inheritedByDefault != null ) { - throw new PlexusConfigurationException( "Error reading plugin descriptor", e ); + return Boolean.parseBoolean( inheritedByDefault ); } + return false; + } + + private boolean extractIsolatedRealm( PlexusConfiguration c ) + { + String isolatedRealm = c.getChild( "isolatedRealm" ).getValue(); + + if ( isolatedRealm != null ) + { + return Boolean.parseBoolean( isolatedRealm ); + } + return false; + } + + private Optional extractRequiredJavaVersion( PlexusConfiguration c ) + { + return Optional.ofNullable( c.getChild( "requiredJavaVersion" ) ).map( PlexusConfiguration::getValue ); + } + + private Optional extractRequiredMavenVersion( PlexusConfiguration c ) + { + return Optional.ofNullable( c.getChild( "requiredMavenVersion" ) ).map( PlexusConfiguration::getValue ); + } + + private List extractComponentDependencies( PlexusConfiguration c ) + { + + PlexusConfiguration[] dependencyConfigurations = c.getChild( "dependencies" ).getChildren( "dependency" ); + + List dependencies = new ArrayList<>(); + + for ( PlexusConfiguration d : dependencyConfigurations ) + { + dependencies.add( extractComponentDependency( d ) ); + } + return dependencies; + } + + private ComponentDependency extractComponentDependency( PlexusConfiguration d ) + { + ComponentDependency cd = new ComponentDependency(); + + cd.setArtifactId( extractArtifactId( d ) ); + + cd.setGroupId( extractGroupId( d ) ); + + cd.setType( d.getChild( "type" ).getValue() ); + + cd.setVersion( extractVersion( d ) ); + return cd; } -/* @SuppressWarnings( "checkstyle:methodlength" ) public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDescriptor pluginDescriptor ) throws PlexusConfigurationException @@ -299,5 +428,5 @@ public PlexusConfiguration buildConfiguration( Reader configuration ) { throw new PlexusConfigurationException( e.getMessage(), e ); } - }*/ + } } diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Requirement.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Requirement.java new file mode 100644 index 000000000000..1526b039eab4 --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/descriptor/Requirement.java @@ -0,0 +1,72 @@ +package org.apache.maven.plugin.descriptor; + +/* + * 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. + */ + +/** + * Describes a component requirement. + * + * @author Brett Porter + */ +public class Requirement + implements Cloneable +{ + private final String role; + + private final String roleHint; + + public Requirement( String role ) + { + this.role = role; + this.roleHint = null; + } + + public Requirement( String role, String roleHint ) + { + this.role = role; + this.roleHint = roleHint; + } + + public String getRole() + { + return role; + } + + public String getRoleHint() + { + return roleHint; + } + + /** + * Creates a shallow copy of this requirement. + */ + @Override + public Requirement clone() + { + try + { + return (Requirement) super.clone(); + } + catch ( CloneNotSupportedException e ) + { + throw new UnsupportedOperationException( e ); + } + } + +} diff --git a/maven-plugin-api/src/main/mdo/model.vm b/maven-plugin-api/src/main/mdo/model.vm index e995fa6e9478..b96d9cf71099 100644 --- a/maven-plugin-api/src/main/mdo/model.vm +++ b/maven-plugin-api/src/main/mdo/model.vm @@ -44,6 +44,7 @@ #set ( $dummy = $imports.add( "org.apache.maven.api.annotations.Nonnull" ) ) #set ( $dummy = $imports.add( "org.apache.maven.api.annotations.NotThreadSafe" ) ) #set ( $dummy = $imports.add( "org.apache.maven.api.annotations.ThreadSafe" ) ) + #set ( $dummy = $imports.add( "org.apache.maven.plugin.ImmutableCollections" ) ) #foreach ( $field in $allFields ) #if ( $field.type == "java.util.List" ) #set ( $dummy = $imports.add( "java.util.ArrayList" ) ) diff --git a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java deleted file mode 100644 index 4f2c94550dcb..000000000000 --- a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/MojoDescriptorTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.apache.maven.plugin.descriptor; - -/* - * 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.junit.jupiter.api.Test; - -import java.util.List; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class MojoDescriptorTest -{ - @Test - public void getParameterMap() throws DuplicateParameterException - { - MojoDescriptor mojoDescriptor = new MojoDescriptor(); - Parameter param1 = Parameter.newBuilder().name( "param1" ).build(); - mojoDescriptor.addParameter( param1 ); - - assertEquals( 1, mojoDescriptor.getParameters().size() ); - - assertEquals( mojoDescriptor.getParameters().size(), mojoDescriptor.getParameterMap().size() ); - - Parameter param2 = Parameter.newBuilder().name( "param2" ).build(); - mojoDescriptor.addParameter( param2 ); - - assertEquals( 2, mojoDescriptor.getParameters().size() ); - assertEquals( mojoDescriptor.getParameters().size(), mojoDescriptor.getParameterMap().size() ); - } - -} \ No newline at end of file diff --git a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilderTest.java b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilderTest.java index 17fe8b67d40f..88d249069c9c 100644 --- a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilderTest.java +++ b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorBuilderTest.java @@ -131,7 +131,7 @@ public void testBuildReader() md = pd.getMojos().get( 1 ); assertEquals( "war", md.getGoal() ); - assertEquals( "runtime", md.getDependencyResolutionRequired() ); // default + assertNull( md.getDependencyResolutionRequired() ); assertNull( md.getDependencyCollectionRequired() ); assertTrue( md.isThreadSafe() ); } diff --git a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorXpp3ReaderTest.java b/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorXpp3ReaderTest.java deleted file mode 100644 index a840b2b4b5d9..000000000000 --- a/maven-plugin-api/src/test/java/org/apache/maven/plugin/descriptor/PluginDescriptorXpp3ReaderTest.java +++ /dev/null @@ -1,142 +0,0 @@ -package org.apache.maven.plugin.descriptor; - -/* - * 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 java.io.IOException; -import java.io.InputStream; -import java.util.Objects; - -import org.codehaus.plexus.component.repository.ComponentDependency; -import org.codehaus.plexus.component.repository.ComponentRequirement; -import org.codehaus.plexus.configuration.PlexusConfiguration; -import org.codehaus.plexus.configuration.PlexusConfigurationException; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -/** - * Tests {@link PluginDescriptorBuilder}. - * - * @author Benjamin Bentmann - */ -public class PluginDescriptorXpp3ReaderTest -{ - - private PluginDescriptor build( String resource ) - throws IOException, PlexusConfigurationException, XmlPullParserException - { - try (InputStream input = Objects.requireNonNull( getClass().getResourceAsStream( resource ) ) ) - { - // must not be strict in order to disregard no longer supported elements - return new org.apache.maven.plugin.descriptor.io.xpp3.PluginDescriptorXpp3Reader().read( input, false ); - } - } - - @Test - public void testBuildReader() - throws Exception - { - PluginDescriptor pd = build( "/plugin.xml" ); - - assertEquals( "org.apache.maven.plugins", pd.getGroupId() ); - assertEquals( "maven-jar-plugin", pd.getArtifactId() ); - assertEquals( "2.3-SNAPSHOT", pd.getVersion() ); - assertEquals( "jar", pd.getGoalPrefix() ); - assertEquals( "plugin-description", pd.getDescription() ); - assertFalse( pd.isIsolatedRealm() ); - assertTrue( pd.isInheritedByDefault() ); - assertEquals( 2, pd.getMojos().size() ); - assertEquals( 1, pd.getDependencies().size() ); - - MojoDescriptor md = pd.getMojos().get( 0 ); - - assertEquals( "jar", md.getGoal() ); - assertEquals( "mojo-description", md.getDescription() ); - assertEquals( "runtime", md.getDependencyResolutionRequired() ); - assertEquals( "test", md.getDependencyCollectionRequired() ); - assertFalse( md.isAggregator() ); - assertFalse( md.isDirectInvocationOnly() ); - assertTrue( md.isInheritedByDefault() ); - assertFalse( md.isOnlineRequired() ); - assertTrue( md.isProjectRequired() ); - assertFalse( md.isThreadSafe() ); - assertEquals( "package", md.getPhase() ); - assertEquals( "org.apache.maven.plugin.jar.JarMojo", md.getImplementation() ); - assertEquals( "antrun", md.getComponentConfigurator() ); - assertEquals( "java", md.getLanguage() ); - assertEquals( "per-lookup", md.getInstantiationStrategy() ); - assertEquals( "some-goal", md.getExecuteGoal() ); - assertEquals( "generate-sources", md.getExecutePhase() ); - assertEquals( "cobertura", md.getExecuteLifecycle() ); - assertEquals( "2.2", md.getSince() ); - assertEquals( "deprecated-mojo", md.getDeprecated() ); - assertEquals( 1, md.getRequirements().size() ); - assertEquals( 1, md.getParameters().size() ); - - assertNotNull( md.getMojoConfiguration() ); - assertEquals( 1, md.getMojoConfiguration().getChildCount() ); - - PlexusConfiguration pc = md.getMojoConfiguration().getChild( 0 ); - - assertEquals( "${jar.finalName}", pc.getValue() ); - assertEquals( "${project.build.finalName}", pc.getAttribute( "default-value" ) ); - assertEquals( "java.lang.String", pc.getAttribute( "implementation" ) ); - - Parameter mp = md.getParameters().get( 0 ); - - assertEquals( "finalName", mp.getName() ); - assertEquals( "jarName", mp.getAlias() ); - assertEquals( "java.lang.String", mp.getType() ); - assertEquals( "java.lang.String", mp.getImplementation() ); - assertTrue( mp.isEditable() ); - assertFalse( mp.isRequired() ); - assertEquals( "parameter-description", mp.getDescription() ); - assertEquals( "deprecated-parameter", mp.getDeprecated() ); - assertEquals( "${jar.finalName}", mp.getExpression() ); - assertEquals( "${project.build.finalName}", mp.getDefaultValue() ); - assertEquals( "3.0.0", mp.getSince() ); - - ComponentRequirement cr = md.getRequirements().get( 0 ); - - assertEquals( "org.codehaus.plexus.archiver.Archiver", cr.getRole() ); - assertEquals( "jar", cr.getRoleHint() ); - assertEquals( "jarArchiver", cr.getFieldName() ); - - ComponentDependency cd = pd.getDependencies().get( 0 ); - - assertEquals( "org.apache.maven", cd.getGroupId() ); - assertEquals( "maven-plugin-api", cd.getArtifactId() ); - assertEquals( "2.0.6", cd.getVersion() ); - assertEquals( "jar", cd.getType() ); - - md = pd.getMojos().get( 1 ); - - assertEquals( "war", md.getGoal() ); - assertEquals( "runtime", md.getDependencyResolutionRequired() ); // default - assertNull( md.getDependencyCollectionRequired() ); - assertTrue( md.isThreadSafe() ); - } - -}