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