16
16
import org .eclipse .aether .RepositorySystemSession ;
17
17
import org .eclipse .aether .artifact .DefaultArtifact ;
18
18
import org .eclipse .aether .internal .impl .EnhancedLocalRepositoryManagerFactory ;
19
+ import org .eclipse .aether .repository .AuthenticationContext ;
20
+ import org .eclipse .aether .repository .AuthenticationSelector ;
19
21
import org .eclipse .aether .repository .LocalRepository ;
20
22
import org .eclipse .aether .repository .RemoteRepository ;
21
23
import org .eclipse .aether .repository .RepositoryPolicy ;
@@ -49,7 +51,7 @@ private static PrintWriter newPrintWriter(File catalog) throws FileNotFoundExcep
49
51
Objects .requireNonNull (catalog , "catalog must not be null" );
50
52
return new PrintWriter (new OutputStreamWriter (new FileOutputStream (catalog ), UTF_8 ));
51
53
}
52
-
54
+
53
55
@ Parameter (property = "releaseDeploymentRepositoryId" , required = true )
54
56
String releaseDeploymentRepository ;
55
57
@@ -58,25 +60,25 @@ private static PrintWriter newPrintWriter(File catalog) throws FileNotFoundExcep
58
60
59
61
@ Parameter (property = "snapshotDeploymentRepositoryId" , required = true )
60
62
String snapshotDeploymentRepository ;
61
-
63
+
62
64
@ Parameter (property = "otherDeployBranchPattern" , required = false )
63
65
String otherDeployBranchPattern ;
64
66
65
67
@ Parameter (defaultValue = "+" , required = true )
66
68
String otherBranchVersionDelimiter ;
67
-
69
+
68
70
@ Parameter (defaultValue = "${repositorySystemSession}" , required = true )
69
71
RepositorySystemSession repositorySystemSession ;
70
-
72
+
71
73
@ Parameter (defaultValue = "${project.build.directory}" , required = true )
72
74
File buildDirectory ;
73
-
75
+
74
76
@ Component
75
77
private RepositorySystem repositorySystem ;
76
-
78
+
77
79
@ Component
78
80
private EnhancedLocalRepositoryManagerFactory localRepositoryManagerFactory ;
79
-
81
+
80
82
@ Component
81
83
private MavenProjectHelper projectHelper ;
82
84
@@ -99,11 +101,47 @@ ArtifactRepository getDeploymentRepository(final String id) throws MojoFailureEx
99
101
Optional <ArtifactRepository > mirroredRepo = project .getRemoteArtifactRepositories ().stream ()
100
102
.flatMap (r -> r .getMirroredRepositories ().stream ()).filter (r -> r .getId ().equals (id )).findFirst ();
101
103
if (mirroredRepo .isPresent ()) {
102
- return mirroredRepo .get ();
104
+ ArtifactRepository artifactRepository = mirroredRepo .get ();
105
+ if (artifactRepository .getAuthentication () == null ) {
106
+ artifactRepository .setAuthentication (getAuthentication (artifactRepository ));
107
+ }
108
+ return artifactRepository ;
103
109
}
104
110
throw new MojoFailureException ("No Repository with id `" + id + "` is defined." );
105
111
}
106
112
113
+ /**
114
+ * Get the authentication object for an artifact repository
115
+ * @param repository the artifact repository
116
+ * @return der authentication object or @Code{null} when no authentication was found
117
+ */
118
+ private org .apache .maven .artifact .repository .Authentication getAuthentication ( ArtifactRepository repository )
119
+ {
120
+ AuthenticationSelector selector = repositorySystemSession .getAuthenticationSelector ();
121
+ if (selector == null )
122
+ {
123
+ return null ;
124
+ }
125
+ RemoteRepository repo = RepositoryUtils .toRepo (repository );
126
+ org .eclipse .aether .repository .Authentication auth = selector .getAuthentication (repo );
127
+ if (auth == null )
128
+ {
129
+ return null ;
130
+ }
131
+ RemoteRepository repoWithAuth = new RemoteRepository .Builder (repo ).setAuthentication (auth ).build ();
132
+ AuthenticationContext authCtx = AuthenticationContext .forRepository (repositorySystemSession , repoWithAuth );
133
+ if (authCtx == null ) {
134
+ return null ;
135
+ }
136
+ org .apache .maven .artifact .repository .Authentication result =
137
+ new org .apache .maven .artifact .repository .Authentication (authCtx .get (AuthenticationContext .USERNAME ),
138
+ authCtx .get (AuthenticationContext .PASSWORD ));
139
+ result .setPrivateKey (authCtx .get (AuthenticationContext .PRIVATE_KEY_PATH ));
140
+ result .setPassphrase (authCtx .get (AuthenticationContext .PRIVATE_KEY_PASSPHRASE ));
141
+ authCtx .close ();
142
+ return result ;
143
+ }
144
+
107
145
/**
108
146
* Creates and attaches an artifact containing a list of attached artifacts, each line in the file contains
109
147
* group:artifact:type:classifier:version
@@ -173,7 +211,7 @@ private void catalogArtifact(PrintWriter writer, Artifact artifact) {
173
211
*/
174
212
void attachExistingArtifacts (@ Nullable final String sourceRepository , final boolean disableLocal )
175
213
throws MojoExecutionException , MojoFailureException {
176
-
214
+
177
215
List <ArtifactRepository > remoteArtifactRepositories = new ArrayList <>();
178
216
Optional <ArtifactRepository > repo = project .getRemoteArtifactRepositories ().stream ().filter (r -> r .getId ().equals (sourceRepository )).findFirst ();
179
217
if (repo .isPresent ()) {
@@ -185,7 +223,7 @@ void attachExistingArtifacts(@Nullable final String sourceRepository, final bool
185
223
getLog ().debug ("Resolving existing artifacts from local repository only." );
186
224
}
187
225
List <RemoteRepository > remoteRepositories = RepositoryUtils .toRepos (remoteArtifactRepositories );
188
-
226
+
189
227
// A place to store our resolved files...
190
228
List <ArtifactResult > resolvedArtifacts = new ArrayList <>();
191
229
@@ -320,8 +358,8 @@ private boolean hasFile(@Nullable Artifact artifact) {
320
358
&& project .getArtifact ().getFile ().exists ()
321
359
&& project .getArtifact ().getFile ().isFile ();
322
360
}
323
-
324
-
361
+
362
+
325
363
private String getCoordinates (ArtifactResult artifactResult ) {
326
364
return getCoordinates (
327
365
emptyToNull (artifactResult .getArtifact ().getGroupId ()),
@@ -331,25 +369,25 @@ private String getCoordinates(ArtifactResult artifactResult) {
331
369
emptyToNull (artifactResult .getArtifact ().getClassifier ())
332
370
);
333
371
}
334
-
372
+
335
373
private static String emptyToNull (final String s ) {
336
374
return StringUtils .isBlank (s ) ? null : s ;
337
375
}
338
-
376
+
339
377
private String getCoordinates (Artifact artifact ) {
340
378
getLog ().debug (" Encoding Coordinates For: " + artifact );
341
-
379
+
342
380
// Get the extension according to the artifact type.
343
381
String extension = artifact .getArtifactHandler ().getExtension ();
344
-
382
+
345
383
return getCoordinates (
346
384
artifact .getGroupId (),
347
385
artifact .getArtifactId (),
348
386
project .getVersion (),
349
387
extension , artifact .hasClassifier () ? artifact .getClassifier () : null
350
388
);
351
389
}
352
-
390
+
353
391
private String getCoordinates (String groupId ,
354
392
String artifactId ,
355
393
String version ,
@@ -358,7 +396,7 @@ private String getCoordinates(String groupId,
358
396
Objects .requireNonNull (groupId , "groupId must not be null" );
359
397
Objects .requireNonNull (artifactId , "artifactId must not be null" );
360
398
Objects .requireNonNull (version , "version must not be null" );
361
-
399
+
362
400
StringBuilder result = new StringBuilder ();
363
401
for (String s : new String []{groupId , artifactId , extension , classifier , version }) {
364
402
if (s != null ) {
0 commit comments