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

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.admin.model.beans.ManagedRepository;
  23. import org.apache.archiva.consumers.ConsumerException;
  24. import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
  25. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  26. import org.apache.archiva.consumers.RepositoryContentConsumer;
  27. import org.apache.archiva.converter.RepositoryConversionException;
  28. import org.apache.archiva.converter.legacy.LegacyRepositoryConverter;
  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, MalformedURLException
  148. {
  149. ManagedRepository repo = new ManagedRepository();
  150. repo.setId( Paths.get( path ).getFileName().toString());
  151. repo.setName( "Archiva CLI Provided Repo" );
  152. repo.setLocation( path );
  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. }