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 *
060 * @deprecated (To be removed as it was introduced in 2.0.0-alpha-2!) This class is wrong, as it uses Resolver 1.x
061 * bits that do interpret dependency scopes. The proper session supplier should be provided by consumer project
062 * (Maven) that also defines the dependency scopes and their meaning and semantics, as session need to be equipped
063 * with these bits. Session is very much dependent on the consumer project.
064 */
065@Deprecated
066public class SessionBuilderSupplier implements Supplier<SessionBuilder> {
067    protected final RepositorySystem repositorySystem;
068
069    public SessionBuilderSupplier(RepositorySystem repositorySystem) {
070        this.repositorySystem = requireNonNull(repositorySystem);
071    }
072
073    protected void configureSessionBuilder(SessionBuilder session) {
074        session.setDependencyTraverser(getDependencyTraverser());
075        session.setDependencyManager(getDependencyManager());
076        session.setDependencySelector(getDependencySelector());
077        session.setDependencyGraphTransformer(getDependencyGraphTransformer());
078        session.setArtifactTypeRegistry(getArtifactTypeRegistry());
079        session.setArtifactDescriptorPolicy(getArtifactDescriptorPolicy());
080    }
081
082    protected DependencyTraverser getDependencyTraverser() {
083        return new FatArtifactTraverser();
084    }
085
086    protected DependencyManager getDependencyManager() {
087        return new ClassicDependencyManager();
088    }
089
090    protected DependencySelector getDependencySelector() {
091        return new AndDependencySelector(
092                new ScopeDependencySelector("test", "provided"),
093                new OptionalDependencySelector(),
094                new ExclusionDependencySelector());
095    }
096
097    protected DependencyGraphTransformer getDependencyGraphTransformer() {
098        return new ChainedDependencyGraphTransformer(
099                new ConflictResolver(
100                        new NearestVersionSelector(), new JavaScopeSelector(),
101                        new SimpleOptionalitySelector(), new JavaScopeDeriver()),
102                new JavaDependencyContextRefiner());
103    }
104
105    protected ArtifactTypeRegistry getArtifactTypeRegistry() {
106        DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
107        stereotypes.add(new DefaultArtifactType("pom"));
108        stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
109        stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
110        stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
111        stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
112        stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
113        stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
114        stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
115        stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
116        stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
117        stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
118        stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
119        return stereotypes;
120    }
121
122    protected ArtifactDescriptorPolicy getArtifactDescriptorPolicy() {
123        return new SimpleArtifactDescriptorPolicy(true, true);
124    }
125
126    /**
127     * Creates a new Maven-like repository system session by initializing the session with values typical for
128     * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
129     * graphs, most other settings remain at their generic default value. Use the various setters to further configure
130     * the session with authentication, mirror, proxy and other information required for your environment. At least,
131     * local repository manager needs to be configured to make session be able to create session instance.
132     *
133     * @return SessionBuilder configured with minimally required things for "Maven-based resolution". At least LRM must
134     * be set on builder to make it able to create session instances.
135     */
136    @Override
137    public SessionBuilder get() {
138        SessionBuilder builder = repositorySystem.createSessionBuilder();
139        configureSessionBuilder(builder);
140        return builder;
141    }
142}