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.nio.file.Path;
022import java.nio.file.Paths;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026
027import org.eclipse.aether.RepositorySystem;
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
030import org.eclipse.aether.repository.RemoteRepository;
031import org.eclipse.aether.repository.RepositoryPolicy;
032import org.eclipse.aether.supplier.SessionBuilderSupplier;
033import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
034
035/**
036 * A helper to boot the repository system and a repository system session.
037 */
038public class Booter {
039    public static final String SUPPLIER = "supplier";
040
041    public static final String SISU = "sisu";
042
043    public static final DependencyGraphDumper DUMPER_SOUT = new DependencyGraphDumper(System.out::println);
044
045    public static String selectFactory(String[] args) {
046        if (args == null || args.length == 0) {
047            return SUPPLIER;
048        } else {
049            return args[0];
050        }
051    }
052
053    public static RepositorySystem newRepositorySystem(final String factory) {
054        switch (factory) {
055            case SUPPLIER:
056                return org.apache.maven.resolver.examples.supplier.SupplierRepositorySystemFactory
057                        .newRepositorySystem();
058            case SISU:
059                return org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory.newRepositorySystem();
060            default:
061                throw new IllegalArgumentException("Unknown factory: " + factory);
062        }
063    }
064
065    public static SessionBuilder newRepositorySystemSession(RepositorySystem system) {
066        SessionBuilder result = new SessionBuilderSupplier(system)
067                .get()
068                .withLocalRepositoryBaseDirectories(Path.of("target/example-snippets-repo"))
069                .setRepositoryListener(new ConsoleRepositoryListener())
070                .setTransferListener(new ConsoleTransferListener())
071                .setConfigProperty("aether.generator.gpg.enabled", Boolean.TRUE.toString())
072                .setConfigProperty(
073                        "aether.generator.gpg.keyFilePath",
074                        Paths.get("src/main/resources/alice.key")
075                                .toAbsolutePath()
076                                .toString())
077                .setConfigProperty("aether.syncContext.named.factory", "noop");
078
079        // uncomment to generate dirty trees
080        // session.setDependencyGraphTransformer( null );
081
082        return result;
083    }
084
085    public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
086        return new ArrayList<>(Collections.singletonList(newCentralRepository()));
087    }
088
089    private static RemoteRepository newCentralRepository() {
090        return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/")
091                .setReleasePolicy(new RepositoryPolicy(
092                        true,
093                        RepositoryPolicy.UPDATE_POLICY_NEVER,
094                        RepositoryPolicy.UPDATE_POLICY_DAILY,
095                        RepositoryPolicy.CHECKSUM_POLICY_FAIL))
096                .setSnapshotPolicy(new RepositoryPolicy(
097                        false,
098                        RepositoryPolicy.UPDATE_POLICY_NEVER,
099                        RepositoryPolicy.UPDATE_POLICY_DAILY,
100                        RepositoryPolicy.CHECKSUM_POLICY_FAIL))
101                .build();
102    }
103}