Skip to content

Commit 5d56838

Browse files
committed
Cleaned this up just a bit to leverage the existing maven RepositoryFactory which handles injection / resolution from Maven rather than reyling on looping through the list of project repositories.
In my configurations, I typically don't even include a `<repositories>` block and only include the definitions in the gitflow-helper-maven-plugin configuration. Without the repos existing in the project, the existing resolution in the original PR would fail. So I think this cleans up the code a bit, centralizes the repo parsing a bit more, etc. I also added some logging... always a good thing, right?
1 parent 331ce1b commit 5d56838

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/main/java/com/e_gineering/maven/gitflowhelper/AbstractGitflowBasedRepositoryMojo.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.eclipse.aether.RepositorySystemSession;
1313
import org.eclipse.aether.artifact.DefaultArtifact;
1414
import org.eclipse.aether.impl.ArtifactResolver;
15-
import org.eclipse.aether.repository.Authentication;
1615
import org.eclipse.aether.repository.LocalRepository;
1716
import org.eclipse.aether.repository.RemoteRepository;
1817
import org.eclipse.aether.resolution.ArtifactRequest;
@@ -88,6 +87,10 @@ protected ArtifactRepository getDeploymentRepository(final String altRepository)
8887
"Invalid syntax for repository. Use \"id::layout::url::unique\".");
8988
}
9089

90+
if (getLog().isDebugEnabled()) {
91+
getLog().debug("Getting maven deployment repository (to target artifacts) for: " + altRepository);
92+
}
93+
9194
String id = matcher.group(1).trim();
9295
String layout = matcher.group(2).trim();
9396
String url = matcher.group(3).trim();
@@ -107,32 +110,30 @@ protected ArtifactRepository getDeploymentRepository(final String altRepository)
107110
* @throws MojoFailureException
108111
*/
109112
private RemoteRepository getRepository(final String altRepository) throws MojoExecutionException, MojoFailureException {
110-
Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher(altRepository);
111-
if (!matcher.matches()) {
112-
throw new MojoFailureException(altRepository, "Invalid syntax for repository.",
113-
"Invalid syntax for repository. Use \"id::layout::url::unique\".");
113+
if (getLog().isDebugEnabled()) {
114+
getLog().debug("Creating remote Aether repository (to resolve remote artifacts) for: " + altRepository);
114115
}
116+
// Get an appropriate injected ArtifactRepository. (This resolves authentication in the 'normal' manner from Maven)
117+
ArtifactRepository remoteArtifactRepo = getDeploymentRepository(altRepository);
115118

116-
String id = matcher.group(1).trim();
117-
String layout = matcher.group(2).trim();
118-
String url = matcher.group(3).trim();
119-
Authentication remoteRepoAuthentication = null;
120-
121-
for (ArtifactRepository artifactRepository : project.getRemoteArtifactRepositories()) {
122-
if (artifactRepository.getId().equals(id)){
123-
final org.apache.maven.artifact.repository.Authentication authentication = artifactRepository.getAuthentication();
124-
AuthenticationBuilder authenticationBuilder = new AuthenticationBuilder();
125-
authenticationBuilder
126-
.addUsername(authentication.getUsername())
127-
.addPassword(authentication.getPassword())
128-
.addPrivateKey(authentication.getPrivateKey(), authentication.getPassphrase());
129-
remoteRepoAuthentication = authenticationBuilder.build();
119+
if (getLog().isDebugEnabled()) {
120+
getLog().debug("Resolved maven deployment repository. Transcribing to Aether Repository...");
121+
}
122+
123+
RemoteRepository.Builder remoteRepoBuilder = new RemoteRepository.Builder(remoteArtifactRepo.getId(), remoteArtifactRepo.getLayout().getId(), remoteArtifactRepo.getUrl());
130124

125+
// Add authentication.
126+
if (remoteArtifactRepo.getAuthentication() != null) {
127+
if (getLog().isDebugEnabled()) {
128+
getLog().debug("Maven deployment repsoitory has Authentication. Transcribing to Aether Authentication...");
131129
}
130+
remoteRepoBuilder.setAuthentication(new AuthenticationBuilder().addUsername(remoteArtifactRepo.getAuthentication().getUsername())
131+
.addPassword(remoteArtifactRepo.getAuthentication().getPassword())
132+
.addPrivateKey(remoteArtifactRepo.getAuthentication().getPrivateKey(), remoteArtifactRepo.getAuthentication().getPassphrase())
133+
.build());
132134
}
133135

134-
135-
return new RemoteRepository.Builder(id, layout, url).setAuthentication(remoteRepoAuthentication).build();
136+
return remoteRepoBuilder.build();
136137
}
137138

138139
private String getCoordinates(ArtifactResult result) {

0 commit comments

Comments
 (0)