package org.apache.archiva.cli; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import com.sampullara.cli.Args; import com.sampullara.cli.Argument; import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.archiva.consumers.ConsumerException; import org.apache.archiva.consumers.InvalidRepositoryContentConsumer; import org.apache.archiva.consumers.KnownRepositoryContentConsumer; import org.apache.archiva.consumers.RepositoryContentConsumer; import org.apache.archiva.converter.RepositoryConversionException; import org.apache.archiva.converter.legacy.LegacyRepositoryConverter; import org.apache.archiva.repository.scanner.RepositoryScanStatistics; import org.apache.archiva.repository.scanner.RepositoryScanner; import org.apache.archiva.repository.scanner.RepositoryScannerException; import org.apache.commons.lang.StringUtils; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; /** * ArchivaCli *
* * TODO add back reading of archiva.xml from a given location */ public class ArchivaCli { // ---------------------------------------------------------------------------- // Properties controlling Repository conversion // ---------------------------------------------------------------------------- public static final String SOURCE_REPO_PATH = "sourceRepositoryPath"; public static final String TARGET_REPO_PATH = "targetRepositoryPath"; public static final String BLACKLISTED_PATTERNS = "blacklistPatterns"; public static final String POM_PROPERTIES = "/META-INF/maven/org.apache.archiva/archiva-cli/pom.properties"; private static String getVersion() throws IOException { try (InputStream pomStream = ArchivaCli.class.getResourceAsStream( POM_PROPERTIES )) { if ( pomStream == null ) { throw new IOException( "Failed to load " + POM_PROPERTIES ); } Properties properties = new Properties(); properties.load( pomStream ); return properties.getProperty( "version" ); } } private ClassPathXmlApplicationContext applicationContext; public ArchivaCli() { applicationContext = new ClassPathXmlApplicationContext( new String[]{ "classpath*:/META-INF/spring-context.xml" } ); } public static void main( String[] args ) throws Exception { Commands command = new Commands(); try { Args.parse( command, args ); } catch ( IllegalArgumentException e ) { System.err.println( e.getMessage() ); Args.usage( command ); return; } ArchivaCli cli = new ArchivaCli(); try { cli.execute( command ); } finally { cli.destroy(); } } private void destroy() { applicationContext.destroy(); } private void execute( Commands command ) throws Exception { if ( command.help ) { Args.usage( command ); } else if ( command.version ) { System.out.print( "Version: " + getVersion() ); } else if ( command.convert ) { doConversion( command.properties ); } else if ( command.scan ) { if ( command.repository == null ) { System.err.println( "The repository must be specified." ); Args.usage( command ); return; } doScan( command.repository, command.consumers.split( "," ) ); } else if ( command.listConsumers ) { dumpAvailableConsumers(); } else { Args.usage( command ); } } private void doScan( String path, String[] consumers ) throws ConsumerException, MalformedURLException { ManagedRepository repo = new ManagedRepository(); repo.setId( new File( path ).getName() ); repo.setName( "Archiva CLI Provided Repo" ); repo.setLocation( path ); List