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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.converter.RepositoryConversionException;
  27. import org.apache.archiva.converter.legacy.LegacyRepositoryConverter;
  28. import org.apache.archiva.repository.base.BasicManagedRepository;
  29. import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
  30. import org.apache.archiva.repository.scanner.RepositoryScanner;
  31. import org.apache.archiva.repository.scanner.RepositoryScannerException;
  32. import org.apache.commons.lang3.StringUtils;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. import org.springframework.context.support.ClassPathXmlApplicationContext;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.nio.file.Files;
  39. import java.nio.file.Path;
  40. import java.nio.file.Paths;
  41. import java.util.ArrayList;
  42. import java.util.Arrays;
  43. import java.util.Collections;
  44. import java.util.HashMap;
  45. import java.util.List;
  46. import java.util.Map;
  47. import java.util.Properties;
  48. /**
  49. * ArchivaCli
  50. * <p>
  51. * TODO add back reading of archiva.xml from a given location
  52. */
  53. public class ArchivaCli
  54. {
  55. // ----------------------------------------------------------------------------
  56. // Properties controlling Repository conversion
  57. // ----------------------------------------------------------------------------
  58. public static final String SOURCE_REPO_PATH = "sourceRepositoryPath";
  59. public static final String TARGET_REPO_PATH = "targetRepositoryPath";
  60. public static final String BLACKLISTED_PATTERNS = "blacklistPatterns";
  61. public static final String POM_PROPERTIES = "/META-INF/maven/org.apache.archiva/archiva-cli/pom.properties";
  62. private static final Logger LOGGER = LoggerFactory.getLogger( ArchivaCli.class );
  63. private static String getVersion()
  64. throws IOException
  65. {
  66. try (InputStream pomStream = ArchivaCli.class.getResourceAsStream( POM_PROPERTIES ))
  67. {
  68. if ( pomStream == null )
  69. {
  70. throw new IOException( "Failed to load " + POM_PROPERTIES );
  71. }
  72. Properties properties = new Properties();
  73. properties.load( pomStream );
  74. return properties.getProperty( "version" );
  75. }
  76. }
  77. private ClassPathXmlApplicationContext applicationContext;
  78. public ArchivaCli()
  79. {
  80. applicationContext =
  81. new ClassPathXmlApplicationContext( new String[]{ "classpath*:/META-INF/spring-context.xml" } );
  82. }
  83. public static void main( String[] args )
  84. throws Exception
  85. {
  86. Commands command = new Commands();
  87. try
  88. {
  89. Args.parse( command, args );
  90. }
  91. catch ( IllegalArgumentException e )
  92. {
  93. LOGGER.error( e.getMessage(), e );
  94. Args.usage( command );
  95. return;
  96. }
  97. ArchivaCli cli = new ArchivaCli();
  98. try
  99. {
  100. cli.execute( command );
  101. }
  102. finally
  103. {
  104. cli.destroy();
  105. }
  106. }
  107. private void destroy()
  108. {
  109. applicationContext.destroy();
  110. }
  111. private void execute( Commands command )
  112. throws Exception
  113. {
  114. if ( command.help )
  115. {
  116. Args.usage( command );
  117. }
  118. else if ( command.version )
  119. {
  120. LOGGER.info( "Version: {}", getVersion() );
  121. }
  122. else if ( command.convert )
  123. {
  124. doConversion( command.properties );
  125. }
  126. else if ( command.scan )
  127. {
  128. if ( command.repository == null )
  129. {
  130. LOGGER.error( "The repository must be specified." );
  131. Args.usage( command );
  132. return;
  133. }
  134. doScan( command.repository, command.consumers.split( "," ) );
  135. }
  136. else if ( command.listConsumers )
  137. {
  138. dumpAvailableConsumers();
  139. }
  140. else
  141. {
  142. Args.usage( command );
  143. }
  144. }
  145. private void doScan( String path, String[] consumers )
  146. throws ConsumerException, IOException
  147. {
  148. BasicManagedRepository repo = BasicManagedRepository.newFilesystemInstance( Paths.get(path).getFileName().toString(), "Archiva CLI Provided Repo", Paths.get(path));
  149. repo.setLocation( Paths.get(path).toUri() );
  150. List<KnownRepositoryContentConsumer> knownConsumerList = new ArrayList<>();
  151. knownConsumerList.addAll( getConsumerList( consumers ) );
  152. List<InvalidRepositoryContentConsumer> invalidConsumerList = Collections.emptyList();
  153. List<String> ignoredContent = new ArrayList<>();
  154. ignoredContent.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) );
  155. RepositoryScanner scanner = applicationContext.getBean( RepositoryScanner.class );
  156. try
  157. {
  158. RepositoryScanStatistics stats = scanner.scan( repo, knownConsumerList, invalidConsumerList, ignoredContent,
  159. RepositoryScanner.FRESH_SCAN );
  160. LOGGER.info( stats.toDump( repo ) );
  161. }
  162. catch ( RepositoryScannerException e )
  163. {
  164. LOGGER.error( e.getMessage(), e );
  165. }
  166. }
  167. private List<KnownRepositoryContentConsumer> getConsumerList( String[] consumers )
  168. throws ConsumerException
  169. {
  170. List<KnownRepositoryContentConsumer> consumerList = new ArrayList<>();
  171. Map<String, KnownRepositoryContentConsumer> availableConsumers = getConsumers();
  172. for ( String specifiedConsumer : consumers )
  173. {
  174. if ( !availableConsumers.containsKey( specifiedConsumer ) )
  175. {
  176. LOGGER.error( "Specified consumer [{}] not found.", specifiedConsumer );
  177. dumpAvailableConsumers();
  178. System.exit( 1 );
  179. }
  180. consumerList.add( availableConsumers.get( specifiedConsumer ) );
  181. }
  182. return consumerList;
  183. }
  184. private void dumpAvailableConsumers()
  185. {
  186. Map<String, KnownRepositoryContentConsumer> availableConsumers = getConsumers();
  187. LOGGER.info( ".\\ Available Consumer List \\.______________________________" );
  188. for ( Map.Entry<String, KnownRepositoryContentConsumer> entry : availableConsumers.entrySet() )
  189. {
  190. String consumerHint = entry.getKey();
  191. RepositoryContentConsumer consumer = entry.getValue();
  192. LOGGER.info( " {} : {} ({})", //
  193. consumerHint, consumer.getDescription(), consumer.getClass().getName() );
  194. }
  195. }
  196. @SuppressWarnings( "unchecked" )
  197. private Map<String, KnownRepositoryContentConsumer> getConsumers()
  198. {
  199. Map<String, KnownRepositoryContentConsumer> beans =
  200. applicationContext.getBeansOfType( KnownRepositoryContentConsumer.class );
  201. // we use a naming conventions knownRepositoryContentConsumer#hint
  202. // with plexus we used only hint so remove before#
  203. Map<String, KnownRepositoryContentConsumer> smallNames = new HashMap<>( beans.size() );
  204. for ( Map.Entry<String, KnownRepositoryContentConsumer> entry : beans.entrySet() )
  205. {
  206. smallNames.put( StringUtils.substringAfterLast( entry.getKey(), "#" ), entry.getValue() );
  207. }
  208. return smallNames;
  209. }
  210. private void doConversion( String properties )
  211. throws IOException, RepositoryConversionException
  212. {
  213. LegacyRepositoryConverter legacyRepositoryConverter =
  214. applicationContext.getBean( LegacyRepositoryConverter.class );
  215. Properties p = new Properties();
  216. try (InputStream fis = Files.newInputStream( Paths.get( properties ) ))
  217. {
  218. p.load( fis );
  219. }
  220. Path oldRepositoryPath = Paths.get( p.getProperty( SOURCE_REPO_PATH ) );
  221. Path newRepositoryPath = Paths.get( p.getProperty( TARGET_REPO_PATH ) );
  222. LOGGER.info( "Converting {} to {}", oldRepositoryPath, newRepositoryPath );
  223. List<String> fileExclusionPatterns = null;
  224. String s = p.getProperty( BLACKLISTED_PATTERNS );
  225. if ( s != null )
  226. {
  227. fileExclusionPatterns = Arrays.asList( StringUtils.split( s, "," ) );
  228. }
  229. legacyRepositoryConverter.convertLegacyRepository( oldRepositoryPath, newRepositoryPath,
  230. fileExclusionPatterns );
  231. }
  232. private static class Commands
  233. {
  234. @Argument( description = "Display help information", value = "help", alias = "h" )
  235. private boolean help;
  236. @Argument( description = "Display version information", value = "version", alias = "v" )
  237. private boolean version;
  238. @Argument( description = "List available consumers", value = "listconsumers", alias = "l" )
  239. private boolean listConsumers;
  240. @Argument( description = "The consumers to use (comma delimited)", value = "consumers", alias = "u" )
  241. private String consumers = "count-artifacts";
  242. @Argument( description = "Scan the specified repository", value = "scan", alias = "s" )
  243. private boolean scan;
  244. @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" )
  245. private boolean convert;
  246. @Argument( description = "The properties file for the conversion", value = "properties" )
  247. private String properties = "conversion.properties";
  248. @Argument( description = "The repository to scan", value = "repository" )
  249. private String repository;
  250. }
  251. }