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   * @author Benjamin Bentmann
49   */
50  public final class MavenRepositorySystemUtils {
51  
52      private MavenRepositorySystemUtils() {
53          // hide constructor
54      }
55  
56      /**
57       * Creates a new Maven-like repository system session by initializing the session with values typical for
58       * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
59       * graphs, most other settings remain at their generic default value. Use the various setters to further configure
60       * the session with authentication, mirror, proxy and other information required for your environment.
61       *
62       * @return The new repository system session, never {@code null}.
63       */
64      public static DefaultRepositorySystemSession newSession() {
65          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession();
66  
67          DependencyTraverser depTraverser = new FatArtifactTraverser();
68          session.setDependencyTraverser(depTraverser);
69  
70          DependencyManager depManager = new ClassicDependencyManager();
71          session.setDependencyManager(depManager);
72  
73          DependencySelector depFilter = new AndDependencySelector(
74                  new ScopeDependencySelector("test", "provided"),
75                  new OptionalDependencySelector(),
76                  new ExclusionDependencySelector());
77          session.setDependencySelector(depFilter);
78  
79          DependencyGraphTransformer transformer = new ConflictResolver(
80                  new NearestVersionSelector(), new JavaScopeSelector(),
81                  new SimpleOptionalitySelector(), new JavaScopeDeriver());
82          transformer = new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner());
83          session.setDependencyGraphTransformer(transformer);
84  
85          DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
86          stereotypes.add(new DefaultArtifactType("pom"));
87          stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
88          stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
89          stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
90          stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
91          stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
92          stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
93          stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
94          stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
95          stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
96          stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
97          stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
98          session.setArtifactTypeRegistry(stereotypes);
99  
100         session.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true));
101 
102         return session;
103     }
104 }