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.util.artifact.DefaultArtifactTypeRegistry;
28  import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
29  import org.eclipse.aether.util.graph.selector.AndDependencySelector;
30  import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
31  import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
32  import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
33  import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
34  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
35  import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
36  import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
37  import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
38  import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
39  import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
40  import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
41  import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
42  
43  /**
44   * A utility class to assist in setting up a Maven-like repository system. <em>Note:</em> This component is meant to
45   * assist those clients that employ the repository system outside of an IoC container, Maven plugins should instead
46   * always use regular dependency injection to acquire the repository system.
47   *
48   */
49  public final class MavenRepositorySystemUtils {
50  
51      private MavenRepositorySystemUtils() {
52          // hide constructor
53      }
54  
55      /**
56       * Creates a new Maven-like repository system session by initializing the session with values typical for
57       * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
58       * graphs, most other settings remain at their generic default value. Use the various setters to further configure
59       * the session with authentication, mirror, proxy and other information required for your environment.
60       *
61       * @return The new repository system session, never {@code null}.
62       */
63      public static DefaultRepositorySystemSession newSession() {
64          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
65  
66          DependencyTraverser depTraverser = new FatArtifactTraverser();
67          session.setDependencyTraverser(depTraverser);
68  
69          DependencyManager depManager = new ClassicDependencyManager();
70          session.setDependencyManager(depManager);
71  
72          DependencySelector depFilter = new AndDependencySelector(
73                  new ScopeDependencySelector("test", "provided"),
74                  new OptionalDependencySelector(),
75                  new ExclusionDependencySelector());
76          session.setDependencySelector(depFilter);
77  
78          DependencyGraphTransformer transformer = new ConflictResolver(
79                  new NearestVersionSelector(), new JavaScopeSelector(),
80                  new SimpleOptionalitySelector(), new JavaScopeDeriver());
81          transformer = new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner());
82          session.setDependencyGraphTransformer(transformer);
83  
84          DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
85          stereotypes.add(new DefaultArtifactType("pom"));
86          stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
87          stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
88          stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
89          stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
90          stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
91          stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
92          stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
93          stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
94          stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
95          stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
96          stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
97          session.setArtifactTypeRegistry(stereotypes);
98  
99          session.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true));
100 
101         return session;
102     }
103 }