1 package org.apache.maven.repository.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
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(),
128 dependency.getOptional() != null
129 ? dependency.isOptional()
130 : null,
131 exclusions );
132
133 return result;
134 }
135
136 private Exclusion convert( org.apache.maven.model.Exclusion exclusion )
137 {
138 return new Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
139 }
140
141 private void setArtifactProperties( ArtifactDescriptorResult result, Model model )
142 {
143 String downloadUrl = null;
144 DistributionManagement distMgmt = model.getDistributionManagement();
145 if ( distMgmt != null )
146 {
147 downloadUrl = distMgmt.getDownloadUrl();
148 }
149 if ( downloadUrl != null && downloadUrl.length() > 0 )
150 {
151 Artifact artifact = result.getArtifact();
152 Map<String, String> props = new HashMap<>( artifact.getProperties() );
153 props.put( ArtifactProperties.DOWNLOAD_URL, downloadUrl );
154 result.setArtifact( artifact.setProperties( props ) );
155 }
156 }
157 }