1
1
package com .redhat .hacbs .container .deploy .mavenrepository ;
2
2
3
+ import static org .apache .commons .lang3 .StringUtils .isNotEmpty ;
4
+
3
5
import java .io .File ;
4
6
import java .io .IOException ;
5
7
import java .nio .file .FileVisitResult ;
11
13
import java .util .regex .Matcher ;
12
14
import java .util .regex .Pattern ;
13
15
14
- import org .apache .maven .repository .internal .MavenRepositorySystemUtils ;
15
- import org .eclipse .aether .DefaultRepositorySystemSession ;
16
16
import org .eclipse .aether .RepositorySystem ;
17
+ import org .eclipse .aether .RepositorySystemSession ;
17
18
import org .eclipse .aether .artifact .Artifact ;
18
19
import org .eclipse .aether .artifact .DefaultArtifact ;
19
20
import org .eclipse .aether .deployment .DeployRequest ;
20
21
import org .eclipse .aether .deployment .DeploymentException ;
21
- import org .eclipse .aether .repository .LocalRepository ;
22
22
import org .eclipse .aether .repository .RemoteRepository ;
23
23
import org .eclipse .aether .util .repository .AuthenticationBuilder ;
24
24
@@ -37,33 +37,44 @@ public class MavenRepositoryDeployer {
37
37
38
38
private final RepositorySystem system ;
39
39
40
- private final DefaultRepositorySystemSession session ;
40
+ private final RepositorySystemSession session ;
41
41
42
+ private final String serverId ;
42
43
43
- public MavenRepositoryDeployer (BootstrapMavenContext mvnCtx , String username , String password , String repository , Path artifacts )
44
+ public MavenRepositoryDeployer (BootstrapMavenContext mvnCtx , String username , String password , String repository , String serverId , Path artifacts )
44
45
throws BootstrapMavenException {
45
46
this .username = username ;
46
47
this .password = password ;
47
48
this .repository = repository ;
48
49
this .artifacts = artifacts ;
50
+ this .serverId = serverId ;
49
51
50
52
this .system = mvnCtx .getRepositorySystem ();
51
- this .session = MavenRepositorySystemUtils .newSession ();
52
-
53
- Log .infof ("Maven credentials are username '%s' and repository '%s'" , username , repository );
54
-
55
- // https://maven.apache.org/resolver/third-party-integrations.html states a local repository manager should be added.
56
- session .setLocalRepositoryManager (system .newLocalRepositoryManager (session , new LocalRepository (artifacts .toFile ())));
53
+ // Note - we were using MavenRepositorySystemUtils.newSession but that doesn't correctly process
54
+ // the settings.xml without an active project.
55
+ this .session = mvnCtx .getRepositorySystemSession ();
57
56
}
58
57
59
58
public void deploy ()
60
59
throws IOException {
61
- RemoteRepository distRepo = new RemoteRepository .Builder ("repo" ,
60
+ RemoteRepository result ;
61
+ RemoteRepository initial = new RemoteRepository .Builder (serverId ,
62
62
"default" ,
63
- repository )
64
- .setAuthentication (new AuthenticationBuilder ().addUsername (username )
65
- .addPassword (password ).build ())
66
- .build ();
63
+ repository ).build ();
64
+ RemoteRepository .Builder builder = new RemoteRepository .Builder (initial );
65
+
66
+ if (isNotEmpty (username )) {
67
+ builder .setAuthentication (new AuthenticationBuilder ().addUsername (username )
68
+ .addPassword (password ).build ());
69
+ } else {
70
+ builder .setAuthentication (session .getAuthenticationSelector ().getAuthentication (initial ));
71
+ }
72
+ if (initial .getProxy () == null ) {
73
+ builder .setProxy (session .getProxySelector ().getProxy (initial ));
74
+ }
75
+ result = builder .build ();
76
+
77
+ Log .infof ("Configured repository %s" , result );
67
78
68
79
Files .walkFileTree (artifacts ,
69
80
new SimpleFileVisitor <>() {
@@ -75,8 +86,11 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
75
86
List <Path > files = stream .sorted ().toList ();
76
87
boolean hasPom = files .stream ().anyMatch (s -> s .toString ().endsWith (".pom" ));
77
88
if (hasPom ) {
78
-
79
89
Path relative = artifacts .relativize (dir );
90
+ if (relative .getNameCount () <= 2 ) {
91
+ Log .errorf ("Invalid repository format. Local directory is '%s' with relative path '%s' and not enough components to calculate groupId and artifactId" , artifacts , relative );
92
+ }
93
+ // If we're in org/foobar/artifact/1.0 then the group is two up and the artifact is one up.
80
94
String group = relative .getParent ().getParent ().toString ().replace (File .separatorChar ,
81
95
'.' );
82
96
String artifact = relative .getParent ().getFileName ().toString ();
@@ -86,16 +100,16 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
86
100
+ version );
87
101
Pattern p = Pattern
88
102
.compile (artifact + "-" + version + "(-(\\ w+))?\\ .(\\ w+)" );
89
-
90
103
DeployRequest deployRequest = new DeployRequest ();
91
- deployRequest .setRepository (distRepo );
104
+ deployRequest .setRepository (result );
92
105
for (var i : files ) {
93
106
Matcher matcher = p .matcher (i .getFileName ().toString ());
94
107
if (matcher .matches ()) {
95
108
Artifact jarArtifact = new DefaultArtifact (group , artifact ,
96
109
matcher .group (2 ),
97
110
matcher .group (3 ),
98
111
version );
112
+ Log .infof ("Uploading %s" , jarArtifact );
99
113
jarArtifact = jarArtifact .setFile (i .toFile ());
100
114
deployRequest .addArtifact (jarArtifact );
101
115
}
0 commit comments