Skip to content

Commit

Permalink
Maven Archetype 3.3.1 compatibility (#1076)
Browse files Browse the repository at this point in the history
- Pre-emptive updates to support upcoming Maven Archetype 3.3.1 that restores compatibility pre 3.3.0
- Update reflection logic to support getRemoteArtifactRepositories as List<ArtifactRepository> or List<RemoteRepository>
  • Loading branch information
romain-grecourt authored Oct 11, 2024
1 parent e20a813 commit c60640f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
1 change: 1 addition & 0 deletions maven-plugins/helidon-archetype-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-archiver</artifactId>
</dependency>
<!--suppress VulnerableLibrariesLocal -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.project.ProjectBuildingRequest;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;

import static java.util.Collections.emptyMap;

Expand Down Expand Up @@ -66,21 +67,32 @@ public static void generate(ArchetypeGenerationRequest request, List<String> dep
checkMavenVersion();
checkJavaVersion();

Aether aether = EngineFacade.<RepositorySystemSession>invoke(request, "getRepositorySession")
// archetype-common >= 3.3.0
.map(repoSession -> new Aether(repoSession, request.getRemoteArtifactRepositories()))
// archetype-common < 3.3.0
.orElseGet(() -> {
ProjectBuildingRequest pbr = EngineFacade.<ProjectBuildingRequest>invoke(request, "getProjectBuildingRequest")
.orElseThrow(() -> new IllegalStateException("Unable to get project building request"));
RepositorySystemSession repoSession = EngineFacade.<RepositorySystemSession>invoke(pbr,
"getRepositorySession")
.orElseThrow(() -> new IllegalStateException("Unable to get repository system session"));
List<ArtifactRepository> artifactRepos = EngineFacade.<List<ArtifactRepository>>invoke(request,
"getRemoteArtifactRepositories")
.orElseThrow(() -> new IllegalStateException("Unable to get artifact repositories"));
return new Aether(repoSession, artifactRepos, true);
});
// getRepositorySession only exists in archetype-common >= 3.3.0
RepositorySystemSession repoSession = EngineFacade.<RepositorySystemSession>invoke(request, "getRepositorySession")
// getProjectBuildingRequest was removed in archetype-common == 3.3.0
.or(() -> EngineFacade.<ProjectBuildingRequest>invoke(request, "getProjectBuildingRequest")
.map(ProjectBuildingRequest::getRepositorySession))
.orElseThrow(() -> new IllegalStateException("Unable to get repository system session"));

// getRemoteRepositories only exists in archetype-common >= 3.3.1
Aether aether = EngineFacade.<List<RemoteRepository>>invoke(request, "getRemoteRepositories")
.map(remoteRepos -> new Aether(repoSession, remoteRepos))
.or(() -> EngineFacade.<List<?>>invoke(request, "getRemoteArtifactRepositories")
.map(repos -> {
Object repo = repos.isEmpty() ? null : repos.get(0);
if (repo instanceof ArtifactRepository) {
// getRemoteArtifactRepositories returns List<ArtifactRepository> in archetype-common != 3.3.0
return new Aether(repoSession, asListOf(repos, ArtifactRepository.class), true);
} else if (repo instanceof RemoteRepository) {
// getRemoteArtifactRepositories returns List<RemoteRepository> in archetype-common == 3.3.0
return new Aether(repoSession, asListOf(repos, RemoteRepository.class));
} else {
// empty repository, or unsupported repository type
return new Aether(repoSession, List.of());
}
}))
.orElseThrow(() -> new IllegalStateException("Unable to initialize aether"));

File localRepo = aether.repoSession().getLocalRepository().getBasedir();

// enable mvn:// URL support
Expand Down Expand Up @@ -167,4 +179,9 @@ private static <T> Optional<T> invoke(Object object, String methodName) {
throw new RuntimeException(ex);
}
}

@SuppressWarnings("unchecked")
private static <T> List<T> asListOf(List<?> list, Class<T> type) {
return (List<T>) list;
}
}

0 comments on commit c60640f

Please sign in to comment.