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

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