1 package org.apache.maven.archiva.cli;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.cli.CommandLine;
23 import org.apache.commons.cli.Option;
24 import org.apache.commons.cli.Options;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.archiva.common.utils.DateUtil;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.consumers.ConsumerException;
29 import org.apache.maven.archiva.consumers.RepositoryContentConsumer;
30 import org.apache.maven.archiva.converter.RepositoryConversionException;
31 import org.apache.maven.archiva.converter.legacy.LegacyRepositoryConverter;
32 import org.apache.maven.archiva.model.ArchivaRepository;
33 import org.apache.maven.archiva.model.RepositoryContentStatistics;
34 import org.apache.maven.archiva.repository.RepositoryException;
35 import org.apache.maven.archiva.repository.scanner.RepositoryScanner;
36 import org.codehaus.plexus.PlexusContainer;
37 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
38 import org.codehaus.plexus.tools.cli.AbstractCli;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.text.SimpleDateFormat;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.Iterator;
48 import java.util.List;
50 import java.util.Properties;
55 * @author Jason van Zyl
56 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
59 public class ArchivaCli
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 public static final char CONVERT = 'c';
68 public static final char SCAN = 's';
70 public static final char CONSUMERS = 'u';
72 public static final char LIST_CONSUMERS = 'l';
74 public static final char DUMP_CONFIGURATION = 'd';
76 // ----------------------------------------------------------------------------
77 // Properties controlling Repository conversion
78 // ----------------------------------------------------------------------------
80 public static final String SOURCE_REPO_PATH = "sourceRepositoryPath";
82 public static final String TARGET_REPO_PATH = "targetRepositoryPath";
84 public static final String BLACKLISTED_PATTERNS = "blacklistPatterns";
87 * Configuration store.
91 private ArchivaConfiguration archivaConfiguration;
93 public static void main( String[] args )
96 new ArchivaCli().execute( args );
99 public String getPomPropertiesPath()
101 return "META-INF/maven/org.apache.maven.archiva/archiva-cli/pom.properties";
104 private Option createOption( char shortOpt, String longOpt, int argCount, String description )
106 boolean hasArg = ( argCount > 0 );
107 Option opt = new Option( String.valueOf( shortOpt ), hasArg, description );
108 opt.setLongOpt( longOpt );
111 opt.setArgs( argCount );
116 public Options buildCliOptions( Options options )
118 Option convertOption = createOption( CONVERT, "convert", 1, "Convert a legacy Maven 1.x repository to a "
119 + "Maven 2.x repository using a properties file to describe the conversion." );
120 convertOption.setArgName( "conversion.properties" );
121 options.addOption( convertOption );
123 Option scanOption = createOption( SCAN, "scan", 1, "Scan the specified repository." );
124 scanOption.setArgName( "repository directory" );
125 options.addOption( scanOption );
127 Option consumerOption = createOption( CONSUMERS, "consumers", 1, "The consumers to use. "
128 + "(comma delimited. default: 'count-artifacts')" );
129 consumerOption.setArgName( "consumer list" );
130 options.addOption( consumerOption );
132 Option listConsumersOption = createOption( LIST_CONSUMERS, "listconsumers", 0, "List available consumers." );
133 options.addOption( listConsumersOption );
135 Option dumpConfig = createOption( DUMP_CONFIGURATION, "dumpconfig", 0, "Dump Current Configuration." );
136 options.addOption( dumpConfig );
141 public void invokePlexusComponent( CommandLine cli, PlexusContainer plexus )
144 if ( cli.hasOption( CONVERT ) )
146 doConversion( cli, plexus );
148 else if ( cli.hasOption( SCAN ) )
150 doScan( cli, plexus );
152 else if ( cli.hasOption( LIST_CONSUMERS ) )
154 dumpAvailableConsumers( plexus );
156 else if ( cli.hasOption( DUMP_CONFIGURATION ) )
158 dumpConfiguration( plexus );
166 private void doScan( CommandLine cli, PlexusContainer plexus )
167 throws ConsumerException, ComponentLookupException
169 String path = cli.getOptionValue( SCAN );
171 ArchivaRepository repo = new ArchivaRepository( "cliRepo", "Archiva CLI Provided Repo", "file://" + path );
173 List consumerList = new ArrayList();
175 consumerList.addAll( getConsumerList( cli, plexus ) );
177 RepositoryScanner scanner = new RepositoryScanner();
181 RepositoryContentStatistics stats = scanner.scan( repo, consumerList, true );
183 SimpleDateFormat df = new SimpleDateFormat();
184 System.out.println( "" );
185 System.out.println( ".\\ Scan of " + repo.getId() + " \\.__________________________________________" );
186 System.out.println( " Repository URL : " + repo.getUrl() );
187 System.out.println( " Repository Name : " + repo.getModel().getName() );
188 System.out.println( " Repository Layout : " + repo.getModel().getLayoutName() );
189 System.out.println( " Consumers : (" + consumerList.size() + " active)" );
190 for ( Iterator iter = consumerList.iterator(); iter.hasNext(); )
192 RepositoryContentConsumer consumer = (RepositoryContentConsumer) iter.next();
193 System.out.println( " " + consumer.getId() + " - " + consumer.getDescription() );
195 System.out.println( " Duration : " + DateUtil.getDuration( stats.getDuration() ) );
196 System.out.println( " When Gathered : " + df.format( stats.getWhenGathered() ) );
197 System.out.println( " Total File Count : " + stats.getTotalFileCount() );
198 long averageMsPerFile = ( stats.getDuration() / stats.getTotalFileCount() );
199 System.out.println( " Avg Time Per File : " + DateUtil.getDuration( averageMsPerFile ) );
200 System.out.println( "______________________________________________________________" );
202 catch ( RepositoryException e )
204 e.printStackTrace( System.err );
208 private Collection getConsumerList( CommandLine cli, PlexusContainer plexus )
209 throws ComponentLookupException, ConsumerException
211 String specifiedConsumers = "count-artifacts";
213 if ( cli.hasOption( CONSUMERS ) )
215 specifiedConsumers = cli.getOptionValue( CONSUMERS );
218 List consumerList = new ArrayList();
220 Map availableConsumers = plexus.lookupMap( RepositoryContentConsumer.class );
222 String consumerArray[] = StringUtils.split( specifiedConsumers, ',' );
224 for ( int i = 0; i < consumerArray.length; i++ )
226 String specifiedConsumer = consumerArray[i];
227 if ( !availableConsumers.containsKey( specifiedConsumer ) )
229 System.err.println( "Specified consumer [" + specifiedConsumer + "] not found." );
230 dumpAvailableConsumers( plexus );
234 consumerList.add( availableConsumers.get( specifiedConsumer ) );
240 private void dumpAvailableConsumers( PlexusContainer plexus )
241 throws ComponentLookupException
243 Map availableConsumers = plexus.lookupMap( RepositoryContentConsumer.class );
245 System.out.println( ".\\ Available Consumer List \\.______________________________" );
247 for ( Iterator iter = availableConsumers.entrySet().iterator(); iter.hasNext(); )
249 Map.Entry entry = (Map.Entry) iter.next();
250 String consumerHint = (String) entry.getKey();
251 RepositoryContentConsumer consumer = (RepositoryContentConsumer) entry.getValue();
252 System.out.println( " " + consumerHint + ": " + consumer.getDescription() + " ("
253 + consumer.getClass().getName() + ")" );
257 private void doConversion( CommandLine cli, PlexusContainer plexus )
258 throws ComponentLookupException
260 LegacyRepositoryConverter legacyRepositoryConverter = (LegacyRepositoryConverter) plexus
261 .lookup( LegacyRepositoryConverter.ROLE );
263 Properties p = new Properties();
267 p.load( new FileInputStream( cli.getOptionValue( CONVERT ) ) );
269 catch ( IOException e )
271 showFatalError( "Cannot find properties file which describes the conversion.", e, true );
274 File oldRepositoryPath = new File( p.getProperty( SOURCE_REPO_PATH ) );
276 File newRepositoryPath = new File( p.getProperty( TARGET_REPO_PATH ) );
278 System.out.println( "Converting " + oldRepositoryPath + " to " + newRepositoryPath );
280 List fileExclusionPatterns = null;
282 String s = p.getProperty( BLACKLISTED_PATTERNS );
286 fileExclusionPatterns = Arrays.asList( StringUtils.split( s, "," ) );
291 legacyRepositoryConverter.convertLegacyRepository( oldRepositoryPath, newRepositoryPath,
292 fileExclusionPatterns, true );
294 catch ( RepositoryConversionException e )
296 showFatalError( "Error converting repository.", e, true );
300 private void dumpConfiguration( PlexusContainer plexus ) throws ComponentLookupException
302 archivaConfiguration = (ArchivaConfiguration) plexus.lookup( ArchivaConfiguration.ROLE );
304 System.out.println( "File Type Count: " + archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().size() );