001package org.apache.maven.tools.plugin.extractor; 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.io.File; 023import java.io.IOException; 024import java.util.HashSet; 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028import java.util.TreeMap; 029 030import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; 031import org.apache.maven.plugin.descriptor.MojoDescriptor; 032import org.apache.maven.project.MavenProject; 033import org.apache.maven.tools.plugin.PluginToolsRequest; 034import org.codehaus.plexus.logging.AbstractLogEnabled; 035import org.codehaus.plexus.util.DirectoryScanner; 036import org.codehaus.plexus.util.FileUtils; 037import org.codehaus.plexus.util.StringUtils; 038 039/** 040 * @author jdcasey 041 */ 042public abstract class AbstractScriptedMojoDescriptorExtractor 043 extends AbstractLogEnabled 044 implements MojoDescriptorExtractor 045{ 046 /** {@inheritDoc} */ 047 @Override 048 public List<MojoDescriptor> execute( PluginToolsRequest request ) 049 throws ExtractionException, InvalidPluginDescriptorException 050 { 051 getLogger().debug( "Running: " + getClass().getName() ); 052 String metadataExtension = getMetadataFileExtension( request ); 053 String scriptExtension = getScriptFileExtension( request ); 054 055 MavenProject project = request.getProject(); 056 057 @SuppressWarnings( "unchecked" ) 058 Map<String, Set<File>> scriptFilesKeyedByBasedir = 059 gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), scriptExtension, request ); 060 061 List<MojoDescriptor> mojoDescriptors; 062 if ( !StringUtils.isEmpty( metadataExtension ) ) 063 { 064 @SuppressWarnings( "unchecked" ) 065 Map<String, Set<File>> metadataFilesKeyedByBasedir = 066 gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), metadataExtension, 067 request ); 068 069 mojoDescriptors = extractMojoDescriptorsFromMetadata( metadataFilesKeyedByBasedir, request ); 070 } 071 else 072 { 073 mojoDescriptors = extractMojoDescriptors( scriptFilesKeyedByBasedir, request ); 074 } 075 076 copyScriptsToOutputDirectory( scriptFilesKeyedByBasedir, project.getBuild().getOutputDirectory(), request ); 077 078 return mojoDescriptors; 079 } 080 081 /** 082 * @param scriptFilesKeyedByBasedir not null 083 * @param outputDirectory not null 084 * @param request the request 085 * @throws ExtractionException if any 086 */ 087 protected void copyScriptsToOutputDirectory( Map<String, Set<File>> scriptFilesKeyedByBasedir, 088 String outputDirectory, PluginToolsRequest request ) 089 throws ExtractionException 090 { 091 File outputDir = new File( outputDirectory ); 092 093 if ( !outputDir.exists() ) 094 { 095 outputDir.mkdirs(); 096 } 097 098 for ( Map.Entry<String, Set<File>> entry : scriptFilesKeyedByBasedir.entrySet() ) 099 { 100 File sourceDir = new File( entry.getKey() ); 101 102 Set<File> scripts = entry.getValue(); 103 104 for ( File scriptFile : scripts ) 105 { 106 String relativePath = scriptFile.getPath().substring( sourceDir.getPath().length() ); 107 108 if ( relativePath.charAt( 0 ) == File.separatorChar ) 109 { 110 relativePath = relativePath.substring( 1 ); 111 } 112 113 File outputFile = new File( outputDir, relativePath ).getAbsoluteFile(); 114 115 if ( !outputFile.getParentFile().exists() ) 116 { 117 outputFile.getParentFile().mkdirs(); 118 } 119 120 try 121 { 122 FileUtils.copyFile( scriptFile, outputFile ); 123 } 124 catch ( IOException e ) 125 { 126 throw new ExtractionException( 127 "Cannot copy script file: " + scriptFile + " to output: " + outputFile, e ); 128 } 129 } 130 } 131 } 132 133 /** 134 * @param basedir not null 135 * @param directories not null 136 * @param scriptFileExtension not null 137 * @param request the request 138 * @return map with subdirs paths as key 139 */ 140 protected Map<String, Set<File>> gatherFilesByBasedir( File basedir, List<String> directories, 141 String scriptFileExtension, PluginToolsRequest request ) 142 { 143 Map<String, Set<File>> sourcesByBasedir = new TreeMap<>(); 144 145 for ( String resourceDir : directories ) 146 { 147 Set<File> sources = new HashSet<>(); 148 149 getLogger().debug( "Scanning script dir: " + resourceDir + " with extractor: " + getClass().getName() ); 150 File dir = new File( resourceDir ); 151 if ( !dir.isAbsolute() ) 152 { 153 dir = new File( basedir, resourceDir ).getAbsoluteFile(); 154 } 155 156 resourceDir = dir.getPath(); 157 158 if ( dir.exists() ) 159 { 160 DirectoryScanner scanner = new DirectoryScanner(); 161 162 scanner.setBasedir( dir ); 163 scanner.addDefaultExcludes(); 164 scanner.setIncludes( new String[]{"**/*" + scriptFileExtension} ); 165 scanner.scan(); 166 167 String[] relativePaths = scanner.getIncludedFiles(); 168 169 for ( String relativePath : relativePaths ) 170 { 171 File scriptFile = new File( dir, relativePath ).getAbsoluteFile(); 172 173 if ( scriptFile.isFile() && relativePath.endsWith( scriptFileExtension ) ) 174 { 175 sources.add( scriptFile ); 176 } 177 } 178 179 sourcesByBasedir.put( resourceDir, sources ); 180 } 181 } 182 183 return sourcesByBasedir; 184 } 185 186 /** 187 * Should be implemented in the sub classes. 188 * 189 * @param metadataFilesByBasedir could be null 190 * @param request The plugin request, never <code>null</code>. 191 * @return always null 192 * @throws ExtractionException if any 193 * @throws InvalidPluginDescriptorException if any 194 */ 195 protected List<MojoDescriptor> extractMojoDescriptorsFromMetadata( Map<String, Set<File>> metadataFilesByBasedir, 196 PluginToolsRequest request ) 197 throws ExtractionException, InvalidPluginDescriptorException 198 { 199 return null; 200 } 201 202 /** 203 * Should be implemented in the sub classes. 204 * @param request the request 205 * @return always null 206 */ 207 protected String getMetadataFileExtension( PluginToolsRequest request ) 208 { 209 return null; 210 } 211 212 /** 213 * Should be implemented in the sub classes. 214 * 215 * @param scriptFilesKeyedByBasedir could be null 216 * @param request The plugin request, never <code>null</code>. 217 * @return always null 218 * @throws ExtractionException if any 219 * @throws InvalidPluginDescriptorException if any 220 */ 221 protected List<MojoDescriptor> extractMojoDescriptors( Map<String, Set<File>> scriptFilesKeyedByBasedir, 222 PluginToolsRequest request ) 223 throws ExtractionException, InvalidPluginDescriptorException 224 { 225 return null; 226 } 227 228 /** 229 * @param request the request 230 * @return the file extension like <code>.bsh</code> for BeanShell. 231 */ 232 protected abstract String getScriptFileExtension( PluginToolsRequest request ); 233 234}