001    package 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    
022    import org.sonatype.aether.collection.DependencyGraphTransformer;
023    import org.sonatype.aether.collection.DependencyManager;
024    import org.sonatype.aether.collection.DependencySelector;
025    import org.sonatype.aether.collection.DependencyTraverser;
026    import org.sonatype.aether.util.DefaultRepositorySystemSession;
027    import org.sonatype.aether.util.artifact.DefaultArtifactType;
028    import org.sonatype.aether.util.artifact.DefaultArtifactTypeRegistry;
029    import org.sonatype.aether.util.graph.manager.ClassicDependencyManager;
030    import org.sonatype.aether.util.graph.selector.AndDependencySelector;
031    import org.sonatype.aether.util.graph.selector.ExclusionDependencySelector;
032    import org.sonatype.aether.util.graph.selector.OptionalDependencySelector;
033    import org.sonatype.aether.util.graph.selector.ScopeDependencySelector;
034    import org.sonatype.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
035    import org.sonatype.aether.util.graph.transformer.ConflictMarker;
036    import org.sonatype.aether.util.graph.transformer.JavaDependencyContextRefiner;
037    import org.sonatype.aether.util.graph.transformer.JavaEffectiveScopeCalculator;
038    import org.sonatype.aether.util.graph.transformer.NearestVersionConflictResolver;
039    import org.sonatype.aether.util.graph.traverser.FatArtifactTraverser;
040    import org.sonatype.aether.util.repository.DefaultAuthenticationSelector;
041    import org.sonatype.aether.util.repository.DefaultMirrorSelector;
042    import org.sonatype.aether.util.repository.DefaultProxySelector;
043    
044    /**
045     * A simplistic repository system session that mimics Maven's behavior to help third-party developers that want to embed
046     * Maven's dependency resolution into their own applications. <strong>Warning:</strong> This class is not intended for
047     * usage by Maven plugins, those should always acquire the current repository system session via parameter injection.
048     * 
049     * @author Benjamin Bentmann
050     */
051    public class MavenRepositorySystemSession
052        extends DefaultRepositorySystemSession
053    {
054    
055        /**
056         * Creates a new Maven-like repository system session by initializing the session with values typical for
057         * Maven-based resolution. In more detail, this constructor configures settings relevant for the processing of
058         * dependency graphs, most other settings remain at their generic default value. Use the various setters to further
059         * configure the session with authentication, mirror, proxy and other information required for your environment.
060         */
061        public MavenRepositorySystemSession()
062        {
063            setMirrorSelector( new DefaultMirrorSelector() );
064            setAuthenticationSelector( new DefaultAuthenticationSelector() );
065            setProxySelector( new DefaultProxySelector() );
066    
067            DependencyTraverser depTraverser = new FatArtifactTraverser();
068            setDependencyTraverser( depTraverser );
069    
070            DependencyManager depManager = new ClassicDependencyManager();
071            setDependencyManager( depManager );
072    
073            DependencySelector depFilter =
074                new AndDependencySelector( new ScopeDependencySelector( "test", "provided" ),
075                                           new OptionalDependencySelector(), new ExclusionDependencySelector() );
076            setDependencySelector( depFilter );
077    
078            DependencyGraphTransformer transformer =
079                new ChainedDependencyGraphTransformer( new ConflictMarker(), new JavaEffectiveScopeCalculator(),
080                                                       new NearestVersionConflictResolver(),
081                                                       new JavaDependencyContextRefiner() );
082            setDependencyGraphTransformer( transformer );
083    
084            DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
085            stereotypes.add( new DefaultArtifactType( "pom" ) );
086            stereotypes.add( new DefaultArtifactType( "maven-plugin", "jar", "", "java" ) );
087            stereotypes.add( new DefaultArtifactType( "jar", "jar", "", "java" ) );
088            stereotypes.add( new DefaultArtifactType( "ejb", "jar", "", "java" ) );
089            stereotypes.add( new DefaultArtifactType( "ejb-client", "jar", "client", "java" ) );
090            stereotypes.add( new DefaultArtifactType( "test-jar", "jar", "tests", "java" ) );
091            stereotypes.add( new DefaultArtifactType( "javadoc", "jar", "javadoc", "java" ) );
092            stereotypes.add( new DefaultArtifactType( "java-source", "jar", "sources", "java", false, false ) );
093            stereotypes.add( new DefaultArtifactType( "war", "war", "", "java", false, true ) );
094            stereotypes.add( new DefaultArtifactType( "ear", "ear", "", "java", false, true ) );
095            stereotypes.add( new DefaultArtifactType( "rar", "rar", "", "java", false, true ) );
096            stereotypes.add( new DefaultArtifactType( "par", "par", "", "java", false, true ) );
097            setArtifactTypeRegistry( stereotypes );
098    
099            setIgnoreInvalidArtifactDescriptor( true );
100            setIgnoreMissingArtifactDescriptor( true );
101    
102            setSystemProps( System.getProperties() );
103            setConfigProps( System.getProperties() );
104        }
105    
106    }