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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.common.plexusbridge.PlexusSisuBridge;
  24. import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
  25. import org.apache.archiva.consumers.ConsumerException;
  26. import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
  27. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  28. import org.apache.archiva.consumers.RepositoryContentConsumer;
  29. import org.apache.archiva.converter.RepositoryConversionException;
  30. import org.apache.archiva.converter.legacy.LegacyRepositoryConverter;
  31. import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
  32. import org.apache.archiva.repository.scanner.RepositoryScanner;
  33. import org.apache.archiva.repository.scanner.RepositoryScannerException;
  34. import org.apache.commons.lang.StringUtils;
  35. import org.apache.maven.artifact.manager.WagonManager;
  36. import org.springframework.context.support.ClassPathXmlApplicationContext;
  37. import java.io.File;
  38. import java.io.FileInputStream;
  39. import java.io.FileNotFoundException;
  40. import java.io.IOException;
  41. import java.io.InputStream;
  42. import java.net.MalformedURLException;
  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.Map;
  49. import java.util.Properties;
  50. /**
  51. * ArchivaCli
  52. *
  53. *
  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 String getVersion()
  66. throws IOException
  67. {
  68. try (InputStream pomStream = ArchivaCli.class.getResourceAsStream( POM_PROPERTIES ))
  69. {
  70. if ( pomStream == null )
  71. {
  72. throw new IOException( "Failed to load " + POM_PROPERTIES );
  73. }
  74. Properties properties = new Properties();
  75. properties.load( pomStream );
  76. return properties.getProperty( "version" );
  77. }
  78. }
  79. private ClassPathXmlApplicationContext applicationContext;
  80. public ArchivaCli()
  81. {
  82. applicationContext =
  83. new ClassPathXmlApplicationContext( new String[]{ "classpath*:/META-INF/spring-context.xml" } );
  84. }
  85. public static void main( String[] args )
  86. throws Exception
  87. {
  88. Commands command = new Commands();
  89. try
  90. {
  91. Args.parse( command, args );
  92. }
  93. catch ( IllegalArgumentException e )
  94. {
  95. System.err.println( e.getMessage() );
  96. Args.usage( command );
  97. return;
  98. }
  99. new ArchivaCli().execute( command );
  100. }
  101. private void execute( Commands command )
  102. throws Exception
  103. {
  104. if ( command.help )
  105. {
  106. Args.usage( command );
  107. }
  108. else if ( command.version )
  109. {
  110. System.out.print( "Version: " + getVersion() );
  111. }
  112. else if ( command.convert )
  113. {
  114. doConversion( command.properties );
  115. }
  116. else if ( command.scan )
  117. {
  118. if ( command.repository == null )
  119. {
  120. System.err.println( "The repository must be specified." );
  121. Args.usage( command );
  122. return;
  123. }
  124. doScan( command.repository, command.consumers.split( "," ) );
  125. }
  126. else if ( command.listConsumers )
  127. {
  128. dumpAvailableConsumers();
  129. }
  130. else
  131. {
  132. Args.usage( command );
  133. }
  134. }
  135. private void doScan( String path, String[] consumers )
  136. throws ConsumerException, MalformedURLException, PlexusSisuBridgeException
  137. {
  138. // hack around poorly configurable project builder by pointing all repositories back at this location to be self
  139. // contained
  140. WagonManager wagonManager = lookup( WagonManager.class );
  141. wagonManager.addMirror( "internal", "*", new File( path ).toURL().toExternalForm() );
  142. ManagedRepository repo = new ManagedRepository();
  143. repo.setId( "cliRepo" );
  144. repo.setName( "Archiva CLI Provided Repo" );
  145. repo.setLocation( path );
  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 = lookup( RepositoryScanner.class );
  152. try
  153. {
  154. RepositoryScanStatistics stats = scanner.scan( repo, knownConsumerList, invalidConsumerList, ignoredContent,
  155. RepositoryScanner.FRESH_SCAN );
  156. System.out.println( "\n" + stats.toDump( repo ) );
  157. }
  158. catch ( RepositoryScannerException e )
  159. {
  160. e.printStackTrace( System.err );
  161. }
  162. }
  163. private <T>T lookup( Class<T> clazz )
  164. throws PlexusSisuBridgeException
  165. {
  166. PlexusSisuBridge plexusSisuBridge = applicationContext.getBean( PlexusSisuBridge.class );
  167. return plexusSisuBridge.lookup( clazz );
  168. }
  169. private List<KnownRepositoryContentConsumer> getConsumerList( String[] consumers )
  170. throws ConsumerException, PlexusSisuBridgeException
  171. {
  172. List<KnownRepositoryContentConsumer> consumerList = new ArrayList<>();
  173. Map<String, KnownRepositoryContentConsumer> availableConsumers = getConsumers();
  174. for ( String specifiedConsumer : consumers )
  175. {
  176. if ( !availableConsumers.containsKey( specifiedConsumer ) )
  177. {
  178. System.err.println( "Specified consumer [" + specifiedConsumer + "] not found." );
  179. dumpAvailableConsumers();
  180. System.exit( 1 );
  181. }
  182. consumerList.add( availableConsumers.get( specifiedConsumer ) );
  183. }
  184. return consumerList;
  185. }
  186. private void dumpAvailableConsumers()
  187. throws PlexusSisuBridgeException
  188. {
  189. Map<String, KnownRepositoryContentConsumer> availableConsumers = getConsumers();
  190. System.out.println( ".\\ Available Consumer List \\.______________________________" );
  191. for ( Map.Entry<String, KnownRepositoryContentConsumer> entry : availableConsumers.entrySet() )
  192. {
  193. String consumerHint = entry.getKey();
  194. RepositoryContentConsumer consumer = entry.getValue();
  195. System.out.println(
  196. " " + consumerHint + ": " + consumer.getDescription() + " (" + consumer.getClass().getName() + ")" );
  197. }
  198. }
  199. @SuppressWarnings( "unchecked" )
  200. private Map<String, KnownRepositoryContentConsumer> getConsumers()
  201. throws PlexusSisuBridgeException
  202. {
  203. Map<String, KnownRepositoryContentConsumer> beans =
  204. applicationContext.getBeansOfType( KnownRepositoryContentConsumer.class );
  205. // we use a naming conventions knownRepositoryContentConsumer#hint
  206. // with plexus we used only hint so remove before#
  207. Map<String, KnownRepositoryContentConsumer> smallNames =
  208. new HashMap<String, KnownRepositoryContentConsumer>( beans.size() );
  209. for ( Map.Entry<String, KnownRepositoryContentConsumer> entry : beans.entrySet() )
  210. {
  211. smallNames.put( StringUtils.substringAfterLast( entry.getKey(), "#" ), entry.getValue() );
  212. }
  213. return smallNames;
  214. }
  215. private void doConversion( String properties )
  216. throws FileNotFoundException, IOException, RepositoryConversionException, PlexusSisuBridgeException
  217. {
  218. LegacyRepositoryConverter legacyRepositoryConverter = lookup( LegacyRepositoryConverter.class );
  219. Properties p = new Properties();
  220. try(FileInputStream fis = new FileInputStream( properties ))
  221. {
  222. p.load( fis );
  223. }
  224. File oldRepositoryPath = new File( p.getProperty( SOURCE_REPO_PATH ) );
  225. File newRepositoryPath = new File( p.getProperty( TARGET_REPO_PATH ) );
  226. System.out.println( "Converting " + oldRepositoryPath + " to " + newRepositoryPath );
  227. List<String> fileExclusionPatterns = null;
  228. String s = p.getProperty( BLACKLISTED_PATTERNS );
  229. if ( s != null )
  230. {
  231. fileExclusionPatterns = Arrays.asList( StringUtils.split( s, "," ) );
  232. }
  233. legacyRepositoryConverter.convertLegacyRepository( oldRepositoryPath, newRepositoryPath,
  234. fileExclusionPatterns );
  235. }
  236. private static class Commands
  237. {
  238. @Argument( description = "Display help information", value = "help", alias = "h" )
  239. private boolean help;
  240. @Argument( description = "Display version information", value = "version", alias = "v" )
  241. private boolean version;
  242. @Argument( description = "List available consumers", value = "listconsumers", alias = "l" )
  243. private boolean listConsumers;
  244. @Argument( description = "The consumers to use (comma delimited)", value = "consumers", alias = "u" )
  245. private String consumers = "count-artifacts";
  246. @Argument( description = "Scan the specified repository", value = "scan", alias = "s" )
  247. private boolean scan;
  248. @Argument(
  249. description = "Convert a legacy Maven 1.x repository to a Maven 2.x repository using a properties file to describe the conversion",
  250. value = "convert", alias = "c" )
  251. private boolean convert;
  252. @Argument( description = "The properties file for the conversion", value = "properties" )
  253. private String properties = "conversion.properties";
  254. @Argument( description = "The repository to scan", value = "repository" )
  255. private String repository;
  256. }
  257. }