Skip to content

Commit 70b3f62

Browse files
committed
Merge branch '3.3.x' into 3.4.x
Closes gh-45951
2 parents e950a08 + fdbddcf commit 70b3f62

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/MavenMetadataVersionResolver.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import javax.xml.xpath.XPathFactory;
3232

3333
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
34+
import org.gradle.api.artifacts.repositories.PasswordCredentials;
35+
import org.gradle.api.credentials.Credentials;
36+
import org.gradle.internal.artifacts.repositories.AuthenticationSupportedInternal;
3437
import org.w3c.dom.Document;
3538
import org.w3c.dom.NodeList;
3639
import org.xml.sax.InputSource;
@@ -83,9 +86,10 @@ private Set<String> resolveVersions(String groupId, String artifactId, MavenArti
8386
.toUri();
8487
try {
8588
HttpHeaders headers = new HttpHeaders();
86-
String username = repository.getCredentials().getUsername();
89+
PasswordCredentials credentials = credentialsOf(repository);
90+
String username = (credentials != null) ? credentials.getUsername() : null;
8791
if (username != null) {
88-
headers.setBasicAuth(username, repository.getCredentials().getPassword());
92+
headers.setBasicAuth(username, credentials.getPassword());
8993
}
9094
HttpEntity<Void> request = new HttpEntity<>(headers);
9195
String metadata = this.rest.exchange(url, HttpMethod.GET, request, String.class).getBody();
@@ -112,4 +116,25 @@ private Set<String> resolveVersions(String groupId, String artifactId, MavenArti
112116
return versions;
113117
}
114118

119+
/**
120+
* Retrives the configured credentials of the given {@code repository}. We cannot use
121+
* {@link MavenArtifactRepository#getCredentials()} as, if the repository has no
122+
* credentials, it has the unwanted side-effect of assigning an empty set of username
123+
* and password credentials to the repository which may cause subsequent "Username
124+
* must not be null!" failures.
125+
* @param repository the repository that is the source of the credentials
126+
* @return the configured password credentials or {@code null}
127+
*/
128+
private PasswordCredentials credentialsOf(MavenArtifactRepository repository) {
129+
Credentials credentials = ((AuthenticationSupportedInternal) repository).getConfiguredCredentials().getOrNull();
130+
if (credentials != null) {
131+
if (credentials instanceof PasswordCredentials passwordCredentials) {
132+
return passwordCredentials;
133+
}
134+
throw new IllegalStateException("Repository '%s (%s)' has credentials '%s' that are not PasswordCredentials"
135+
.formatted(repository.getName(), repository.getUrl(), credentials));
136+
}
137+
return null;
138+
}
139+
115140
}

0 commit comments

Comments
 (0)