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.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import com.google.common.jimfs.Configuration;
29  import com.google.common.jimfs.Jimfs;
30  import org.eclipse.aether.RepositorySystem;
31  import org.eclipse.aether.RepositorySystemSession;
32  import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
33  import org.eclipse.aether.repository.RemoteRepository;
34  import org.eclipse.aether.repository.RepositoryPolicy;
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 FACTORY_SUPPLIER = "supplier";
43  
44      public static final String FACTORY_SISU = "sisu";
45  
46      public static final String FS_DEFAULT = "default";
47  
48      public static final String FS_JIMFS = "jimfs";
49  
50      public static final DependencyGraphDumper DUMPER_SOUT = new DependencyGraphDumper(System.out::println);
51  
52      public static String selectFactory(String[] args) {
53          if (args == null || args.length == 0) {
54              return FACTORY_SUPPLIER;
55          } else {
56              return args[0];
57          }
58      }
59  
60      public static String selectFs(String[] args) {
61          if (args == null || args.length < 2) {
62              return FS_DEFAULT;
63          } else {
64              return args[1];
65          }
66      }
67  
68      public static RepositorySystem newRepositorySystem(final String factory) {
69          System.out.println("Using factory: " + factory);
70          return switch (factory) {
71              case FACTORY_SUPPLIER -> org.apache.maven.resolver.examples.supplier.SupplierRepositorySystemFactory
72                      .newRepositorySystem();
73              case FACTORY_SISU -> org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory
74                      .newRepositorySystem();
75              default -> throw new IllegalArgumentException("Unknown factory: " + factory);
76          };
77      }
78  
79      public static SessionBuilder newRepositorySystemSession(RepositorySystem system, String fs) {
80          System.out.println("Using FS: " + fs);
81          boolean close;
82          Path localRepository;
83          if (FS_JIMFS.equals(fs)) {
84              close = true;
85              localRepository = Jimfs.newFileSystem(Configuration.unix()).getPath("/demo");
86          } else {
87              close = false;
88              localRepository = Path.of("target/example-snippets-repo");
89          }
90          // Path localRepository = Path.of("target/example-snippets-repo");
91          SessionBuilder result = new SessionBuilderSupplier(system)
92                  .get()
93                  .withLocalRepositoryBaseDirectories(localRepository)
94                  .setRepositoryListener(new ConsoleRepositoryListener())
95                  .setTransferListener(new ConsoleTransferListener())
96                  .setConfigProperty("aether.generator.gpg.enabled", Boolean.TRUE.toString())
97                  .setConfigProperty(
98                          "aether.generator.gpg.keyFilePath",
99                          Paths.get("src/main/resources/alice.key")
100                                 .toAbsolutePath()
101                                 .toString());
102 
103         // uncomment to generate dirty trees
104         // session.setDependencyGraphTransformer( null );
105 
106         if (close) {
107             result.addOnSessionEndedHandler(() -> {
108                 try {
109                     localRepository.getFileSystem().close();
110                 } catch (IOException e) {
111                     e.printStackTrace();
112                 }
113             });
114         }
115 
116         return result;
117     }
118 
119     public static List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
120         return new ArrayList<>(Collections.singletonList(newCentralRepository()));
121     }
122 
123     private static RemoteRepository newCentralRepository() {
124         return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/")
125                 .setReleasePolicy(new RepositoryPolicy(
126                         true,
127                         RepositoryPolicy.UPDATE_POLICY_NEVER,
128                         RepositoryPolicy.UPDATE_POLICY_DAILY,
129                         RepositoryPolicy.CHECKSUM_POLICY_FAIL))
130                 .setSnapshotPolicy(new RepositoryPolicy(
131                         false,
132                         RepositoryPolicy.UPDATE_POLICY_NEVER,
133                         RepositoryPolicy.UPDATE_POLICY_DAILY,
134                         RepositoryPolicy.CHECKSUM_POLICY_FAIL))
135                 .build();
136     }
137 }