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.repository.RepositoryPolicy;
32  import org.eclipse.aether.supplier.SessionBuilderSupplier;
33  import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
34  
35  /**
36   * A helper to boot the repository system and a repository system session.
37   */
38  public class Booter {
39      public static final String SUPPLIER = "supplier";
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 SUPPLIER;
48          } else {
49              return args[0];
50          }
51      }
52  
53      public static RepositorySystem newRepositorySystem(final String factory) {
54          switch (factory) {
55              case SUPPLIER:
56                  return org.apache.maven.resolver.examples.supplier.SupplierRepositorySystemFactory
57                          .newRepositorySystem();
58              case SISU:
59                  return org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory.newRepositorySystem();
60              default:
61                  throw new IllegalArgumentException("Unknown factory: " + factory);
62          }
63      }
64  
65      public static SessionBuilder newRepositorySystemSession(RepositorySystem system) {
66          SessionBuilder result = new SessionBuilderSupplier(system)
67                  .get()
68                  .withLocalRepositoryBaseDirectories(Path.of("target/example-snippets-repo"))
69                  .setRepositoryListener(new ConsoleRepositoryListener())
70                  .setTransferListener(new ConsoleTransferListener())
71                  .setConfigProperty("aether.generator.gpg.enabled", Boolean.TRUE.toString())
72                  .setConfigProperty(
73                          "aether.generator.gpg.keyFilePath",
74                          Paths.get("src/main/resources/alice.key")
75                                  .toAbsolutePath()
76                                  .toString())
77                  .setConfigProperty("aether.syncContext.named.factory", "noop");
78  
79          // uncomment to generate dirty trees
80          // session.setDependencyGraphTransformer( null );
81  
82          return result;
83      }
84  
85      public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
86          return new ArrayList<>(Collections.singletonList(newCentralRepository()));
87      }
88  
89      private static RemoteRepository newCentralRepository() {
90          return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/")
91                  .setReleasePolicy(new RepositoryPolicy(
92                          true,
93                          RepositoryPolicy.UPDATE_POLICY_NEVER,
94                          RepositoryPolicy.UPDATE_POLICY_DAILY,
95                          RepositoryPolicy.CHECKSUM_POLICY_FAIL))
96                  .setSnapshotPolicy(new RepositoryPolicy(
97                          false,
98                          RepositoryPolicy.UPDATE_POLICY_NEVER,
99                          RepositoryPolicy.UPDATE_POLICY_DAILY,
100                         RepositoryPolicy.CHECKSUM_POLICY_FAIL))
101                 .build();
102     }
103 }