View Javadoc

1   package org.apache.maven.archetype.common;
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.archetype.common.util.ListScanner;
23  import org.apache.maven.archetype.common.util.PathUtils;
24  import org.apache.maven.archetype.metadata.FileSet;
25  import org.codehaus.plexus.component.annotations.Component;
26  import org.codehaus.plexus.logging.AbstractLogEnabled;
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.ArrayList;
33  import java.util.Arrays;
34  import java.util.HashSet;
35  import java.util.List;
36  import java.util.Set;
37  
38  @Component( role = ArchetypeFilesResolver.class )
39  public class DefaultArchetypeFilesResolver
40      extends AbstractLogEnabled
41      implements ArchetypeFilesResolver
42  {
43      public List<String> getFilesWithExtension( List<String> files, String extension )
44      {
45          ListScanner scanner = new ListScanner();
46          scanner.setBasedir( "" );
47  
48          scanner.setIncludes( "**/*." + extension );
49          scanner.setExcludes( "" );
50  
51          return scanner.scan( files );
52      }
53  
54      public List<String> getFilteredFiles( List<String> files, String filtered )
55      {
56          ListScanner scanner = new ListScanner();
57          scanner.setBasedir( "" );
58  
59          scanner.setIncludes( filtered );
60          scanner.setExcludes( "" );
61  
62          List<String> result = scanner.scan( files );
63          getLogger().debug( "Scanned " + result.size() + " filtered files in " + files.size() + " files" );
64          System.out.println(
65              "Scanned " + result.size() + " filtered files in " + files.size() + " files: " + StringUtils.join(
66                  result.iterator(), ", " ) );
67  
68          return result;
69      }
70  
71      public List<String> filterFiles( String moduleOffset, FileSet fileSet, List<String> archetypeResources )
72      {
73          ListScanner scanner = new ListScanner();
74          scanner.setBasedir( ( StringUtils.isEmpty( moduleOffset ) ? "" : ( moduleOffset + File.separatorChar ) )
75                                  + fileSet.getDirectory() );
76          scanner.setIncludes( fileSet.getIncludes() );
77          scanner.setExcludes( fileSet.getExcludes() );
78          scanner.setCaseSensitive( true );
79  
80          return scanner.scan( archetypeResources );
81      }
82  
83      public List<String> findOtherResources( int level, List<String> files, String languages )
84      {
85          ListScanner scanner = new ListScanner();
86  
87          scanner.setBasedir( "" );
88  
89          StringBuilder includes = new StringBuilder();
90          for ( int i = 0; i < level; i++ )
91          {
92              includes.append( "*/" );
93          }
94  
95          includes.append( "**" );
96          scanner.setIncludes( includes.toString() );
97          scanner.setExcludes( languages );
98  
99          List<String> result = scanner.scan( files );
100         getLogger().debug(
101             "Scanned " + result.size() + " other resources in " + files.size() + " files at level " + level );
102 
103         return result;
104     }
105 
106     public List<String> findOtherResources( int level, List<String> files, List<String> sourcesFiles, String languages )
107     {
108         ListScanner scanner = new ListScanner();
109 
110         scanner.setBasedir( "" );
111 
112         Set<String> selectedDirectories = new HashSet<String>();
113 
114         List<String> includes = new ArrayList<String>();
115 
116         for ( String sourcesFile : sourcesFiles )
117         {
118             String directory = PathUtils.getDirectory( sourcesFile, level - 1 );
119             if ( !selectedDirectories.contains( directory ) )
120             {
121                 includes.add( directory + "/**" );
122             }
123 
124             selectedDirectories.add( directory );
125         }
126 
127         scanner.setExcludes( languages );
128 
129         List<String> result = scanner.scan( files );
130         getLogger().debug(
131             "Scanned " + result.size() + " other resources in " + files.size() + " files at level " + level );
132 
133         return result;
134     }
135 
136     public List<String> findOtherSources( int level, List<String> files, String languages )
137     {
138         ListScanner scanner = new ListScanner();
139         scanner.setBasedir( "" );
140 
141         StringBuilder levelDirectory = new StringBuilder();
142         for ( int i = 0; i < ( level - 1 ); i++ )
143         {
144             levelDirectory.append( "*/" );
145         }
146 
147         StringBuilder includes = new StringBuilder();
148         String[] languagesAsArray = StringUtils.split( languages );
149         for ( int i = 0; i < languagesAsArray.length; i++ )
150         {
151             includes.append( levelDirectory ).append( languagesAsArray[i] );
152         }
153 
154         scanner.setIncludes( includes.toString() );
155 
156         List<String> result = scanner.scan( files );
157         getLogger().debug(
158             "Scanned " + result.size() + " other sources in " + files.size() + " files at level " + level );
159 
160         return result;
161     }
162 
163     public List<String> findResourcesMainFiles( List<String> files, String languages )
164     {
165         ListScanner scanner = new ListScanner();
166         scanner.setBasedir( "src/main" );
167 
168         scanner.setIncludes( "**" );
169         scanner.setExcludes( languages );
170 
171         List<String> result = scanner.scan( files );
172         getLogger().debug( "Scanned " + result.size() + " resources in " + files.size() + " files" );
173 
174         return result;
175     }
176 
177     public List<String> findResourcesTestFiles( List<String> files, String languages )
178     {
179         ListScanner scanner = new ListScanner();
180         scanner.setBasedir( "src/test" );
181 
182         scanner.setIncludes( "**" );
183         scanner.setExcludes( languages );
184 
185         List<String> result = scanner.scan( files );
186         getLogger().debug( "Scanned " + result.size() + " test resources in " + files.size() + " files" );
187 
188         return result;
189     }
190 
191     public List<String> findSiteFiles( List<String> files, String languages )
192     {
193         ListScanner scanner = new ListScanner();
194         scanner.setBasedir( "src/site" );
195 
196         scanner.setIncludes( "**" );
197         scanner.setExcludes( languages );
198 
199         List<String> result = scanner.scan( files );
200         getLogger().debug( "Scanned " + result.size() + " site resources in " + files.size() + " files" );
201 
202         return result;
203     }
204 
205     public List<String> findSourcesMainFiles( List<String> files, String languages )
206     {
207         ListScanner scanner = new ListScanner();
208         scanner.setBasedir( "src/main" );
209 
210         scanner.setIncludes( languages );
211 
212         List<String> result = scanner.scan( files );
213         getLogger().debug( "Scanned " + result.size() + " sources in " + files.size() + " files" );
214 
215         return result;
216     }
217 
218     public List<String> findSourcesTestFiles( List<String> files, String languages )
219     {
220         ListScanner scanner = new ListScanner();
221         scanner.setBasedir( "src/test" );
222 
223         scanner.setIncludes( languages );
224 
225         List<String> result = scanner.scan( files );
226         getLogger().debug( "Scanned " + result.size() + " test sources in " + files.size() + " files" );
227 
228         return result;
229     }
230 
231     public List<String> getPackagedFiles( List<String> files, String packageName )
232     {
233         List<String> packagedFiles = new ArrayList<String>();
234         for ( String file : files )
235         {
236             if ( file.startsWith( packageName ) )
237             {
238                 packagedFiles.add( file.substring( packageName.length() + 1 ) );
239             }
240         }
241         getLogger().debug( "Scanned " + packagedFiles.size() + " packaged files in " + files.size() + " files" );
242         return packagedFiles;
243     }
244 
245     public String resolvePackage( File basedir, List<String> languages )
246         throws IOException
247     {
248         getLogger().debug( "Resolving package in " + basedir + " using languages " + languages );
249 
250         List<String> files = resolveFiles( basedir, languages );
251 
252         return resolvePackage( files );
253     }
254 
255     public List<String> getUnpackagedFiles( List<String> files, String packageName )
256     {
257         List<String> unpackagedFiles = new ArrayList<String>();
258         for ( String file : files )
259         {
260             if ( !file.startsWith( packageName ) )
261             {
262                 unpackagedFiles.add( file );
263             }
264         }
265         getLogger().debug( "Scanned " + unpackagedFiles.size() + " unpackaged files in " + files.size() + " files" );
266         return unpackagedFiles;
267     }
268 
269     private String getCommonPackage( String packageName, String templatePackage )
270     {
271         String common = "";
272 
273         String difference = StringUtils.difference( packageName, templatePackage );
274         if ( StringUtils.isNotEmpty( difference ) )
275         {
276             String temporaryCommon =
277                 StringUtils.substring( templatePackage, 0, templatePackage.lastIndexOf( difference ) );
278             if ( !difference.startsWith( "." ) )
279             {
280                 common = StringUtils.substring( temporaryCommon, 0, temporaryCommon.lastIndexOf( "." ) );
281             }
282             else
283             {
284                 common = temporaryCommon;
285             }
286         }
287         else
288         {
289             common = packageName;
290         }
291 
292         return common;
293     }
294 
295     private List<String> resolveFiles( File basedir, List<String> languages )
296         throws IOException
297     {
298         String[] languagesArray = languages.toArray( new String[languages.size()] );
299         String[] languagesPathesArray = new String[languagesArray.length];
300         for ( int i = 0; i < languagesArray.length; i++ )
301         {
302             languagesPathesArray[i] = "**/src/**/" + languagesArray[i] + "/**";
303         }
304 
305         String excludes = "target";
306         for ( String defaultExclude : Arrays.asList( ListScanner.DEFAULTEXCLUDES ) )
307         {
308             excludes += "," + defaultExclude + "/**";
309         }
310 
311         @SuppressWarnings( "unchecked" ) List<File> absoluteFiles =
312             FileUtils.getFiles( basedir, StringUtils.join( languagesPathesArray, "," ), excludes );
313 
314         getLogger().debug( "Found " + absoluteFiles.size() + " potential archetype files" );
315 
316         List<String> files = new ArrayList<String>( absoluteFiles.size() );
317 
318         for ( File file : absoluteFiles )
319         {
320             String filePath =
321                 StringUtils.prechomp( file.getAbsolutePath(), basedir.getAbsolutePath() + File.separator );
322 
323             String minusSrc = StringUtils.prechomp( filePath, "src" + File.separator );
324 
325             for ( int i = 0; i < languagesArray.length; i++ )
326             {
327                 String language = languagesArray[i];
328 
329                 if ( StringUtils.countMatches( minusSrc, File.separator + language + File.separator ) > 0 )
330                 {
331                     String minusLanguage = StringUtils.prechomp( minusSrc, language + File.separator );
332 
333                     files.add( toUnixPath( minusLanguage ) );
334                 }
335             }
336         }
337 
338         getLogger().debug( "Found " + files.size() + " archetype files for package resolution " );
339 
340         return files;
341     }
342 
343     private String resolvePackage( List<String> files )
344     {
345         String packageName = null;
346         for ( String minusLanguage : files )
347         {
348             String filePackage;
349             if ( minusLanguage.indexOf( "/" ) >= 0 )
350             {
351                 filePackage =
352                     StringUtils.replace( minusLanguage.substring( 0, minusLanguage.lastIndexOf( "/" ) ), "/", "." );
353             }
354             else
355             {
356                 filePackage = "";
357             }
358 
359             if ( packageName == null )
360             {
361                 packageName = filePackage;
362             }
363             else
364             {
365                 packageName = getCommonPackage( packageName, filePackage );
366             }
367         }
368 
369         getLogger().debug( "Package resolved to " + packageName );
370 
371         return packageName;
372     }
373 
374     private String toUnixPath( String path )
375     {
376         return path.replace( File.separatorChar, '/' );
377     }
378 }