View Javadoc
1   package org.apache.maven.internal.impl;
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 javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.stream.Collectors;
26  
27  import org.apache.maven.api.DependencyCoordinate;
28  import org.apache.maven.api.Exclusion;
29  import org.apache.maven.api.annotations.Nonnull;
30  import org.apache.maven.api.services.DependencyCoordinateFactory;
31  import org.apache.maven.api.services.DependencyCoordinateFactoryRequest;
32  import org.eclipse.aether.artifact.ArtifactType;
33  
34  import static org.apache.maven.internal.impl.Utils.cast;
35  import static org.apache.maven.internal.impl.Utils.nonNull;
36  
37  @Named
38  @Singleton
39  public class DefaultDependencyCoordinateFactory implements DependencyCoordinateFactory
40  {
41  
42      @Nonnull
43      @Override
44      public DependencyCoordinate create( @Nonnull DependencyCoordinateFactoryRequest request )
45      {
46          nonNull( request, "request can not be null" );
47          DefaultSession session = cast( DefaultSession.class, request.getSession(),
48                  "request.session should be a " + DefaultSession.class );
49  
50          ArtifactType type = null;
51          if ( request.getType() != null )
52          {
53              type = session.getSession().getArtifactTypeRegistry().get( request.getType() );
54          }
55          return new DefaultDependencyCoordinate(
56                  session,
57                  new org.eclipse.aether.graph.Dependency(
58                          new org.eclipse.aether.artifact.DefaultArtifact(
59                                  request.getGroupId(),
60                                  request.getArtifactId(),
61                                  request.getClassifier(),
62                                  request.getExtension(),
63                                  request.getVersion(),
64                                  type
65                          ),
66                          request.getScope(),
67                          request.isOptional(),
68                          request.getExclusions().stream().map( this::toExclusion ).collect( Collectors.toList() ) ) );
69      }
70  
71      private org.eclipse.aether.graph.Exclusion toExclusion( Exclusion exclusion )
72      {
73          return new org.eclipse.aether.graph.Exclusion(
74                  exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
75      }
76  
77  }