From 28d9a2384a42236eef4274e163335ff3b32bbe0b Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Mon, 7 Apr 2008 13:13:06 +0000 Subject: [PATCH] refreshed the CLI git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@645493 13f79535-47bb-0310-9956-ffa450edef68 --- archiva-cli/pom.xml | 82 +++--- .../main/assembly/archiva-cli-assembly.xml | 4 + .../apache/maven/archiva/cli/ArchivaCli.java | 250 ++++++++---------- .../archiva/cli/ArtifactCountConsumer.java | 8 +- .../archiva/cli/ProjectReaderConsumer.java | 14 +- .../resources/META-INF/plexus/components.xml | 50 ---- .../src/main/resources/log4j.properties | 4 +- 7 files changed, 174 insertions(+), 238 deletions(-) delete mode 100644 archiva-cli/src/main/resources/META-INF/plexus/components.xml diff --git a/archiva-cli/pom.xml b/archiva-cli/pom.xml index cf746ec41..f2101ed79 100644 --- a/archiva-cli/pom.xml +++ b/archiva-cli/pom.xml @@ -35,44 +35,63 @@ org.apache.archiva - archiva-core-consumers + archiva-dependency-tree-consumer + 1.1-SNAPSHOT + runtime org.apache.archiva - archiva-converter - - - org.codehaus.plexus - plexus-digest - - - org.codehaus.plexus - plexus-component-api - - - org.codehaus.plexus - plexus-container-default + archiva-xml-tools - org.codehaus.plexus - plexus-cli + org.apache.archiva + archiva-converter - commons-lang - commons-lang + com.google.code.cli-parser + cli + 7 - org.codehaus.plexus.registry - plexus-registry-commons + org.slf4j + slf4j-log4j12 + 1.5.0 + runtime - maven-assembly-plugin + org.apache.maven.plugins + maven-shade-plugin + 1.0.1 + + + package + + shade + + + apache-archiva-cli-${project.version} + + + + + + xml-apis:xml-apis + xalan:xalan + commons-beanutils:commons-beanutils + + + + + + + + org.apache.maven.plugins + maven-jar-plugin 2.1 - src/main/assembly/archiva-cli-assembly.xml org.apache.maven.archiva.cli.ArchivaCli @@ -80,25 +99,6 @@ - - org.codehaus.plexus - plexus-maven-plugin - 1.3.5 - - - merge - - merge-descriptors - - - - ${basedir}/src/main/resources/META-INF/plexus/components.xml - ${project.build.directory}/generated-resources/plexus/META-INF/plexus/components.xml - - - - - diff --git a/archiva-cli/src/main/assembly/archiva-cli-assembly.xml b/archiva-cli/src/main/assembly/archiva-cli-assembly.xml index 187b457ac..7b81464b4 100644 --- a/archiva-cli/src/main/assembly/archiva-cli-assembly.xml +++ b/archiva-cli/src/main/assembly/archiva-cli-assembly.xml @@ -38,6 +38,10 @@ / true runtime + + xml-apis:xml-apis + xalan:xalan + diff --git a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArchivaCli.java b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArchivaCli.java index 815747701..c2e98049e 100644 --- a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArchivaCli.java +++ b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArchivaCli.java @@ -19,59 +19,48 @@ package org.apache.maven.archiva.cli; * under the License. */ -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; import org.apache.commons.lang.StringUtils; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.consumers.ConsumerException; +import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer; +import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; import org.apache.maven.archiva.consumers.RepositoryContentConsumer; import org.apache.maven.archiva.converter.RepositoryConversionException; import org.apache.maven.archiva.converter.legacy.LegacyRepositoryConverter; import org.apache.maven.archiva.repository.RepositoryException; import org.apache.maven.archiva.repository.scanner.RepositoryScanStatistics; import org.apache.maven.archiva.repository.scanner.RepositoryScanner; -import org.codehaus.plexus.PlexusContainer; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.codehaus.plexus.tools.cli.AbstractCli; +import org.codehaus.plexus.spring.PlexusClassPathXmlApplicationContext; +import org.codehaus.plexus.spring.PlexusToSpringUtils; + +import com.sampullara.cli.Args; +import com.sampullara.cli.Argument; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; /** - * ArchivaCli - * + * ArchivaCli + * + * @todo add back reading of archiva.xml from a given location * @author Jason van Zyl * @author Joakim Erdfelt * @version $Id$ */ public class ArchivaCli - extends AbstractCli { - // ---------------------------------------------------------------------------- - // Options - // ---------------------------------------------------------------------------- - - public static final char CONVERT = 'c'; - - public static final char SCAN = 's'; - - public static final char CONSUMERS = 'u'; - - public static final char LIST_CONSUMERS = 'l'; - - public static final char DUMP_CONFIGURATION = 'd'; - // ---------------------------------------------------------------------------- // Properties controlling Repository conversion // ---------------------------------------------------------------------------- @@ -82,111 +71,102 @@ public class ArchivaCli public static final String BLACKLISTED_PATTERNS = "blacklistPatterns"; - /** - * Configuration store. - * - * @plexus.requirement - */ - private ArchivaConfiguration archivaConfiguration; - - public static void main( String[] args ) - throws Exception + private static String getVersion() + throws IOException { - new ArchivaCli().execute( args ); + Properties properties = new Properties(); + properties.load( ArchivaCli.class.getResourceAsStream( "/META-INF/maven/org.apache.archiva/archiva-cli/pom.properties" ) ); + return properties.getProperty( "version" ); } - public String getPomPropertiesPath() - { - return "META-INF/maven/org.apache.maven.archiva/archiva-cli/pom.properties"; - } + private PlexusClassPathXmlApplicationContext applicationContext; - private Option createOption( char shortOpt, String longOpt, int argCount, String description ) + public ArchivaCli() { - boolean hasArg = ( argCount > 0 ); - Option opt = new Option( String.valueOf( shortOpt ), hasArg, description ); - opt.setLongOpt( longOpt ); - if ( hasArg ) - { - opt.setArgs( argCount ); - } - return opt; + applicationContext = + new PlexusClassPathXmlApplicationContext( new String[] { "classpath*:/META-INF/spring-context.xml", + "classpath*:/META-INF/plexus/components.xml" } ); } - public Options buildCliOptions( Options options ) + public static void main( String[] args ) + throws Exception { - Option convertOption = createOption( CONVERT, "convert", 1, "Convert a legacy Maven 1.x repository to a " - + "Maven 2.x repository using a properties file to describe the conversion." ); - convertOption.setArgName( "conversion.properties" ); - options.addOption( convertOption ); - - Option scanOption = createOption( SCAN, "scan", 1, "Scan the specified repository." ); - scanOption.setArgName( "repository directory" ); - options.addOption( scanOption ); - - Option consumerOption = createOption( CONSUMERS, "consumers", 1, "The consumers to use. " - + "(comma delimited. default: 'count-artifacts')" ); - consumerOption.setArgName( "consumer list" ); - options.addOption( consumerOption ); - - Option listConsumersOption = createOption( LIST_CONSUMERS, "listconsumers", 0, "List available consumers." ); - options.addOption( listConsumersOption ); + Commands command = new Commands(); - Option dumpConfig = createOption( DUMP_CONFIGURATION, "dumpconfig", 0, "Dump Current Configuration." ); - options.addOption( dumpConfig ); + try + { + Args.parse( command, args ); + } + catch ( IllegalArgumentException e ) + { + System.err.println( e.getMessage() ); + Args.usage( command ); + return; + } - return options; + new ArchivaCli().execute( command ); } - public void invokePlexusComponent( CommandLine cli, PlexusContainer plexus ) + private void execute( Commands command ) throws Exception { - if ( cli.hasOption( CONVERT ) ) + if ( command.help ) + { + Args.usage( command ); + } + else if ( command.version ) { - doConversion( cli, plexus ); + System.out.print( "Version: " + getVersion() ); } - else if ( cli.hasOption( SCAN ) ) + else if ( command.convert ) { - doScan( cli, plexus ); + doConversion( command.properties ); } - else if ( cli.hasOption( LIST_CONSUMERS ) ) + else if ( command.scan ) { - dumpAvailableConsumers( plexus ); + if ( command.repository == null ) + { + System.err.println( "The repository must be specified." ); + Args.usage( command ); + return; + } + + doScan( command.repository, command.consumers.split( "," ) ); } - else if ( cli.hasOption( DUMP_CONFIGURATION ) ) + else if ( command.listConsumers ) { - dumpConfiguration( plexus ); + dumpAvailableConsumers(); } else { - displayHelp(); + Args.usage( command ); } } - private void doScan( CommandLine cli, PlexusContainer plexus ) - throws ConsumerException, ComponentLookupException + private void doScan( String path, String[] consumers ) + throws ConsumerException { - String path = cli.getOptionValue( SCAN ); - ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration(); repo.setId( "cliRepo" ); repo.setName( "Archiva CLI Provided Repo" ); repo.setLocation( path ); - List knownConsumerList = new ArrayList(); + List knownConsumerList = new ArrayList(); - knownConsumerList.addAll( getConsumerList( cli, plexus ) ); + knownConsumerList.addAll( getConsumerList( consumers ) ); - List invalidConsumerList = Collections.EMPTY_LIST; + List invalidConsumerList = Collections.emptyList(); - List ignoredContent = new ArrayList(); + List ignoredContent = new ArrayList(); ignoredContent.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) ); - RepositoryScanner scanner = (RepositoryScanner) plexus.lookup( RepositoryScanner.class ); + RepositoryScanner scanner = (RepositoryScanner) lookup( RepositoryScanner.class ); try { - RepositoryScanStatistics stats = scanner.scan( repo, knownConsumerList, invalidConsumerList, - ignoredContent, RepositoryScanner.FRESH_SCAN ); + RepositoryScanStatistics stats = + scanner.scan( repo, knownConsumerList, invalidConsumerList, ignoredContent, + RepositoryScanner.FRESH_SCAN ); System.out.println( "\n" + stats.toDump( repo ) ); } @@ -196,29 +176,24 @@ public class ArchivaCli } } - private Collection getConsumerList( CommandLine cli, PlexusContainer plexus ) - throws ComponentLookupException, ConsumerException + private Object lookup( Class clazz ) { - String specifiedConsumers = "count-artifacts"; - - if ( cli.hasOption( CONSUMERS ) ) - { - specifiedConsumers = cli.getOptionValue( CONSUMERS ); - } - - List consumerList = new ArrayList(); + return applicationContext.getBean( PlexusToSpringUtils.buildSpringId( clazz.getName(), null ) ); + } - Map availableConsumers = plexus.lookupMap( RepositoryContentConsumer.class ); + private List getConsumerList( String[] consumers ) + throws ConsumerException + { + List consumerList = new ArrayList(); - String consumerArray[] = StringUtils.split( specifiedConsumers, ',' ); + Map availableConsumers = getConsumers(); - for ( int i = 0; i < consumerArray.length; i++ ) + for ( String specifiedConsumer : consumers ) { - String specifiedConsumer = consumerArray[i]; if ( !availableConsumers.containsKey( specifiedConsumer ) ) { System.err.println( "Specified consumer [" + specifiedConsumer + "] not found." ); - dumpAvailableConsumers( plexus ); + dumpAvailableConsumers(); System.exit( 1 ); } @@ -228,16 +203,14 @@ public class ArchivaCli return consumerList; } - private void dumpAvailableConsumers( PlexusContainer plexus ) - throws ComponentLookupException + private void dumpAvailableConsumers() { - Map availableConsumers = plexus.lookupMap( RepositoryContentConsumer.class ); + Map availableConsumers = getConsumers(); System.out.println( ".\\ Available Consumer List \\.______________________________" ); - for ( Iterator iter = availableConsumers.entrySet().iterator(); iter.hasNext(); ) + for ( Map.Entry entry : availableConsumers.entrySet() ) { - Map.Entry entry = (Map.Entry) iter.next(); String consumerHint = (String) entry.getKey(); RepositoryContentConsumer consumer = (RepositoryContentConsumer) entry.getValue(); System.out.println( " " + consumerHint + ": " + consumer.getDescription() + " (" @@ -245,22 +218,21 @@ public class ArchivaCli } } - private void doConversion( CommandLine cli, PlexusContainer plexus ) - throws ComponentLookupException + @SuppressWarnings("unchecked") + private Map getConsumers() { - LegacyRepositoryConverter legacyRepositoryConverter = (LegacyRepositoryConverter) plexus - .lookup( LegacyRepositoryConverter.ROLE ); + return PlexusToSpringUtils.lookupMap( "knownRepositoryContentConsumer", applicationContext ); + } + + private void doConversion( String properties ) + throws FileNotFoundException, IOException, RepositoryConversionException + { + LegacyRepositoryConverter legacyRepositoryConverter = + (LegacyRepositoryConverter) lookup( LegacyRepositoryConverter.class ); Properties p = new Properties(); - try - { - p.load( new FileInputStream( cli.getOptionValue( CONVERT ) ) ); - } - catch ( IOException e ) - { - showFatalError( "Cannot find properties file which describes the conversion.", e, true ); - } + p.load( new FileInputStream( properties ) ); File oldRepositoryPath = new File( p.getProperty( SOURCE_REPO_PATH ) ); @@ -268,7 +240,7 @@ public class ArchivaCli System.out.println( "Converting " + oldRepositoryPath + " to " + newRepositoryPath ); - List fileExclusionPatterns = null; + List fileExclusionPatterns = null; String s = p.getProperty( BLACKLISTED_PATTERNS ); @@ -277,23 +249,33 @@ public class ArchivaCli fileExclusionPatterns = Arrays.asList( StringUtils.split( s, "," ) ); } - try - { - legacyRepositoryConverter.convertLegacyRepository( oldRepositoryPath, newRepositoryPath, - fileExclusionPatterns ); - } - catch ( RepositoryConversionException e ) - { - showFatalError( "Error converting repository.", e, true ); - } + legacyRepositoryConverter.convertLegacyRepository( oldRepositoryPath, newRepositoryPath, fileExclusionPatterns ); } - private void dumpConfiguration( PlexusContainer plexus ) - throws ComponentLookupException + private static class Commands { - archivaConfiguration = (ArchivaConfiguration) plexus.lookup( ArchivaConfiguration.ROLE, "cli" ); + @Argument( description = "Display help information", value = "help", alias = "h" ) + private boolean help; + + @Argument( description = "Display version information", value = "version", alias = "v" ) + private boolean version; + + @Argument( description = "List available consumers", value = "listconsumers", alias = "l" ) + private boolean listConsumers; + + @Argument( description = "The consumers to use (comma delimited)", value = "consumers", alias = "u" ) + private String consumers = "count-artifacts"; + + @Argument( description = "Scan the specified repository", value = "scan", alias = "s" ) + private boolean scan; + + @Argument( description = "Convert a legacy Maven 1.x repository to a Maven 2.x repository using a properties file to describe the conversion", value = "convert", alias = "c" ) + private boolean convert; + + @Argument( description = "The properties file for the converstion", value = "properties" ) + private String properties = "conversion.properties"; - System.out.println( "File Type Count: " - + archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().size() ); + @Argument( description = "The repository to scan", value = "repository" ) + private String repository; } } diff --git a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArtifactCountConsumer.java b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArtifactCountConsumer.java index 7c9aa368f..37fc78020 100644 --- a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArtifactCountConsumer.java +++ b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ArtifactCountConsumer.java @@ -19,24 +19,24 @@ package org.apache.maven.archiva.cli; * under the License. */ -import org.apache.maven.archiva.consumers.RepositoryContentConsumer; - import java.util.ArrayList; import java.util.List; +import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; + /** * ArtifactCountConsumer * * @author Joakim Erdfelt * @version $Id$ * - * @plexus.component role="org.apache.maven.archiva.consumers.RepositoryContentConsumer" + * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer" * role-hint="count-artifacts" * instantiation-strategy="per-lookup" */ public class ArtifactCountConsumer extends AbstractProgressConsumer - implements RepositoryContentConsumer + implements KnownRepositoryContentConsumer { /** * @plexus.configuration default-value="count-artifacts" diff --git a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ProjectReaderConsumer.java b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ProjectReaderConsumer.java index 1a6511270..04f52768d 100644 --- a/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ProjectReaderConsumer.java +++ b/archiva-cli/src/main/java/org/apache/maven/archiva/cli/ProjectReaderConsumer.java @@ -19,31 +19,31 @@ package org.apache.maven.archiva.cli; * under the License. */ +import java.io.File; +import java.util.ArrayList; +import java.util.List; + import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.consumers.ConsumerException; -import org.apache.maven.archiva.consumers.RepositoryContentConsumer; +import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; import org.apache.maven.archiva.model.ArchivaProjectModel; import org.apache.maven.archiva.repository.project.ProjectModelException; import org.apache.maven.archiva.repository.project.ProjectModelReader; import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader; -import java.io.File; -import java.util.ArrayList; -import java.util.List; - /** * ProjectReaderConsumer * * @author Joakim Erdfelt * @version $Id$ * - * @plexus.component role="org.apache.maven.archiva.consumers.RepositoryContentConsumer" + * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer" * role-hint="read-poms" * instantiation-strategy="per-lookup" */ public class ProjectReaderConsumer extends AbstractProgressConsumer - implements RepositoryContentConsumer + implements KnownRepositoryContentConsumer { /** * @plexus.configuration default-value="read-poms" diff --git a/archiva-cli/src/main/resources/META-INF/plexus/components.xml b/archiva-cli/src/main/resources/META-INF/plexus/components.xml deleted file mode 100644 index ad355c655..000000000 --- a/archiva-cli/src/main/resources/META-INF/plexus/components.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - org.codehaus.plexus.registry.Registry - - org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry - - commons-configuration - - - - - - - - - - - - \ No newline at end of file diff --git a/archiva-cli/src/main/resources/log4j.properties b/archiva-cli/src/main/resources/log4j.properties index 6d107570b..ff5b14c77 100644 --- a/archiva-cli/src/main/resources/log4j.properties +++ b/archiva-cli/src/main/resources/log4j.properties @@ -1,10 +1,10 @@ # Set root logger level to DEBUG and its only appender to A1. -log4j.rootLogger=INFO, A1 +log4j.rootLogger=ERROR, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout -log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n +log4j.appender.A1.layout.ConversionPattern=[%t] %-5p %c %x - %m%n -- 2.39.5