View Javadoc

1   package org.apache.maven.tools.plugin.extractor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
23  import org.apache.maven.plugin.descriptor.PluginDescriptor;
24  import org.apache.maven.project.MavenProject;
25  import org.codehaus.plexus.logging.AbstractLogEnabled;
26  import org.codehaus.plexus.util.DirectoryScanner;
27  import org.codehaus.plexus.util.FileUtils;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.util.HashSet;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  import java.util.TreeMap;
38  
39  /**
40   * @author jdcasey
41   */
42  public abstract class AbstractScriptedMojoDescriptorExtractor
43      extends AbstractLogEnabled
44      implements MojoDescriptorExtractor
45  {
46      public List execute( MavenProject project, PluginDescriptor pluginDescriptor )
47          throws ExtractionException, InvalidPluginDescriptorException
48      {
49          String metadataExtension = getMetadataFileExtension();
50          String scriptExtension = getScriptFileExtension();
51  
52          Map scriptFilesKeyedByBasedir =
53              gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), scriptExtension );
54  
55          List mojoDescriptors;
56          if ( !StringUtils.isEmpty( metadataExtension ) )
57          {
58              Map metadataFilesKeyedByBasedir =
59                  gatherFilesByBasedir( project.getBasedir(), project.getScriptSourceRoots(), metadataExtension );
60  
61              mojoDescriptors = extractMojoDescriptorsFromMetadata( metadataFilesKeyedByBasedir, pluginDescriptor );
62          }
63          else
64          {
65              mojoDescriptors = extractMojoDescriptors( scriptFilesKeyedByBasedir, pluginDescriptor );
66          }
67  
68          copyScriptsToOutputDirectory( scriptFilesKeyedByBasedir, project.getBuild().getOutputDirectory() );
69  
70          return mojoDescriptors;
71      }
72  
73      protected void copyScriptsToOutputDirectory( Map scriptFilesKeyedByBasedir, String outputDirectory )
74          throws ExtractionException
75      {
76          File outputDir = new File( outputDirectory );
77  
78          if ( !outputDir.exists() )
79          {
80              outputDir.mkdirs();
81          }
82  
83          for ( Iterator it = scriptFilesKeyedByBasedir.entrySet().iterator(); it.hasNext(); )
84          {
85              Map.Entry entry = (Map.Entry) it.next();
86  
87              File sourceDir = new File( (String) entry.getKey() );
88  
89              Set scripts = (Set) entry.getValue();
90  
91              for ( Iterator scriptIterator = scripts.iterator(); scriptIterator.hasNext(); )
92              {
93                  File scriptFile = (File) scriptIterator.next();
94  
95                  String relativePath = scriptFile.getPath().substring( sourceDir.getPath().length() );
96  
97                  if ( relativePath.charAt( 0 ) == File.separatorChar )
98                  {
99                      relativePath = relativePath.substring( 1 );
100                 }
101 
102                 File outputFile = new File( outputDir, relativePath ).getAbsoluteFile();
103 
104                 if ( !outputFile.getParentFile().exists() )
105                 {
106                     outputFile.getParentFile().mkdirs();
107                 }
108 
109                 try
110                 {
111                     FileUtils.copyFile( scriptFile, outputFile );
112                 }
113                 catch ( IOException e )
114                 {
115                     throw new ExtractionException(
116                         "Cannot copy script file: " + scriptFile + " to output: " + outputFile, e );
117                 }
118             }
119         }
120     }
121 
122     protected Map gatherFilesByBasedir( File basedir, List directories, String scriptFileExtension )
123     {
124         Map sourcesByBasedir = new TreeMap();
125 
126         for ( Iterator it = directories.iterator(); it.hasNext(); )
127         {
128             Set sources = new HashSet();
129 
130             String resourceDir = (String) it.next();
131 
132             File dir = new File( basedir, resourceDir ).getAbsoluteFile();
133 
134             resourceDir = dir.getPath();
135 
136             if ( dir.exists() )
137             {
138                 DirectoryScanner scanner = new DirectoryScanner();
139 
140                 scanner.setBasedir( dir );
141                 scanner.addDefaultExcludes();
142                 scanner.setIncludes( new String[]{"**/*" + scriptFileExtension} );
143                 scanner.scan();
144 
145                 String[] relativePaths = scanner.getIncludedFiles();
146 
147                 for ( int i = 0; i < relativePaths.length; i++ )
148                 {
149                     String relativePath = relativePaths[i];
150                     File scriptFile = new File( dir, relativePath ).getAbsoluteFile();
151 
152                     if ( scriptFile.isFile() && relativePath.endsWith( scriptFileExtension ) )
153                     {
154                         sources.add( scriptFile );
155                     }
156                 }
157 
158                 sourcesByBasedir.put( resourceDir, sources );
159             }
160         }
161 
162         return sourcesByBasedir;
163     }
164 
165     protected List extractMojoDescriptorsFromMetadata( Map metadataFilesKeyedByBasedir,
166                                                        PluginDescriptor pluginDescriptor )
167         throws ExtractionException, InvalidPluginDescriptorException
168     {
169         return null;
170     }
171 
172     protected String getMetadataFileExtension()
173     {
174         return null;
175     }
176 
177     protected List extractMojoDescriptors( Map scriptFilesKeyedByBasedir, PluginDescriptor pluginDescriptor )
178         throws ExtractionException, InvalidPluginDescriptorException
179     {
180         return null;
181     }
182 
183     protected abstract String getScriptFileExtension();
184 
185 }