001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.resolver.examples.util;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
026import org.eclipse.aether.DefaultRepositorySystemSession;
027import org.eclipse.aether.RepositorySystem;
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.repository.LocalRepository;
030import org.eclipse.aether.repository.RemoteRepository;
031
032/**
033 * A helper to boot the repository system and a repository system session.
034 */
035public class Booter {
036    public static final String SERVICE_LOCATOR = "serviceLocator";
037
038    public static final String GUICE = "guice";
039
040    public static final String SISU = "sisu";
041
042    public static String selectFactory(String[] args) {
043        if (args == null || args.length == 0) {
044            return SERVICE_LOCATOR;
045        } else {
046            return args[0];
047        }
048    }
049
050    public static RepositorySystem newRepositorySystem(final String factory) {
051        switch (factory) {
052            case SERVICE_LOCATOR:
053                return org.apache.maven.resolver.examples.manual.ManualRepositorySystemFactory.newRepositorySystem();
054            case GUICE:
055                return org.apache.maven.resolver.examples.guice.GuiceRepositorySystemFactory.newRepositorySystem();
056            case SISU:
057                return org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory.newRepositorySystem();
058            default:
059                throw new IllegalArgumentException("Unknown factory: " + factory);
060        }
061    }
062
063    public static DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system) {
064        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
065
066        LocalRepository localRepo = new LocalRepository("target/local-repo");
067        session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
068
069        session.setTransferListener(new ConsoleTransferListener());
070        session.setRepositoryListener(new ConsoleRepositoryListener());
071
072        // uncomment to generate dirty trees
073        // session.setDependencyGraphTransformer( null );
074
075        return session;
076    }
077
078    public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
079        return new ArrayList<>(Collections.singletonList(newCentralRepository()));
080    }
081
082    private static RemoteRepository newCentralRepository() {
083        return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build();
084    }
085}