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