1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
104
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 }