001package org.apache.maven.repository.internal;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Properties;
023
024import org.eclipse.aether.DefaultRepositorySystemSession;
025import org.eclipse.aether.artifact.DefaultArtifactType;
026import org.eclipse.aether.collection.DependencyGraphTransformer;
027import org.eclipse.aether.collection.DependencyManager;
028import org.eclipse.aether.collection.DependencySelector;
029import org.eclipse.aether.collection.DependencyTraverser;
030import org.eclipse.aether.impl.ArtifactDescriptorReader;
031import org.eclipse.aether.impl.DefaultServiceLocator;
032import org.eclipse.aether.impl.MetadataGeneratorFactory;
033import org.eclipse.aether.impl.VersionRangeResolver;
034import org.eclipse.aether.impl.VersionResolver;
035import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
036import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
037import org.eclipse.aether.util.graph.selector.AndDependencySelector;
038import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
039import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
040import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
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
051/**
052 * A utility class to assist in setting up a Maven-like repository system. <em>Note:</em> This component is meant to
053 * assist those clients that employ the repository system outside of an IoC container, Maven plugins should instead
054 * always use regular dependency injection to acquire the repository system.
055 * 
056 * @author Benjamin Bentmann
057 */
058public final class MavenRepositorySystemUtils
059{
060
061    private MavenRepositorySystemUtils()
062    {
063        // hide constructor
064    }
065
066    /**
067     * Creates a new service locator that already knows about all service implementations included in this library. To
068     * acquire a complete repository system, clients need to add some repository connectors for remote transfers.
069     * 
070     * @return The new service locator, never {@code null}.
071     */
072    public static DefaultServiceLocator newServiceLocator()
073    {
074        DefaultServiceLocator locator = new DefaultServiceLocator();
075        locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
076        locator.addService( VersionResolver.class, DefaultVersionResolver.class );
077        locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
078        locator.addService( MetadataGeneratorFactory.class, SnapshotMetadataGeneratorFactory.class );
079        locator.addService( MetadataGeneratorFactory.class, VersionsMetadataGeneratorFactory.class );
080        return locator;
081    }
082
083    /**
084     * Creates a new Maven-like repository system session by initializing the session with values typical for
085     * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
086     * graphs, most other settings remain at their generic default value. Use the various setters to further configure
087     * the session with authentication, mirror, proxy and other information required for your environment.
088     * 
089     * @return The new repository system session, never {@code null}.
090     */
091    public static DefaultRepositorySystemSession newSession()
092    {
093        DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
094
095        DependencyTraverser depTraverser = new FatArtifactTraverser();
096        session.setDependencyTraverser( depTraverser );
097
098        DependencyManager depManager = new ClassicDependencyManager();
099        session.setDependencyManager( depManager );
100
101        DependencySelector depFilter =
102            new AndDependencySelector( new ScopeDependencySelector( "test", "provided" ),
103                                       new OptionalDependencySelector(), new ExclusionDependencySelector() );
104        session.setDependencySelector( depFilter );
105
106        DependencyGraphTransformer transformer =
107            new ConflictResolver( new NearestVersionSelector(), new JavaScopeSelector(),
108                                  new SimpleOptionalitySelector(), new JavaScopeDeriver() );
109        new ChainedDependencyGraphTransformer( transformer, new JavaDependencyContextRefiner() );
110        session.setDependencyGraphTransformer( transformer );
111
112        DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
113        stereotypes.add( new DefaultArtifactType( "pom" ) );
114        stereotypes.add( new DefaultArtifactType( "maven-plugin", "jar", "", "java" ) );
115        stereotypes.add( new DefaultArtifactType( "jar", "jar", "", "java" ) );
116        stereotypes.add( new DefaultArtifactType( "ejb", "jar", "", "java" ) );
117        stereotypes.add( new DefaultArtifactType( "ejb-client", "jar", "client", "java" ) );
118        stereotypes.add( new DefaultArtifactType( "test-jar", "jar", "tests", "java" ) );
119        stereotypes.add( new DefaultArtifactType( "javadoc", "jar", "javadoc", "java" ) );
120        stereotypes.add( new DefaultArtifactType( "java-source", "jar", "sources", "java", false, false ) );
121        stereotypes.add( new DefaultArtifactType( "war", "war", "", "java", false, true ) );
122        stereotypes.add( new DefaultArtifactType( "ear", "ear", "", "java", false, true ) );
123        stereotypes.add( new DefaultArtifactType( "rar", "rar", "", "java", false, true ) );
124        stereotypes.add( new DefaultArtifactType( "par", "par", "", "java", false, true ) );
125        session.setArtifactTypeRegistry( stereotypes );
126
127        session.setArtifactDescriptorPolicy( new SimpleArtifactDescriptorPolicy( true, true ) );
128
129        Properties sysProps = System.getProperties();
130        session.setSystemProperties( sysProps );
131        session.setConfigProperties( sysProps );
132
133        return session;
134    }
135
136}