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.Collections; 025import java.util.LinkedHashMap; 026import java.util.List; 027import java.util.Map; 028import java.util.Optional; 029 030import org.apache.maven.plugin.descriptor.MojoDescriptor; 031import org.apache.maven.plugin.descriptor.Parameter; 032import org.apache.maven.plugin.descriptor.PluginDescriptor; 033import org.apache.maven.plugin.plugin.report.PluginReport; 034import org.apache.maven.rtinfo.RuntimeInformation; 035import org.apache.maven.tools.plugin.EnhancedParameterWrapper; 036import org.apache.maven.tools.plugin.ExtendedPluginDescriptor; 037import org.codehaus.plexus.configuration.PlexusConfiguration; 038import org.codehaus.plexus.configuration.PlexusConfigurationException; 039 040/** 041 * Reads enhanced plugin.xml files as generated by 042 * {@code 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<>( 091 Optional.ofNullable(mojoDescriptor.getParameters()).orElseGet(Collections::emptyList)); 092 Map<String, Parameter> parameterMap = new LinkedHashMap<>(mojoDescriptor.getParameterMap()); 093 094 for (PlexusConfiguration d : parameterConfigurations) { 095 String parameterName = d.getChild("name").getValue(); 096 // don't call getParameterMap() to not populate 097 Parameter pd = parameterMap.get(parameterName); 098 if (requireAddingMissingParameterSinceField) { 099 addMissingParameterSinceField(pd, d); 100 } 101 PlexusConfiguration configTypeJavadocUrl = d.getChild("typeJavadocUrl", false); 102 if (configTypeJavadocUrl != null) { 103 String parameterTypeJavadocUrl = configTypeJavadocUrl.getValue(); 104 EnhancedParameterWrapper enhancedParameter = new EnhancedParameterWrapper(pd); 105 enhancedParameter.setTypeJavadocUrl(URI.create(parameterTypeJavadocUrl)); 106 parameters.set(mojoDescriptor.getParameters().indexOf(pd), enhancedParameter); 107 parameterMap.put(parameterName, enhancedParameter); 108 } 109 } 110 111 // TODO This cruft has been resolved in Maven 3.8.9/3.9.7/4.0.0-alpha-1 with MNG-6776/MNG-7309 112 if (mojoDescriptor.getParameters() != null) { 113 mojoDescriptor.getParameters().clear(); 114 } 115 // set parameters 116 mojoDescriptor.setParameters(parameters); 117 mojoDescriptor.getParameterMap().putAll(parameterMap); 118 119 return mojoDescriptor; 120 } 121 122 /** 123 * Reads the plugin descriptor and adds the fix for <a href="https://issues.apache.org/jira/browse/MNG-6109"> 124 * MNG-6109</a> when using Maven-3.3.9 and before. 125 * Method can be removed once Maven 3.5.0 is the prerequisite for this plugin. 126 * @throws PlexusConfigurationException 127 * 128 * @since 3.5.1 129 * @see <a href="https://issues.apache.org/jira/browse/MNG-6109">MNG-6109</a> 130 * @see <a href="https://issues.apache.org/jira/browse/MPLUGIN-319">MPLUGIN-319</a> 131 */ 132 void addMissingParameterSinceField(Parameter pd, PlexusConfiguration d) throws PlexusConfigurationException { 133 String parameterSince = d.getChild("since").getValue(); 134 pd.setSince(parameterSince); 135 } 136}