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.eclipse.aether.supplier; 020 021import java.util.Arrays; 022import java.util.function.Supplier; 023 024import org.eclipse.aether.RepositorySystem; 025import org.eclipse.aether.RepositorySystemSession.CloseableSession; 026import org.eclipse.aether.RepositorySystemSession.SessionBuilder; 027import org.eclipse.aether.artifact.ArtifactTypeRegistry; 028import org.eclipse.aether.artifact.DefaultArtifactType; 029import org.eclipse.aether.collection.DependencyGraphTransformer; 030import org.eclipse.aether.collection.DependencyManager; 031import org.eclipse.aether.collection.DependencySelector; 032import org.eclipse.aether.collection.DependencyTraverser; 033import org.eclipse.aether.internal.impl.scope.OptionalDependencySelector; 034import org.eclipse.aether.internal.impl.scope.ScopeDependencySelector; 035import org.eclipse.aether.resolution.ArtifactDescriptorPolicy; 036import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry; 037import org.eclipse.aether.util.artifact.JavaScopes; 038import org.eclipse.aether.util.graph.manager.ClassicDependencyManager; 039import org.eclipse.aether.util.graph.selector.AndDependencySelector; 040import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector; 041import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer; 042import org.eclipse.aether.util.graph.transformer.ConflictResolver; 043import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner; 044import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; 045import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; 046import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; 047import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; 048import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser; 049import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy; 050 051import static java.util.Objects.requireNonNull; 052 053/** 054 * A simple {@link Supplier} of {@link SessionBuilder} instances, that on each call supplies newly 055 * constructed instance. To create session out of builder, use {@link SessionBuilder#build()}. For proper closing 056 * of sessions, use {@link CloseableSession#close()} method on built instance(s). 057 * <p> 058 * Extend this class and override methods to customize, if needed. 059 * <p> 060 * Resolver created as this is NOT using {@link org.eclipse.aether.scope.ScopeManager}! 061 * 062 * @since 2.0.0 063 */ 064public class SessionBuilderSupplier implements Supplier<SessionBuilder> { 065 protected final RepositorySystem repositorySystem; 066 067 public SessionBuilderSupplier(RepositorySystem repositorySystem) { 068 this.repositorySystem = requireNonNull(repositorySystem); 069 } 070 071 protected void configureSessionBuilder(SessionBuilder session) { 072 session.setDependencyTraverser(getDependencyTraverser()); 073 session.setDependencyManager(getDependencyManager()); 074 session.setDependencySelector(getDependencySelector()); 075 session.setDependencyGraphTransformer(getDependencyGraphTransformer()); 076 session.setArtifactTypeRegistry(getArtifactTypeRegistry()); 077 session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy()); 078 } 079 080 protected DependencyTraverser getDependencyTraverser() { 081 return new FatArtifactTraverser(); 082 } 083 084 protected DependencyManager getDependencyManager() { 085 return new ClassicDependencyManager(false, null); 086 } 087 088 protected DependencySelector getDependencySelector() { 089 return new AndDependencySelector( 090 ScopeDependencySelector.legacy(null, Arrays.asList(JavaScopes.TEST, JavaScopes.PROVIDED)), 091 OptionalDependencySelector.fromDirect(), 092 new ExclusionDependencySelector()); 093 } 094 095 protected DependencyGraphTransformer getDependencyGraphTransformer() { 096 return new ChainedDependencyGraphTransformer( 097 new ConflictResolver( 098 new NearestVersionSelector(), new JavaScopeSelector(), 099 new SimpleOptionalitySelector(), new JavaScopeDeriver()), 100 new JavaDependencyContextRefiner()); 101 } 102 103 protected ArtifactTypeRegistry getArtifactTypeRegistry() { 104 DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry(); 105 stereotypes.add(new DefaultArtifactType("pom")); 106 stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java")); 107 stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java")); 108 stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java")); 109 stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java")); 110 stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java")); 111 stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java")); 112 stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false)); 113 stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true)); 114 stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true)); 115 stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true)); 116 stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true)); 117 return stereotypes; 118 } 119 120 protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() { 121 return new SimpleArtifactDescriptorPolicy(true, true); 122 } 123 124 /** 125 * Creates a new Maven-like repository system session by initializing the session with values typical for 126 * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency 127 * graphs, most other settings remain at their generic default value. Use the various setters to further configure 128 * the session with authentication, mirror, proxy and other information required for your environment. At least, 129 * local repository manager needs to be configured to make session be able to create session instance. 130 * 131 * @return SessionBuilder configured with minimally required things for "Maven-based resolution". At least LRM must 132 * be set on builder to make it able to create session instances. 133 */ 134 @Override 135 public SessionBuilder get() { 136 SessionBuilder builder = repositorySystem.createSessionBuilder(); 137 configureSessionBuilder(builder); 138 return builder; 139 } 140}