11package com .redhat .hacbs .container .deploy .mavenrepository ;
22
3+ import static org .apache .commons .lang3 .StringUtils .isNotEmpty ;
4+
35import java .io .File ;
46import java .io .IOException ;
57import java .nio .file .FileVisitResult ;
1113import java .util .regex .Matcher ;
1214import java .util .regex .Pattern ;
1315
14- import org .apache .maven .repository .internal .MavenRepositorySystemUtils ;
15- import org .eclipse .aether .DefaultRepositorySystemSession ;
1616import org .eclipse .aether .RepositorySystem ;
17+ import org .eclipse .aether .RepositorySystemSession ;
1718import org .eclipse .aether .artifact .Artifact ;
1819import org .eclipse .aether .artifact .DefaultArtifact ;
1920import org .eclipse .aether .deployment .DeployRequest ;
2021import org .eclipse .aether .deployment .DeploymentException ;
21- import org .eclipse .aether .repository .LocalRepository ;
2222import org .eclipse .aether .repository .RemoteRepository ;
2323import org .eclipse .aether .util .repository .AuthenticationBuilder ;
2424
@@ -37,33 +37,44 @@ public class MavenRepositoryDeployer {
3737
3838 private final RepositorySystem system ;
3939
40- private final DefaultRepositorySystemSession session ;
40+ private final RepositorySystemSession session ;
4141
42+ private final String serverId ;
4243
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 )
4445 throws BootstrapMavenException {
4546 this .username = username ;
4647 this .password = password ;
4748 this .repository = repository ;
4849 this .artifacts = artifacts ;
50+ this .serverId = serverId ;
4951
5052 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 ();
5756 }
5857
5958 public void deploy ()
6059 throws IOException {
61- RemoteRepository distRepo = new RemoteRepository .Builder ("repo" ,
60+ RemoteRepository result ;
61+ RemoteRepository initial = new RemoteRepository .Builder (serverId ,
6262 "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 );
6778
6879 Files .walkFileTree (artifacts ,
6980 new SimpleFileVisitor <>() {
@@ -75,8 +86,11 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
7586 List <Path > files = stream .sorted ().toList ();
7687 boolean hasPom = files .stream ().anyMatch (s -> s .toString ().endsWith (".pom" ));
7788 if (hasPom ) {
78-
7989 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.
8094 String group = relative .getParent ().getParent ().toString ().replace (File .separatorChar ,
8195 '.' );
8296 String artifact = relative .getParent ().getFileName ().toString ();
@@ -86,16 +100,16 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
86100 + version );
87101 Pattern p = Pattern
88102 .compile (artifact + "-" + version + "(-(\\ w+))?\\ .(\\ w+)" );
89-
90103 DeployRequest deployRequest = new DeployRequest ();
91- deployRequest .setRepository (distRepo );
104+ deployRequest .setRepository (result );
92105 for (var i : files ) {
93106 Matcher matcher = p .matcher (i .getFileName ().toString ());
94107 if (matcher .matches ()) {
95108 Artifact jarArtifact = new DefaultArtifact (group , artifact ,
96109 matcher .group (2 ),
97110 matcher .group (3 ),
98111 version );
112+ Log .infof ("Uploading %s" , jarArtifact );
99113 jarArtifact = jarArtifact .setFile (i .toFile ());
100114 deployRequest .addArtifact (jarArtifact );
101115 }
0 commit comments