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.io.IOException;
22  import java.io.UncheckedIOException;
23  import java.nio.file.FileSystem;
24  import java.nio.file.Paths;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import com.google.common.jimfs.Configuration;
30  import com.google.common.jimfs.Jimfs;
31  import org.eclipse.aether.RepositorySystem;
32  import org.eclipse.aether.RepositorySystemSession;
33  import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
34  import org.eclipse.aether.repository.RemoteRepository;
35  import org.eclipse.aether.supplier.SessionBuilderSupplier;
36  import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
37  
38  /**
39   * A helper to boot the repository system and a repository system session.
40   */
41  public class Booter {
42      public static final String SUPPLIER = "supplier";
43  
44      public static final String SISU = "sisu";
45  
46      public static final DependencyGraphDumper DUMPER_SOUT = new DependencyGraphDumper(System.out::println);
47  
48      public static String selectFactory(String[] args) {
49          if (args == null || args.length == 0) {
50              return SUPPLIER;
51          } else {
52              return args[0];
53          }
54      }
55  
56      public static RepositorySystem newRepositorySystem(final String factory) {
57          switch (factory) {
58              case SUPPLIER:
59                  return org.apache.maven.resolver.examples.supplier.SupplierRepositorySystemFactory
60                          .newRepositorySystem();
61              case SISU:
62                  return org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory.newRepositorySystem();
63              default:
64                  throw new IllegalArgumentException("Unknown factory: " + factory);
65          }
66      }
67  
68      public static SessionBuilder newRepositorySystemSession(RepositorySystem system) {
69          FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
70          SessionBuilder result = new SessionBuilderSupplier(system)
71                  .get()
72                  .withLocalRepositoryBaseDirectories(fs.getPath("local-repo"))
73                  .setRepositoryListener(new ConsoleRepositoryListener())
74                  .setTransferListener(new ConsoleTransferListener())
75                  .setConfigProperty("aether.generator.gpg.enabled", Boolean.TRUE.toString())
76                  .setConfigProperty(
77                          "aether.generator.gpg.keyFilePath",
78                          Paths.get("src/main/resources/alice.key")
79                                  .toAbsolutePath()
80                                  .toString())
81                  .setConfigProperty("aether.syncContext.named.factory", "noop");
82          result.addOnSessionEndedHandler(() -> {
83              try {
84                  fs.close();
85              } catch (IOException e) {
86                  throw new UncheckedIOException(e);
87              }
88          });
89  
90          // uncomment to generate dirty trees
91          // session.setDependencyGraphTransformer( null );
92  
93          return result;
94      }
95  
96      public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
97          return new ArrayList<>(Collections.singletonList(newCentralRepository()));
98      }
99  
100     private static RemoteRepository newCentralRepository() {
101         return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build();
102     }
103 }