View Javadoc

1   package org.apache.maven.plugins.repository;
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.MojoExecutionException;
23  import org.apache.maven.plugin.logging.Log;
24  import org.codehaus.plexus.components.interactivity.InputHandler;
25  
26  import java.io.File;
27  import java.io.FilenameFilter;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.Collections;
32  import java.util.Comparator;
33  import java.util.List;
34  import java.util.StringTokenizer;
35  
36  final class BundleUtils
37  {
38      private BundleUtils()
39      {
40      }
41      
42      public static List<File> selectProjectFiles( final File dir, final InputHandler inputHandler, final String finalName,
43                                             final File pom, final Log log, final boolean batchMode )
44          throws MojoExecutionException
45      {
46          File[] projectFiles = dir.listFiles( new FilenameFilter()
47          {
48              public boolean accept( File dir, String name )
49              {
50                  return new File( dir, name ).isFile() && name.startsWith( finalName );
51              }
52          } );
53          
54          List<File> result = new ArrayList<File>();
55          
56          if ( projectFiles == null )
57          {
58              return result;
59          }
60          
61          for ( int i = 0; i < projectFiles.length; i++ )
62          {
63              if ( projectFiles[i].getName().endsWith( ".pom" ) )
64              {
65                  if ( !projectFiles[i].equals( pom ) )
66                  {
67                      log.info( "Detected POM file will be excluded:\n" + projectFiles[i]
68                                                                                       + "\n\nInstead, the bundle will include the POM from:\n" + pom );
69                  }
70              }
71              else if ( projectFiles[i].getName().endsWith( "-bundle.jar" ) )
72              {
73                  log.warn( "Skipping project file which collides with repository bundle filename:\n" + projectFiles[i] );
74              }
75              else
76              {
77                  result.add( projectFiles[i] );
78              }
79          }
80          
81          if ( result.isEmpty() )
82          {
83              return result;
84          }
85          
86          Collections.sort( result, new Comparator<File>()
87          {
88              public int compare( File first, File second )
89              {
90                  String f = first.getName();
91                  String s = second.getName();
92                  
93                  if ( f.length() == s.length() )
94                  {
95                      return f.compareTo( s );
96                  }
97                  
98                  return f.length() < s.length() ? -1 : 1;
99              }
100         } );
101         
102         result = reviseFileList( result, inputHandler, log, batchMode );
103         
104         return result;
105     }
106 
107     public static List<File> reviseFileList( List<File> input, InputHandler inputHandler, Log log, boolean batchMode )
108         throws MojoExecutionException
109     {
110         List<File> result = new ArrayList<File>( input );
111         
112         if ( batchMode )
113         {
114             return result;
115         }
116         
117         while( true )
118         {
119             StringBuffer message = new StringBuffer();
120             message.append( "The following files are marked for inclusion in the repository bundle:\n" );
121             message.append( "\n0.) Done" );
122             
123             int i = 1;
124             for ( File f : result )
125             {
126                 message.append( "\n" ).append( (i++) ).append( ".) " ).append( f.getName() );
127             }
128             
129             message.append( "\n\nPlease select the number(s) for any files you wish to exclude, " +
130                     "or '0' when you're done.\nSeparate the numbers for multiple files with a " +
131                     "comma (',').\n\nSelection: " );
132             
133             log.info( message );
134             String response = null;
135             try
136             {
137                 response = inputHandler.readLine();
138             }
139             catch ( IOException e )
140             {
141                 throw new MojoExecutionException( "Project file selection failed with an I/O exception: " + e.getMessage(), e );
142             }
143             
144             if ( response == null || "0".equals( response ) )
145             {
146                 break;
147             }
148             
149             StringTokenizer st = new StringTokenizer( response, "," );
150             
151             if ( st.countTokens() > 0 )
152             {
153                 int[] idxs = new int[st.countTokens()];
154                 for ( int j = 0; j < idxs.length; j++ )
155                 {
156                     idxs[j] = Integer.parseInt( st.nextToken().trim() );
157                 }
158                 
159                 Arrays.sort( idxs );
160                 
161                 for( int k = idxs.length - 1; k > -1; k-- )
162                 {
163                     if ( idxs[k] < 1 || idxs[k] > result.size() )
164                     {
165                         log.warn( "NOT removing: " + idxs[k] + "; no such file." );
166                         continue;
167                     }
168                     
169                     File removed = (File) result.remove( idxs[k] -1 );
170                     log.info( "Removed: " + removed.getName() );
171                 }
172             }
173             else
174             {
175                 break;
176             }
177         }
178         
179         return result;
180     }
181 
182 }