Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ArchivaCli.java 11KB

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