001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.plugins.plugin.descriptor; 020 021import java.io.Reader; 022import java.net.URI; 023import java.util.ArrayList; 024import java.util.LinkedHashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.apache.maven.plugin.descriptor.MojoDescriptor; 029import org.apache.maven.plugin.descriptor.Parameter; 030import org.apache.maven.plugin.descriptor.PluginDescriptor; 031import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder; 032import org.apache.maven.plugin.plugin.report.PluginReport; 033import org.apache.maven.rtinfo.RuntimeInformation; 034import org.apache.maven.tools.plugin.EnhancedParameterWrapper; 035import org.apache.maven.tools.plugin.ExtendedPluginDescriptor; 036import org.apache.maven.tools.plugin.generator.PluginDescriptorFilesGenerator; 037import org.codehaus.plexus.configuration.PlexusConfiguration; 038import org.codehaus.plexus.configuration.PlexusConfigurationException; 039 040/** 041 * Reads enhanced plugin.xml files as generated by 042 * {@link PluginDescriptorFilesGenerator} and used by {@link PluginReport}. 043 * Populates the slightly extended {@link Parameter} object {@link EnhancedParameterWrapper}. 044 * In addition populates all (optional) elements added after Maven Plugin API 3.2.5. 045 */ 046public class EnhancedPluginDescriptorBuilder extends PluginDescriptorBuilder { 047 private final boolean requireAddingMissingParameterSinceField; 048 private PlexusConfiguration configuration; 049 050 public EnhancedPluginDescriptorBuilder(RuntimeInformation rtInfo) { 051 this(rtInfo.isMavenVersion("[,3.3.9]")); 052 } 053 054 EnhancedPluginDescriptorBuilder(boolean requireAddingMissingParameterSinceField) { 055 this.requireAddingMissingParameterSinceField = requireAddingMissingParameterSinceField; 056 } 057 058 /** 059 * Cache the returned configuration for additional evaluation in {@link #build(Reader, String)}. 060 */ 061 @Override 062 public PlexusConfiguration buildConfiguration(Reader reader) throws PlexusConfigurationException { 063 configuration = super.buildConfiguration(reader); 064 return configuration; 065 } 066 067 @Override 068 public PluginDescriptor build(Reader reader, String source) throws PlexusConfigurationException { 069 PluginDescriptor pluginDescriptor = super.build(reader, source); 070 // elements added in plugin descriptor 1.1 071 ExtendedPluginDescriptor extendedPluginDescriptor = new ExtendedPluginDescriptor(pluginDescriptor); 072 extendedPluginDescriptor.setRequiredJavaVersion( 073 configuration.getChild("requiredJavaVersion").getValue()); 074 extendedPluginDescriptor.setRequiredMavenVersion( 075 configuration.getChild("requiredMavenVersion").getValue()); 076 return extendedPluginDescriptor; 077 } 078 079 @Override 080 public MojoDescriptor buildComponentDescriptor(PlexusConfiguration c, PluginDescriptor pluginDescriptor) 081 throws PlexusConfigurationException { 082 MojoDescriptor mojoDescriptor = super.buildComponentDescriptor(c, pluginDescriptor); 083 084 // ---------------------------------------------------------------------- 085 // Parameters 086 // ---------------------------------------------------------------------- 087 088 PlexusConfiguration[] parameterConfigurations = c.getChild("parameters").getChildren("parameter"); 089 090 List<Parameter> parameters = new ArrayList<>(mojoDescriptor.getParameters()); 091 Map<String, Parameter> parameterMap = new LinkedHashMap<>(mojoDescriptor.getParameterMap()); 092 093 for (PlexusConfiguration d : parameterConfigurations) { 094 String parameterName = d.getChild("name").getValue(); 095 // don't call getParameterMap() to not populate 096 Parameter pd = parameterMap.get(parameterName); 097 if (requireAddingMissingParameterSinceField) { 098 addMissingParameterSinceField(pd, d); 099 } 100 PlexusConfiguration configTypeJavadocUrl = d.getChild("typeJavadocUrl", false); 101 if (configTypeJavadocUrl != null) { 102 String parameterTypeJavadocUrl = configTypeJavadocUrl.getValue(); 103 EnhancedParameterWrapper enhancedParameter = new EnhancedParameterWrapper(pd); 104 enhancedParameter.setTypeJavadocUrl(URI.create(parameterTypeJavadocUrl)); 105 parameters.set(mojoDescriptor.getParameters().indexOf(pd), enhancedParameter); 106 parameterMap.put(parameterName, enhancedParameter); 107 } 108 } 109 110 // clear() is required for maven < 3.6.2 111 mojoDescriptor.getParameters().clear(); 112 // set parameters 113 mojoDescriptor.setParameters(parameters); 114 // on maven < 3.6.2, getParameterMap is kept internally in a field 115 // so update it in case we're on an old maven version 116 mojoDescriptor.getParameterMap().putAll(parameterMap); 117 118 return mojoDescriptor; 119 } 120 121 /** 122 * Reads the plugin descriptor and adds the fix for <a href="https://issues.apache.org/jira/browse/MNG-6109"> 123 * MNG-6109</a> when using Maven-3.3.9 and before. 124 * Method can be removed once Maven 3.5.0 is the prerequisite for this plugin. 125 * @throws PlexusConfigurationException 126 * 127 * @since 3.5.1 128 * @see <a href="https://issues.apache.org/jira/browse/MNG-6109">MNG-6109</a> 129 * @see <a href="https://issues.apache.org/jira/browse/MPLUGIN-319">MPLUGIN-319</a> 130 */ 131 void addMissingParameterSinceField(Parameter pd, PlexusConfiguration d) throws PlexusConfigurationException { 132 String parameterSince = d.getChild("since").getValue(); 133 pd.setSince(parameterSince); 134 } 135}