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.RepositorySystemSession.SessionBuilder;
23  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
24  import org.eclipse.aether.artifact.DefaultArtifactType;
25  import org.eclipse.aether.collection.DependencyGraphTransformer;
26  import org.eclipse.aether.collection.DependencyManager;
27  import org.eclipse.aether.collection.DependencySelector;
28  import org.eclipse.aether.collection.DependencyTraverser;
29  import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
30  import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
31  import org.eclipse.aether.util.graph.selector.AndDependencySelector;
32  import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
33  import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
34  import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
35  import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
36  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
37  import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
38  import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
39  import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
40  import org.eclipse.aether.util.graph.transformer.NearestVersionSelector;
41  import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
42  import org.eclipse.aether.util.graph.traverser.FatArtifactTraverser;
43  import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
44  
45  import static java.util.Objects.requireNonNull;
46  
47  /**
48   * A utility class to assist in setting up a Maven-like repository system. <em>Note:</em> This component is meant to
49   * assist those clients that employ the repository system outside of an IoC container, Maven plugins should instead
50   * always use regular dependency injection to acquire the repository system.
51   *
52   */
53  public final class MavenRepositorySystemUtils {
54  
55      private MavenRepositorySystemUtils() {
56          // hide constructor
57      }
58  
59      /**
60       * This method is deprecated, nobody should use it.
61       *
62       * @deprecated This method is here only for legacy uses (like UTs), nothing else should use it.
63       */
64      @Deprecated
65      public static DefaultRepositorySystemSession newSession() {
66          DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(h -> false); // no close handle
67  
68          DependencyTraverser depTraverser = new FatArtifactTraverser();
69          session.setDependencyTraverser(depTraverser);
70  
71          DependencyManager depManager = new ClassicDependencyManager();
72          session.setDependencyManager(depManager);
73  
74          DependencySelector depFilter = new AndDependencySelector(
75                  new ScopeDependencySelector("test", "provided"),
76                  new OptionalDependencySelector(),
77                  new ExclusionDependencySelector());
78          session.setDependencySelector(depFilter);
79  
80          DependencyGraphTransformer transformer = new ConflictResolver(
81                  new NearestVersionSelector(), new JavaScopeSelector(),
82                  new SimpleOptionalitySelector(), new JavaScopeDeriver());
83          transformer = new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner());
84          session.setDependencyGraphTransformer(transformer);
85  
86          session.setArtifactTypeRegistry(newArtifactTypeRegistry());
87  
88          session.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true));
89  
90          return session;
91      }
92  
93      /**
94       * Creates new Maven-like {@link ArtifactTypeRegistry}. This method should not be used from Maven.
95       *
96       * @since 4.0.0
97       */
98      public static ArtifactTypeRegistry newArtifactTypeRegistry() {
99          DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
100         stereotypes.add(new DefaultArtifactType("pom"));
101         stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
102         stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
103         stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
104         stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
105         stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
106         stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
107         stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
108         stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
109         stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
110         stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
111         stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
112         return stereotypes;
113     }
114 
115     /**
116      * Creates a new Maven-like repository system session by initializing the session with values typical for
117      * Maven-based resolution. In more detail, this method configures settings relevant for the processing of dependency
118      * graphs, most other settings remain at their generic default value. Use the various setters to further configure
119      * the session with authentication, mirror, proxy and other information required for your environment.
120      *
121      * @return The new repository system session, never {@code null}.
122      * @since 4.0.0
123      */
124     public static SessionBuilder newSession(SessionBuilder session, ArtifactTypeRegistry artifactTypeRegistry) {
125         requireNonNull(session, "null sessionBuilder");
126         requireNonNull(artifactTypeRegistry, "null artifactTypeRegistry");
127 
128         DependencyTraverser depTraverser = new FatArtifactTraverser();
129         session.setDependencyTraverser(depTraverser);
130 
131         DependencyManager depManager = new ClassicDependencyManager();
132         session.setDependencyManager(depManager);
133 
134         DependencySelector depFilter = new AndDependencySelector(
135                 new ScopeDependencySelector("test", "provided"),
136                 new OptionalDependencySelector(),
137                 new ExclusionDependencySelector());
138         session.setDependencySelector(depFilter);
139 
140         DependencyGraphTransformer transformer = new ConflictResolver(
141                 new NearestVersionSelector(), new JavaScopeSelector(),
142                 new SimpleOptionalitySelector(), new JavaScopeDeriver());
143         transformer = new ChainedDependencyGraphTransformer(transformer, new JavaDependencyContextRefiner());
144         session.setDependencyGraphTransformer(transformer);
145         session.setArtifactTypeRegistry(artifactTypeRegistry);
146 
147         session.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true));
148 
149         return session;
150     }
151 }