001package org.apache.maven.plugins.enforcer.utils; 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 java.util.ArrayList; 023import java.util.List; 024import java.util.Optional; 025 026import org.apache.maven.model.InputLocation; 027import org.apache.maven.model.InputLocationTracker; 028import org.apache.maven.model.Plugin; 029import org.apache.maven.model.ReportPlugin; 030 031/** 032 * @author Brian Fox 033 * 034 */ 035public class PluginWrapper 036{ 037 private final String groupId; 038 039 private final String artifactId; 040 041 private final String version; 042 043 private final InputLocationTracker locationTracker; 044 045 public static List<PluginWrapper> addAll( List<? extends InputLocationTracker> plugins, boolean banMavenDefaults ) 046 { 047 List<PluginWrapper> results = null; 048 049 if ( !plugins.isEmpty() ) 050 { 051 results = new ArrayList<>( plugins.size() ); 052 for ( InputLocationTracker o : plugins ) 053 { 054 // null or true means it is most assumed a Maven default 055 if ( banMavenDefaults && ( isVersionFromDefaultLifecycleBindings( o ).orElse( true ) 056 || isVersionFromSuperpom( o ).orElse( true ) ) ) 057 { 058 continue; 059 } 060 061 if ( o instanceof Plugin ) 062 { 063 results.add( new PluginWrapper( (Plugin) o ) ); 064 } 065 else 066 { 067 if ( o instanceof ReportPlugin ) 068 { 069 results.add( new PluginWrapper( (ReportPlugin) o ) ); 070 } 071 } 072 } 073 } 074 return results; 075 } 076 077 /** 078 * Whether the version is coming from the default lifecycle bindings. 079 * Cannot be determined before Maven 3.6.1 080 * 081 * @param o either Plugin or ReportPlugin 082 * @return null if untraceable, otherwise its matching value 083 * @see <a href="https://issues.apache.org/jira/browse/MNG-6600">MNG-6600</a> 084 */ 085 public static Optional<Boolean> isVersionFromDefaultLifecycleBindings( InputLocationTracker o ) 086 { 087 InputLocation versionLocation = o.getLocation( "version" ); 088 if ( versionLocation == null ) 089 { 090 return Optional.empty(); 091 } 092 093 String modelId = versionLocation.getSource().getModelId(); 094 return Optional.of( modelId.startsWith( "org.apache.maven:maven-core:" ) 095 && modelId.endsWith( ":default-lifecycle-bindings" ) ); 096 } 097 098 /** 099 * Whether the version is coming from the super POM. 100 * Cannot be determined before Maven 3.6.1 101 * 102 * @param o either Plugin or ReportPlugin 103 * @return null if untraceable, otherwise its matching value 104 * @see <a href="https://issues.apache.org/jira/browse/MNG-6593">MNG-6593</a> 105 */ 106 public static Optional<Boolean> isVersionFromSuperpom( InputLocationTracker o ) 107 { 108 InputLocation versionLocation = o.getLocation( "version" ); 109 if ( versionLocation == null ) 110 { 111 return Optional.empty(); 112 } 113 114 String modelId = versionLocation.getSource().getModelId(); 115 return Optional.of( modelId.startsWith( "org.apache.maven:maven-model-builder:" ) 116 && modelId.endsWith( ":super-pom" ) ); 117 } 118 119 private PluginWrapper( Plugin plugin ) 120 { 121 this.groupId = plugin.getGroupId(); 122 this.artifactId = plugin.getArtifactId(); 123 this.version = plugin.getVersion(); 124 this.locationTracker = plugin; 125 } 126 127 private PluginWrapper( ReportPlugin plugin ) 128 { 129 this.groupId = plugin.getGroupId(); 130 this.artifactId = plugin.getArtifactId(); 131 this.version = plugin.getVersion(); 132 this.locationTracker = plugin; 133 } 134 135 public String getGroupId() 136 { 137 return groupId; 138 } 139 140 public String getArtifactId() 141 { 142 return artifactId; 143 } 144 145 public String getVersion() 146 { 147 return version; 148 } 149 150 public String getSource() 151 { 152 InputLocation inputLocation = locationTracker.getLocation( "version" ); 153 154 if ( inputLocation == null ) 155 { 156 // most likely super-pom or default-lifecycle-bindings in Maven 3.6.0 or before (MNG-6593 / MNG-6600) 157 return "unknown"; 158 } 159 else 160 { 161 return inputLocation.getSource().getLocation(); 162 } 163 } 164}