001package org.apache.maven.tools.plugin.extractor.model;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.plugin.descriptor.DuplicateParameterException;
023import org.apache.maven.plugin.descriptor.MojoDescriptor;
024import org.apache.maven.plugin.descriptor.Parameter;
025import org.apache.maven.tools.plugin.extractor.model.io.xpp3.PluginMetadataXpp3Reader;
026import org.codehaus.plexus.component.repository.ComponentRequirement;
027import org.codehaus.plexus.util.ReaderFactory;
028import org.codehaus.plexus.util.StringUtils;
029import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
030
031import java.io.File;
032import java.io.IOException;
033import java.io.Reader;
034import java.util.HashSet;
035import java.util.List;
036import java.util.Set;
037
038/**
039 * Parser for plugin metadata.
040 *
041 * @deprecated Scripting support for mojos is deprecated and is planned tp be removed in maven 4.0
042 */
043@Deprecated
044public class PluginMetadataParser
045{
046    /**
047     * Default implementation path which will be replaced in
048     * AbstractScriptedMojoDescriptorExtractor#extractMojoDescriptorsFromMetadata(Map, PluginDescriptor)
049     */
050    public static final String IMPL_BASE_PLACEHOLDER = "<REPLACE-WITH-MOJO-PATH>";
051
052    /**
053     * @param metadataFile the metadata file to be parse
054     * @return a set of <code>MojoDescriptor</code>
055     * @throws PluginMetadataParseException if any
056     */
057    public Set<MojoDescriptor> parseMojoDescriptors( File metadataFile )
058        throws PluginMetadataParseException
059    {
060        Set<MojoDescriptor> descriptors = new HashSet<>();
061
062        try ( Reader reader = ReaderFactory.newXmlReader( metadataFile ) )
063        {
064
065            PluginMetadataXpp3Reader metadataReader = new PluginMetadataXpp3Reader();
066
067            PluginMetadata pluginMetadata = metadataReader.read( reader );
068
069            List<Mojo> mojos = pluginMetadata.getMojos();
070
071            if ( mojos != null )
072            {
073                for ( Mojo mojo : mojos )
074                {
075                    MojoDescriptor descriptor = asDescriptor( metadataFile, mojo );
076
077                    descriptors.add( descriptor );
078                }
079            }
080        }
081        catch ( IOException | XmlPullParserException e )
082        {
083            throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
084        }
085
086        return descriptors;
087    }
088
089    /**
090     * @param metadataFile not null
091     * @param mojo         not null
092     * @return a mojo descriptor instance
093     * @throws PluginMetadataParseException if any
094     */
095    private MojoDescriptor asDescriptor( File metadataFile, Mojo mojo )
096        throws PluginMetadataParseException
097    {
098        MojoDescriptor descriptor = new MojoDescriptor();
099
100        if ( mojo.getCall() != null )
101        {
102            descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() );
103        }
104        else
105        {
106            descriptor.setImplementation( IMPL_BASE_PLACEHOLDER );
107        }
108
109        descriptor.setGoal( mojo.getGoal() );
110        descriptor.setPhase( mojo.getPhase() );
111        descriptor.setDependencyResolutionRequired( mojo.getRequiresDependencyResolution() );
112        descriptor.setAggregator( mojo.isAggregator() );
113        descriptor.setInheritedByDefault( mojo.isInheritByDefault() );
114        descriptor.setDirectInvocationOnly( mojo.isRequiresDirectInvocation() );
115        descriptor.setOnlineRequired( mojo.isRequiresOnline() );
116        descriptor.setProjectRequired( mojo.isRequiresProject() );
117        descriptor.setRequiresReports( mojo.isRequiresReports() );
118        descriptor.setDescription( mojo.getDescription() );
119        descriptor.setDeprecated( mojo.getDeprecation() );
120        descriptor.setSince( mojo.getSince() );
121
122        LifecycleExecution le = mojo.getExecution();
123        if ( le != null )
124        {
125            descriptor.setExecuteLifecycle( le.getLifecycle() );
126            descriptor.setExecutePhase( le.getPhase() );
127            descriptor.setExecuteGoal( le.getGoal() );
128        }
129
130        List<org.apache.maven.tools.plugin.extractor.model.Parameter> parameters = mojo.getParameters();
131
132        if ( parameters != null && !parameters.isEmpty() )
133        {
134            for ( org.apache.maven.tools.plugin.extractor.model.Parameter param : parameters )
135            {
136                Parameter dParam = new Parameter();
137                dParam.setAlias( param.getAlias() );
138                dParam.setDeprecated( param.getDeprecation() );
139                dParam.setDescription( param.getDescription() );
140                dParam.setEditable( !param.isReadonly() );
141                dParam.setExpression( param.getExpression() );
142                dParam.setDefaultValue( param.getDefaultValue() );
143                dParam.setSince( param.getSince() );
144
145                String property = param.getProperty();
146                if ( StringUtils.isNotEmpty( property ) )
147                {
148                    dParam.setName( property );
149                }
150                else
151                {
152                    dParam.setName( param.getName() );
153                }
154
155                if ( StringUtils.isEmpty( dParam.getName() ) )
156                {
157                    throw new PluginMetadataParseException( metadataFile, "Mojo: \'" + mojo.getGoal()
158                        + "\' has a parameter without either property or name attributes. Please specify one." );
159                }
160
161                dParam.setRequired( param.isRequired() );
162                dParam.setType( param.getType() );
163
164                try
165                {
166                    descriptor.addParameter( dParam );
167                }
168                catch ( DuplicateParameterException e )
169                {
170                    throw new PluginMetadataParseException( metadataFile,
171                                                            "Duplicate parameters detected for mojo: " + mojo.getGoal(),
172                                                            e );
173                }
174            }
175        }
176
177        List<Component> components = mojo.getComponents();
178
179        if ( components != null && !components.isEmpty() )
180        {
181            for ( Component component : components )
182            {
183                ComponentRequirement cr = new ComponentRequirement();
184                cr.setRole( component.getRole() );
185                cr.setRoleHint( component.getHint() );
186
187                descriptor.addRequirement( cr );
188            }
189        }
190
191        return descriptor;
192    }
193}