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