20
20
import java .io .IOException ;
21
21
import java .io .InputStream ;
22
22
import java .nio .file .Files ;
23
- import java .util .*;
23
+ import java .util .ArrayList ;
24
+ import java .util .Arrays ;
25
+ import java .util .Collections ;
26
+ import java .util .HashMap ;
27
+ import java .util .LinkedList ;
28
+ import java .util .List ;
29
+ import java .util .Map ;
30
+ import java .util .Objects ;
31
+ import java .util .Set ;
24
32
import java .util .stream .Collectors ;
25
33
import java .util .stream .Stream ;
26
34
27
35
import org .apache .commons .lang .StringUtils ;
28
36
import org .apache .maven .archetype .ArchetypeManager ;
29
37
import org .apache .maven .archetype .catalog .ArchetypeCatalog ;
30
- import org .apache .maven .artifact .repository .ArtifactRepository ;
31
- import org .apache .maven .artifact .repository .metadata .*;
32
- import org .apache .maven .artifact .versioning .InvalidVersionSpecificationException ;
33
- import org .apache .maven .artifact .versioning .VersionRange ;
38
+ import org .apache .maven .artifact .repository .metadata .Metadata ;
39
+ import org .apache .maven .artifact .repository .metadata .io .xpp3 .MetadataXpp3Reader ;
34
40
import org .apache .maven .execution .MavenSession ;
35
41
import org .apache .maven .plugin .AbstractMojo ;
36
42
import org .apache .maven .plugin .logging .Log ;
37
43
import org .codehaus .mojo .mrm .api .ResolverUtils ;
38
- import org .codehaus .mojo .mrm .api .maven .*;
44
+ import org .codehaus .mojo .mrm .api .maven .ArchetypeCatalogNotFoundException ;
45
+ import org .codehaus .mojo .mrm .api .maven .Artifact ;
46
+ import org .codehaus .mojo .mrm .api .maven .ArtifactNotFoundException ;
47
+ import org .codehaus .mojo .mrm .api .maven .BaseArtifactStore ;
48
+ import org .codehaus .mojo .mrm .api .maven .MetadataNotFoundException ;
39
49
import org .codehaus .mojo .mrm .plugin .FactoryHelper ;
50
+ import org .codehaus .plexus .util .xml .pull .XmlPullParserException ;
40
51
import org .eclipse .aether .RepositorySystem ;
52
+ import org .eclipse .aether .metadata .DefaultMetadata ;
41
53
import org .eclipse .aether .repository .RemoteRepository ;
42
54
import org .eclipse .aether .resolution .ArtifactRequest ;
55
+ import org .eclipse .aether .resolution .MetadataRequest ;
56
+ import org .eclipse .aether .resolution .MetadataResult ;
43
57
44
58
import static java .util .Optional .ofNullable ;
45
59
@@ -50,21 +64,11 @@ public class ProxyArtifactStore extends BaseArtifactStore {
50
64
51
65
private final List <RemoteRepository > remoteRepositories ;
52
66
53
- /**
54
- * The remote repositories that we will query.
55
- */
56
- private final List <ArtifactRepository > artifactRepositories ;
57
-
58
67
/**
59
68
* The {@link Log} to log to.
60
69
*/
61
70
private final Log log ;
62
71
63
- /**
64
- * A version range that matches any version
65
- */
66
- private static final VersionRange ANY_VERSION ;
67
-
68
72
/**
69
73
* A cache of what artifacts are present.
70
74
*/
@@ -74,17 +78,6 @@ public class ProxyArtifactStore extends BaseArtifactStore {
74
78
75
79
private final MavenSession session ;
76
80
77
- static {
78
- try {
79
- ANY_VERSION = VersionRange .createFromVersionSpec ("[0,]" );
80
- } catch (InvalidVersionSpecificationException e ) {
81
- // must never happen... so if it does make sure we stop
82
- throw new IllegalStateException ("[0,] should always be a valid version specification" , e );
83
- }
84
- }
85
-
86
- private final RepositoryMetadataManager repositoryMetadataManager ;
87
-
88
81
private final ArchetypeManager archetypeManager ;
89
82
90
83
/**
@@ -96,16 +89,10 @@ public class ProxyArtifactStore extends BaseArtifactStore {
96
89
*/
97
90
public ProxyArtifactStore (FactoryHelper factoryHelper , MavenSession session , Log log ) {
98
91
this .repositorySystem = Objects .requireNonNull (factoryHelper .getRepositorySystem ());
99
- this .repositoryMetadataManager = Objects .requireNonNull (factoryHelper .getRepositoryMetadataManager ());
100
92
this .archetypeManager = Objects .requireNonNull (factoryHelper .getArchetypeManager ());
101
93
this .log = log ;
102
94
this .session = Objects .requireNonNull (session );
103
95
104
- artifactRepositories = Stream .concat (
105
- session .getCurrentProject ().getRemoteArtifactRepositories ().stream (),
106
- session .getCurrentProject ().getPluginArtifactRepositories ().stream ())
107
- .distinct ()
108
- .collect (Collectors .toList ());
109
96
remoteRepositories = Stream .concat (
110
97
session .getCurrentProject ().getRemoteProjectRepositories ().stream (),
111
98
session .getCurrentProject ().getRemotePluginRepositories ().stream ())
@@ -242,49 +229,76 @@ public void set(Artifact artifact, InputStream content) {
242
229
243
230
@ Override
244
231
public Metadata getMetadata (String path ) throws MetadataNotFoundException {
245
- path = StringUtils .strip (path , "/" );
246
- Metadata metadata = new Metadata ();
247
- boolean foundSomething = false ;
248
-
249
- // is this path a groupId:artifactId pair?
250
- int slashIndex = path .lastIndexOf ('/' );
251
- String artifactId = slashIndex == -1 ? null : path .substring (slashIndex + 1 );
252
- String groupId = slashIndex == -1 ? null : path .substring (0 , slashIndex ).replace ('/' , '.' );
253
- if (!StringUtils .isEmpty (artifactId ) && !StringUtils .isEmpty (groupId )) {
254
- org .apache .maven .artifact .Artifact artifact = createDependencyArtifact (groupId , artifactId );
255
- ArtifactRepositoryMetadata artifactRepositoryMetadata = new ArtifactRepositoryMetadata (artifact );
256
- try {
257
- repositoryMetadataManager .resolve (
258
- artifactRepositoryMetadata , artifactRepositories , session .getLocalRepository ());
259
-
260
- final Metadata artifactMetadata = artifactRepositoryMetadata .getMetadata ();
261
- if (artifactMetadata .getVersioning () != null ) {
262
- foundSomething = true ;
263
- if (StringUtils .isEmpty (metadata .getGroupId ())) {
264
- metadata .setGroupId (groupId );
265
- metadata .setArtifactId (artifactId );
266
- }
267
- metadata .merge (artifactMetadata );
268
- for (String v : artifactMetadata .getVersioning ().getVersions ()) {
269
- addResolved (path + "/" + v );
270
- }
232
+ LinkedList <String > pathItems =
233
+ new LinkedList <>(Arrays .asList (StringUtils .strip (path , "/" ).split ("/" )));
234
+
235
+ String version ;
236
+ String artifactId ;
237
+ String groupId ;
238
+
239
+ org .eclipse .aether .metadata .Metadata .Nature metadataNature ;
240
+
241
+ if (pathItems .getLast ().endsWith ("-SNAPSHOT" )) {
242
+ // V level metadata request
243
+ if (pathItems .size () < 3 ) {
244
+ // at least we need G:A:V
245
+ throw new MetadataNotFoundException (path );
246
+ }
247
+ metadataNature = org .eclipse .aether .metadata .Metadata .Nature .SNAPSHOT ;
248
+ version = pathItems .pollLast ();
249
+ artifactId = pathItems .pollLast ();
250
+ } else {
251
+ // A or G level metadata request
252
+ metadataNature = org .eclipse .aether .metadata .Metadata .Nature .RELEASE_OR_SNAPSHOT ;
253
+ version = null ;
254
+ artifactId = null ;
255
+ }
256
+
257
+ groupId = String .join ("." , pathItems );
258
+
259
+ org .eclipse .aether .metadata .Metadata requestedMetadata =
260
+ new DefaultMetadata (groupId , artifactId , version , "maven-metadata.xml" , metadataNature );
261
+ List <MetadataRequest > requests = new ArrayList <>();
262
+ for (RemoteRepository repo : remoteRepositories ) {
263
+ MetadataRequest request = new MetadataRequest ();
264
+ request .setMetadata (requestedMetadata );
265
+ request .setRepository (repo );
266
+ requests .add (request );
267
+ }
268
+
269
+ List <MetadataResult > metadataResults =
270
+ repositorySystem .resolveMetadata (session .getRepositorySession (), requests );
271
+
272
+ Metadata resultMetadata = null ;
273
+ for (MetadataResult result : metadataResults ) {
274
+ if (!result .isResolved ()) {
275
+ continue ;
276
+ }
277
+ Metadata metadata = readMetadata (result .getMetadata ().getFile ());
278
+ if (metadata != null ) {
279
+ if (resultMetadata == null ) {
280
+ resultMetadata = metadata ;
281
+ } else {
282
+ resultMetadata .merge (metadata );
271
283
}
272
- } catch (RepositoryMetadataResolutionException e ) {
273
- log .debug (e );
274
284
}
275
285
}
276
286
277
- if (! foundSomething ) {
287
+ if (resultMetadata == null ) {
278
288
throw new MetadataNotFoundException (path );
279
289
}
290
+
280
291
addResolved (path );
281
- return metadata ;
292
+ return resultMetadata ;
282
293
}
283
294
284
- private org .apache .maven .artifact .Artifact createDependencyArtifact (String groupId , String artifactId ) {
285
-
286
- return new org .apache .maven .artifact .DefaultArtifact (
287
- groupId , artifactId , ANY_VERSION , "compile" , "pom" , "" , null );
295
+ private Metadata readMetadata (File file ) {
296
+ try (InputStream in = Files .newInputStream (file .toPath ())) {
297
+ return new MetadataXpp3Reader ().read (in );
298
+ } catch (IOException | XmlPullParserException e ) {
299
+ log .warn ("Error reading metadata from file: " + file , e );
300
+ }
301
+ return null ;
288
302
}
289
303
290
304
@ Override
0 commit comments