View Javadoc
1   package org.apache.maven.repository.internal;
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 java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.model.DependencyManagement;
30  import org.apache.maven.model.DistributionManagement;
31  import org.apache.maven.model.License;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.Prerequisites;
34  import org.apache.maven.model.Repository;
35  import org.eclipse.aether.RepositorySystemSession;
36  import org.eclipse.aether.artifact.Artifact;
37  import org.eclipse.aether.artifact.ArtifactProperties;
38  import org.eclipse.aether.artifact.ArtifactType;
39  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
40  import org.eclipse.aether.artifact.DefaultArtifact;
41  import org.eclipse.aether.artifact.DefaultArtifactType;
42  import org.eclipse.aether.graph.Dependency;
43  import org.eclipse.aether.graph.Exclusion;
44  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
45  
46  /**
47   * Populates Aether {@link ArtifactDescriptorResult} from Maven project {@link Model}.
48   * 
49   * <strong>Note:</strong> This class is part of work in progress and can be changed or removed without notice.
50   * @since 3.2.4
51   */
52  public class ArtifactDescriptorReaderDelegate
53  {
54      public void populateResult( RepositorySystemSession session, ArtifactDescriptorResult result, Model model )
55      {
56          ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
57  
58          for ( Repository r : model.getRepositories() )
59          {
60              result.addRepository( ArtifactDescriptorUtils.toRemoteRepository( r ) );
61          }
62  
63          for ( org.apache.maven.model.Dependency dependency : model.getDependencies() )
64          {
65              result.addDependency( convert( dependency, stereotypes ) );
66          }
67  
68          DependencyManagement mgmt = model.getDependencyManagement();
69          if ( mgmt != null )
70          {
71              for ( org.apache.maven.model.Dependency dependency : mgmt.getDependencies() )
72              {
73                  result.addManagedDependency( convert( dependency, stereotypes ) );
74              }
75          }
76  
77          Map<String, Object> properties = new LinkedHashMap<>();
78  
79          Prerequisites prerequisites = model.getPrerequisites();
80          if ( prerequisites != null )
81          {
82              properties.put( "prerequisites.maven", prerequisites.getMaven() );
83          }
84  
85          List<License> licenses = model.getLicenses();
86          properties.put( "license.count", licenses.size() );
87          for ( int i = 0; i < licenses.size(); i++ )
88          {
89              License license = licenses.get( i );
90              properties.put( "license." + i + ".name", license.getName() );
91              properties.put( "license." + i + ".url", license.getUrl() );
92              properties.put( "license." + i + ".comments", license.getComments() );
93              properties.put( "license." + i + ".distribution", license.getDistribution() );
94          }
95  
96          result.setProperties( properties );
97  
98          setArtifactProperties( result, model );
99      }
100 
101     private Dependency convert( org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes )
102     {
103         ArtifactType stereotype = stereotypes.get( dependency.getType() );
104         if ( stereotype == null )
105         {
106             stereotype = new DefaultArtifactType( dependency.getType() );
107         }
108 
109         boolean system = dependency.getSystemPath() != null && dependency.getSystemPath().length() > 0;
110 
111         Map<String, String> props = null;
112         if ( system )
113         {
114             props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, dependency.getSystemPath() );
115         }
116 
117         Artifact artifact =
118             new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
119                                  dependency.getVersion(), props, stereotype );
120 
121         List<Exclusion> exclusions = new ArrayList<>( dependency.getExclusions().size() );
122         for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
123         {
124             exclusions.add( convert( exclusion ) );
125         }
126 
127         Dependency result = new Dependency( artifact, dependency.getScope(), dependency.isOptional(), exclusions );
128 
129         return result;
130     }
131 
132     private Exclusion convert( org.apache.maven.model.Exclusion exclusion )
133     {
134         return new Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
135     }
136 
137     private void setArtifactProperties( ArtifactDescriptorResult result, Model model )
138     {
139         String downloadUrl = null;
140         DistributionManagement distMgmt = model.getDistributionManagement();
141         if ( distMgmt != null )
142         {
143             downloadUrl = distMgmt.getDownloadUrl();
144         }
145         if ( downloadUrl != null && downloadUrl.length() > 0 )
146         {
147             Artifact artifact = result.getArtifact();
148             Map<String, String> props = new HashMap<>( artifact.getProperties() );
149             props.put( ArtifactProperties.DOWNLOAD_URL, downloadUrl );
150             result.setArtifact( artifact.setProperties( props ) );
151         }
152     }
153 }