View Javadoc

1   package org.apache.maven.repository.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Properties;
23  
24  import org.eclipse.aether.DefaultRepositorySystemSession;
25  import org.eclipse.aether.artifact.DefaultArtifactType;
26  import org.eclipse.aether.collection.DependencyGraphTransformer;
27  import org.eclipse.aether.collection.DependencyManager;
28  import org.eclipse.aether.collection.DependencySelector;
29  import org.eclipse.aether.collection.DependencyTraverser;
30  import org.eclipse.aether.impl.ArtifactDescriptorReader;
31  import org.eclipse.aether.impl.DefaultServiceLocator;
32  import org.eclipse.aether.impl.MetadataGeneratorFactory;
33  import org.eclipse.aether.impl.VersionRangeResolver;
34  import org.eclipse.aether.impl.VersionResolver;
35  import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
36  import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
37  import org.eclipse.aether.util.graph.selector.AndDependencySelector;
38  import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
39  import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
40  import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
41  import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
42  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
43  import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
44  import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
45  import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
46  import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
47  import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
48  import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
49  import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
50  
51  /**
52   * A utility class to assist in setting up a Maven-like repository system. <em>Note:</em> This component is meant to
53   * assist those clients that employ the repository system outside of an IoC container, Maven plugins should instead
54   * always use regular dependency injection to acquire the repository system.
55   * 
56   * @author Benjamin Bentmann
57   */
58  public final class MavenRepositorySystemUtils
59  {
60  
61      private MavenRepositorySystemUtils()
62      {
63          // hide constructor
64      }
65  
66      /**
67       * Creates a new service locator that already knows about all service implementations included in this library. To
68       * acquire a complete repository system, clients need to add some repository connectors for remote transfers.
69       * 
70       * @return The new service locator, never {@code null}.
71       */
72      public static DefaultServiceLocator newServiceLocator()
73      {
74          DefaultServiceLocator locator = new DefaultServiceLocator();
75          locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
76          locator.addService( VersionResolver.class, DefaultVersionResolver.class );
77          locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
78          locator.addService( MetadataGeneratorFactory.class, SnapshotMetadataGeneratorFactory.class );
79          locator.addService( MetadataGeneratorFactory.class, VersionsMetadataGeneratorFactory.class );
80          return locator;
81      }
82  
83      /**
84       * Creates a new Maven-like repository system session by initializing the session with values typical for
85       * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
86       * graphs, most other settings remain at their generic default value. Use the various setters to further configure
87       * the session with authentication, mirror, proxy and other information required for your environment.
88       * 
89       * @return The new repository system session, never {@code null}.
90       */
91      public static DefaultRepositorySystemSession newSession()
92      {
93          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
94  
95          DependencyTraverser depTraverser = new FatArtifactTraverser();
96          session.setDependencyTraverser( depTraverser );
97  
98          DependencyManager depManager = new ClassicDependencyManager();
99          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 }