View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.examples.util;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
26  import org.eclipse.aether.DefaultRepositorySystemSession;
27  import org.eclipse.aether.RepositorySystem;
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.repository.LocalRepository;
30  import org.eclipse.aether.repository.RemoteRepository;
31  import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
32  
33  /**
34   * A helper to boot the repository system and a repository system session.
35   */
36  public class Booter {
37      public static final String SERVICE_LOCATOR = "serviceLocator";
38  
39      public static final String GUICE = "guice";
40  
41      public static final String SISU = "sisu";
42  
43      public static final DependencyGraphDumper DUMPER_SOUT = new DependencyGraphDumper(System.out::println);
44  
45      public static String selectFactory(String[] args) {
46          if (args == null || args.length == 0) {
47              return SERVICE_LOCATOR;
48          } else {
49              return args[0];
50          }
51      }
52  
53      public static RepositorySystem newRepositorySystem(final String factory) {
54          switch (factory) {
55              case SERVICE_LOCATOR:
56                  return org.apache.maven.resolver.examples.manual.ManualRepositorySystemFactory.newRepositorySystem();
57              case GUICE:
58                  return org.apache.maven.resolver.examples.guice.GuiceRepositorySystemFactory.newRepositorySystem();
59              case SISU:
60                  return org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory.newRepositorySystem();
61              default:
62                  throw new IllegalArgumentException("Unknown factory: " + factory);
63          }
64      }
65  
66      public static DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system) {
67          DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
68  
69          LocalRepository localRepo = new LocalRepository("target/local-repo");
70          session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
71  
72          session.setTransferListener(new ConsoleTransferListener());
73          session.setRepositoryListener(new ConsoleRepositoryListener());
74  
75          // uncomment to generate dirty trees
76          // session.setDependencyGraphTransformer( null );
77  
78          return session;
79      }
80  
81      public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
82          return new ArrayList<>(Collections.singletonList(newCentralRepository()));
83      }
84  
85      private static RemoteRepository newCentralRepository() {
86          return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build();
87      }
88  }