001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.resolver.examples.util; 020 021import java.io.IOException; 022import java.nio.file.Path; 023import java.nio.file.Paths; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.List; 027 028import com.google.common.jimfs.Configuration; 029import com.google.common.jimfs.Jimfs; 030import org.eclipse.aether.RepositorySystem; 031import org.eclipse.aether.RepositorySystemSession; 032import org.eclipse.aether.RepositorySystemSession.SessionBuilder; 033import org.eclipse.aether.repository.RemoteRepository; 034import org.eclipse.aether.repository.RepositoryPolicy; 035import org.eclipse.aether.supplier.SessionBuilderSupplier; 036import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper; 037 038/** 039 * A helper to boot the repository system and a repository system session. 040 */ 041public class Booter { 042 public static final String FACTORY_SUPPLIER = "supplier"; 043 044 public static final String FACTORY_SISU = "sisu"; 045 046 public static final String FS_DEFAULT = "default"; 047 048 public static final String FS_JIMFS = "jimfs"; 049 050 public static final DependencyGraphDumper DUMPER_SOUT = new DependencyGraphDumper(System.out::println); 051 052 public static String selectFactory(String[] args) { 053 if (args == null || args.length == 0) { 054 return FACTORY_SUPPLIER; 055 } else { 056 return args[0]; 057 } 058 } 059 060 public static String selectFs(String[] args) { 061 if (args == null || args.length < 2) { 062 return FS_DEFAULT; 063 } else { 064 return args[1]; 065 } 066 } 067 068 public static RepositorySystem newRepositorySystem(final String factory) { 069 System.out.println("Using factory: " + factory); 070 return switch (factory) { 071 case FACTORY_SUPPLIER -> org.apache.maven.resolver.examples.supplier.SupplierRepositorySystemFactory 072 .newRepositorySystem(); 073 case FACTORY_SISU -> org.apache.maven.resolver.examples.sisu.SisuRepositorySystemFactory 074 .newRepositorySystem(); 075 default -> throw new IllegalArgumentException("Unknown factory: " + factory); 076 }; 077 } 078 079 public static SessionBuilder newRepositorySystemSession(RepositorySystem system, String fs) { 080 System.out.println("Using FS: " + fs); 081 boolean close; 082 Path localRepository; 083 if (FS_JIMFS.equals(fs)) { 084 close = true; 085 localRepository = Jimfs.newFileSystem(Configuration.unix()).getPath("/demo"); 086 } else { 087 close = false; 088 localRepository = Path.of("target/example-snippets-repo"); 089 } 090 // Path localRepository = Path.of("target/example-snippets-repo"); 091 SessionBuilder result = new SessionBuilderSupplier(system) 092 .get() 093 .withLocalRepositoryBaseDirectories(localRepository) 094 .setRepositoryListener(new ConsoleRepositoryListener()) 095 .setTransferListener(new ConsoleTransferListener()) 096 .setConfigProperty("aether.generator.gpg.enabled", Boolean.TRUE.toString()) 097 .setConfigProperty( 098 "aether.generator.gpg.keyFilePath", 099 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}