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