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.ConfigurableVersionSelector;
043import org.eclipse.aether.util.graph.transformer.ConflictResolver;
044import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
045import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
046import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
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.setSystemProperties(System.getProperties());
073        session.setDependencyTraverser(getDependencyTraverser());
074        session.setDependencyManager(getDependencyManager());
075        session.setDependencySelector(getDependencySelector());
076        session.setDependencyGraphTransformer(getDependencyGraphTransformer());
077        session.setArtifactTypeRegistry(getArtifactTypeRegistry());
078        session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy());
079    }
080
081    protected DependencyTraverser getDependencyTraverser() {
082        return new FatArtifactTraverser();
083    }
084
085    protected DependencyManager getDependencyManager() {
086        return new ClassicDependencyManager(false, null);
087    }
088
089    protected DependencySelector getDependencySelector() {
090        return new AndDependencySelector(
091                ScopeDependencySelector.legacy(null, Arrays.asList(JavaScopes.TEST, JavaScopes.PROVIDED)),
092                OptionalDependencySelector.fromDirect(),
093                new ExclusionDependencySelector());
094    }
095
096    protected DependencyGraphTransformer getDependencyGraphTransformer() {
097        return new ChainedDependencyGraphTransformer(
098                new ConflictResolver(
099                        new ConfigurableVersionSelector(), new JavaScopeSelector(),
100                        new SimpleOptionalitySelector(), new JavaScopeDeriver()),
101                new JavaDependencyContextRefiner());
102    }
103
104    protected ArtifactTypeRegistry getArtifactTypeRegistry() {
105        DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
106        stereotypes.add(new DefaultArtifactType("pom"));
107        stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
108        stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
109        stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
110        stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
111        stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
112        stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
113        stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
114        stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
115        stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
116        stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
117        stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
118        return stereotypes;
119    }
120
121    protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
122        return new SimpleArtifactDescriptorPolicy(true, true);
123    }
124
125    /**
126     * Creates a new Maven-like repository system session by initializing the session with values typical for
127     * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
128     * graphs, most other settings remain at their generic default value. Use the various setters to further configure
129     * the session with authentication, mirror, proxy and other information required for your environment. At least,
130     * local repository manager needs to be configured to make session be able to create session instance.
131     *
132     * @return SessionBuilder configured with minimally required things for "Maven-based resolution". At least LRM must
133     * be set on builder to make it able to create session instances.
134     */
135    @Override
136    public SessionBuilder get() {
137        SessionBuilder builder = repositorySystem.createSessionBuilder();
138        configureSessionBuilder(builder);
139        return builder;
140    }
141}