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