You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaCli.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package org.apache.archiva.cli;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import com.sampullara.cli.Args;
  21. import com.sampullara.cli.Argument;
  22. import org.apache.archiva.consumers.ConsumerException;
  23. import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
  24. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  25. import org.apache.archiva.consumers.RepositoryContentConsumer;
  26. import org.apache.archiva.repository.base.managed.BasicManagedRepository;
  27. import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
  28. import org.apache.archiva.repository.scanner.RepositoryScanner;
  29. import org.apache.archiva.repository.scanner.RepositoryScannerException;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33. import org.springframework.context.support.ClassPathXmlApplicationContext;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.nio.file.Paths;
  37. import java.util.ArrayList;
  38. import java.util.Arrays;
  39. import java.util.Collections;
  40. import java.util.HashMap;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Properties;
  44. /**
  45. * ArchivaCli
  46. * <p>
  47. * TODO add back reading of archiva.xml from a given location
  48. */
  49. public class ArchivaCli
  50. {
  51. // ----------------------------------------------------------------------------
  52. // Properties controlling Repository conversion
  53. // ----------------------------------------------------------------------------
  54. public static final String SOURCE_REPO_PATH = "sourceRepositoryPath";
  55. public static final String TARGET_REPO_PATH = "targetRepositoryPath";
  56. public static final String BLACKLISTED_PATTERNS = "blacklistPatterns";
  57. public static final String POM_PROPERTIES = "/META-INF/maven/org.apache.archiva/archiva-cli/pom.properties";
  58. private static final Logger LOGGER = LoggerFactory.getLogger( ArchivaCli.class );
  59. private static String getVersion()
  60. throws IOException
  61. {
  62. try (InputStream pomStream = ArchivaCli.class.getResourceAsStream( POM_PROPERTIES ))
  63. {
  64. if ( pomStream == null )
  65. {
  66. throw new IOException( "Failed to load " + POM_PROPERTIES );
  67. }
  68. Properties properties = new Properties();
  69. properties.load( pomStream );
  70. return properties.getProperty( "version" );
  71. }
  72. }
  73. private ClassPathXmlApplicationContext applicationContext;
  74. public ArchivaCli()
  75. {
  76. applicationContext =
  77. new ClassPathXmlApplicationContext( new String[]{ "classpath*:/META-INF/spring-context.xml" } );
  78. }
  79. public static void main( String[] args )
  80. throws Exception
  81. {
  82. Commands command = new Commands();
  83. try
  84. {
  85. Args.parse( command, args );
  86. }
  87. catch ( IllegalArgumentException e )
  88. {
  89. LOGGER.error( e.getMessage(), e );
  90. Args.usage( command );
  91. return;
  92. }
  93. ArchivaCli cli = new ArchivaCli();
  94. try
  95. {
  96. cli.execute( command );
  97. }
  98. finally
  99. {
  100. cli.destroy();
  101. }
  102. }
  103. private void destroy()
  104. {
  105. applicationContext.destroy();
  106. }
  107. private void execute( Commands command )
  108. throws Exception
  109. {
  110. if ( command.help )
  111. {
  112. Args.usage( command );
  113. }
  114. else if ( command.version )
  115. {
  116. LOGGER.info( "Version: {}", getVersion() );
  117. }
  118. else if ( command.convert )
  119. {
  120. LOGGER.error( "Conversion is not available anymore" );
  121. }
  122. else if ( command.scan )
  123. {
  124. if ( command.repository == null )
  125. {
  126. LOGGER.error( "The repository must be specified." );
  127. Args.usage( command );
  128. return;
  129. }
  130. doScan( command.repository, command.consumers.split( "," ) );
  131. }
  132. else if ( command.listConsumers )
  133. {
  134. dumpAvailableConsumers();
  135. }
  136. else
  137. {
  138. Args.usage( command );
  139. }
  140. }
  141. private void doScan( String path, String[] consumers )
  142. throws ConsumerException, IOException
  143. {
  144. BasicManagedRepository repo = BasicManagedRepository.newFilesystemInstance( Paths.get(path).getFileName().toString(), "Archiva CLI Provided Repo", Paths.get(path));
  145. repo.setLocation( Paths.get(path).toUri() );
  146. List<KnownRepositoryContentConsumer> knownConsumerList = new ArrayList<>();
  147. knownConsumerList.addAll( getConsumerList( consumers ) );
  148. List<InvalidRepositoryContentConsumer> invalidConsumerList = Collections.emptyList();
  149. List<String> ignoredContent = new ArrayList<>();
  150. ignoredContent.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) );
  151. RepositoryScanner scanner = applicationContext.getBean( RepositoryScanner.class );
  152. try
  153. {
  154. RepositoryScanStatistics stats = scanner.scan( repo, knownConsumerList, invalidConsumerList, ignoredContent,
  155. RepositoryScanner.FRESH_SCAN );
  156. LOGGER.info( stats.toDump( repo ) );
  157. }
  158. catch ( RepositoryScannerException e )
  159. {
  160. LOGGER.error( e.getMessage(), e );
  161. }
  162. }
  163. private List<KnownRepositoryContentConsumer> getConsumerList( String[] consumers )
  164. throws ConsumerException
  165. {
  166. List<KnownRepositoryContentConsumer> consumerList = new ArrayList<>();
  167. Map<String, KnownRepositoryContentConsumer> availableConsumers = getConsumers();
  168. for ( String specifiedConsumer : consumers )
  169. {
  170. if ( !availableConsumers.containsKey( specifiedConsumer ) )
  171. {
  172. LOGGER.error( "Specified consumer [{}] not found.", specifiedConsumer );
  173. dumpAvailableConsumers();
  174. System.exit( 1 );
  175. }
  176. consumerList.add( availableConsumers.get( specifiedConsumer ) );
  177. }
  178. return consumerList;
  179. }
  180. private void dumpAvailableConsumers()
  181. {
  182. Map<String, KnownRepositoryContentConsumer> availableConsumers = getConsumers();
  183. LOGGER.info( ".\\ Available Consumer List \\.______________________________" );
  184. for ( Map.Entry<String, KnownRepositoryContentConsumer> entry : availableConsumers.entrySet() )
  185. {
  186. String consumerHint = entry.getKey();
  187. RepositoryContentConsumer consumer = entry.getValue();
  188. LOGGER.info( " {} : {} ({})", //
  189. consumerHint, consumer.getDescription(), consumer.getClass().getName() );
  190. }
  191. }
  192. @SuppressWarnings( "unchecked" )
  193. private Map<String, KnownRepositoryContentConsumer> getConsumers()
  194. {
  195. Map<String, KnownRepositoryContentConsumer> beans =
  196. applicationContext.getBeansOfType( KnownRepositoryContentConsumer.class );
  197. // we use a naming conventions knownRepositoryContentConsumer#hint
  198. // with plexus we used only hint so remove before#
  199. Map<String, KnownRepositoryContentConsumer> smallNames = new HashMap<>( beans.size() );
  200. for ( Map.Entry<String, KnownRepositoryContentConsumer> entry : beans.entrySet() )
  201. {
  202. smallNames.put( StringUtils.substringAfterLast( entry.getKey(), "#" ), entry.getValue() );
  203. }
  204. return smallNames;
  205. }
  206. private static class Commands
  207. {
  208. @Argument( description = "Display help information", value = "help", alias = "h" )
  209. private boolean help;
  210. @Argument( description = "Display version information", value = "version", alias = "v" )
  211. private boolean version;
  212. @Argument( description = "List available consumers", value = "listconsumers", alias = "l" )
  213. private boolean listConsumers;
  214. @Argument( description = "The consumers to use (comma delimited)", value = "consumers", alias = "u" )
  215. private String consumers = "count-artifacts";
  216. @Argument( description = "Scan the specified repository", value = "scan", alias = "s" )
  217. private boolean scan;
  218. @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" )
  219. private boolean convert;
  220. @Argument( description = "The properties file for the conversion", value = "properties" )
  221. private String properties = "conversion.properties";
  222. @Argument( description = "The repository to scan", value = "repository" )
  223. private String repository;
  224. }
  225. }