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.consumers.ConsumerException;
28 import org.apache.maven.archiva.consumers.RepositoryContentConsumer;
29 import org.apache.maven.archiva.converter.RepositoryConversionException;
30 import org.apache.maven.archiva.converter.legacy.LegacyRepositoryConverter;
31 import org.apache.maven.archiva.model.ArchivaRepository;
32 import org.apache.maven.archiva.model.RepositoryContentStatistics;
33 import org.apache.maven.archiva.repository.RepositoryException;
34 import org.apache.maven.archiva.repository.scanner.RepositoryScanner;
35 import org.codehaus.plexus.PlexusContainer;
36 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
37 import org.codehaus.plexus.tools.cli.AbstractCli;
40 import java.io.FileInputStream;
41 import java.io.IOException;
42 import java.text.SimpleDateFormat;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collection;
46 import java.util.Iterator;
47 import java.util.List;
49 import java.util.Properties;
54 * @author Jason van Zyl
55 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
58 public class ArchivaCli
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 public static final char CONVERT = 'c';
67 public static final char SCAN = 's';
69 public static final char CONSUMERS = 'u';
71 public static final char LIST_CONSUMERS = 'l';
73 // ----------------------------------------------------------------------------
74 // Properties controlling Repository conversion
75 // ----------------------------------------------------------------------------
77 public static final String SOURCE_REPO_PATH = "sourceRepositoryPath";
79 public static final String TARGET_REPO_PATH = "targetRepositoryPath";
81 public static final String BLACKLISTED_PATTERNS = "blacklistPatterns";
83 public static void main( String[] args )
86 new ArchivaCli().execute( args );
89 public String getPomPropertiesPath()
91 return "META-INF/maven/org.apache.maven.archiva/archiva-cli/pom.properties";
94 private Option createOption( char shortOpt, String longOpt, int argCount, String description )
96 boolean hasArg = ( argCount > 0 );
97 Option opt = new Option( String.valueOf( shortOpt ), hasArg, description );
98 opt.setLongOpt( longOpt );
101 opt.setArgs( argCount );
106 public Options buildCliOptions( Options options )
108 Option convertOption = createOption( CONVERT, "convert", 1, "Convert a legacy Maven 1.x repository to a "
109 + "Maven 2.x repository using a properties file to describe the conversion." );
110 convertOption.setArgName( "conversion.properties" );
111 options.addOption( convertOption );
113 Option scanOption = createOption( SCAN, "scan", 1, "Scan the specified repository." );
114 scanOption.setArgName( "repository directory" );
115 options.addOption( scanOption );
117 Option consumerOption = createOption( CONSUMERS, "consumers", 1, "The consumers to use. "
118 + "(comma delimited. default: 'count-artifacts')" );
119 consumerOption.setArgName( "consumer list" );
120 options.addOption( consumerOption );
122 Option listConsumersOption = createOption( LIST_CONSUMERS, "listconsumers", 0, "List available consumers." );
123 options.addOption( listConsumersOption );
128 public void invokePlexusComponent( CommandLine cli, PlexusContainer plexus )
131 if ( cli.hasOption( CONVERT ) )
133 doConversion( cli, plexus );
135 else if ( cli.hasOption( SCAN ) )
137 doScan( cli, plexus );
139 else if ( cli.hasOption( LIST_CONSUMERS ) )
141 dumpAvailableConsumers( plexus );
149 private void doScan( CommandLine cli, PlexusContainer plexus )
150 throws ConsumerException, ComponentLookupException
152 String path = cli.getOptionValue( SCAN );
154 ArchivaRepository repo = new ArchivaRepository( "cliRepo", "Archiva CLI Provided Repo", "file://" + path );
156 List consumerList = new ArrayList();
158 consumerList.addAll( getConsumerList( cli, plexus ) );
160 RepositoryScanner scanner = new RepositoryScanner();
164 RepositoryContentStatistics stats = scanner.scan( repo, consumerList, true );
166 SimpleDateFormat df = new SimpleDateFormat();
167 System.out.println( "" );
168 System.out.println( ".\\ Scan of " + repo.getId() + " \\.__________________________________________" );
169 System.out.println( " Repository URL : " + repo.getUrl() );
170 System.out.println( " Repository Name : " + repo.getModel().getName() );
171 System.out.println( " Repository Layout : " + repo.getModel().getLayoutName() );
172 System.out.println( " Consumers : (" + consumerList.size() + " active)" );
173 for ( Iterator iter = consumerList.iterator(); iter.hasNext(); )
175 RepositoryContentConsumer consumer = (RepositoryContentConsumer) iter.next();
176 System.out.println( " " + consumer.getId() + " - " + consumer.getDescription() );
178 System.out.println( " Duration : " + DateUtil.getDuration( stats.getDuration() ) );
179 System.out.println( " When Gathered : " + df.format( stats.getWhenGathered() ) );
180 System.out.println( " Total File Count : " + stats.getTotalFileCount() );
181 long averageMsPerFile = ( stats.getDuration() / stats.getTotalFileCount() );
182 System.out.println( " Avg Time Per File : " + DateUtil.getDuration( averageMsPerFile ) );
183 System.out.println( "______________________________________________________________" );
185 catch ( RepositoryException e )
187 e.printStackTrace( System.err );
191 private Collection getConsumerList( CommandLine cli, PlexusContainer plexus )
192 throws ComponentLookupException, ConsumerException
194 String specifiedConsumers = "count-artifacts";
196 if ( cli.hasOption( CONSUMERS ) )
198 specifiedConsumers = cli.getOptionValue( CONSUMERS );
201 List consumerList = new ArrayList();
203 Map availableConsumers = plexus.lookupMap( RepositoryContentConsumer.class );
205 String consumerArray[] = StringUtils.split( specifiedConsumers, ',' );
207 for ( int i = 0; i < consumerArray.length; i++ )
209 String specifiedConsumer = consumerArray[i];
210 if ( !availableConsumers.containsKey( specifiedConsumer ) )
212 System.err.println( "Specified consumer [" + specifiedConsumer + "] not found." );
213 dumpAvailableConsumers( plexus );
217 consumerList.add( availableConsumers.get( specifiedConsumer ) );
223 private void dumpAvailableConsumers( PlexusContainer plexus )
224 throws ComponentLookupException
226 Map availableConsumers = plexus.lookupMap( RepositoryContentConsumer.class );
228 System.out.println( ".\\ Available Consumer List \\.______________________________" );
230 for ( Iterator iter = availableConsumers.entrySet().iterator(); iter.hasNext(); )
232 Map.Entry entry = (Map.Entry) iter.next();
233 String consumerHint = (String) entry.getKey();
234 RepositoryContentConsumer consumer = (RepositoryContentConsumer) entry.getValue();
235 System.out.println( " " + consumerHint + ": " + consumer.getDescription() + " ("
236 + consumer.getClass().getName() + ")" );
240 private void doConversion( CommandLine cli, PlexusContainer plexus )
241 throws ComponentLookupException
243 LegacyRepositoryConverter legacyRepositoryConverter = (LegacyRepositoryConverter) plexus
244 .lookup( LegacyRepositoryConverter.ROLE );
246 Properties p = new Properties();
250 p.load( new FileInputStream( cli.getOptionValue( CONVERT ) ) );
252 catch ( IOException e )
254 showFatalError( "Cannot find properties file which describes the conversion.", e, true );
257 File oldRepositoryPath = new File( p.getProperty( SOURCE_REPO_PATH ) );
259 File newRepositoryPath = new File( p.getProperty( TARGET_REPO_PATH ) );
261 System.out.println( "Converting " + oldRepositoryPath + " to " + newRepositoryPath );
263 List fileExclusionPatterns = null;
265 String s = p.getProperty( BLACKLISTED_PATTERNS );
269 fileExclusionPatterns = Arrays.asList( StringUtils.split( s, "," ) );
274 legacyRepositoryConverter.convertLegacyRepository( oldRepositoryPath, newRepositoryPath,
275 fileExclusionPatterns, true );
277 catch ( RepositoryConversionException e )
279 showFatalError( "Error converting repository.", e, true );