|
39 | 39 | import org.apache.maven.artifact.versioning.ComparableVersion; |
40 | 40 | import org.apache.maven.project.ProjectBuildingRequest; |
41 | 41 | import org.eclipse.aether.RepositorySystemSession; |
| 42 | +import org.eclipse.aether.repository.RemoteRepository; |
42 | 43 |
|
43 | 44 | import static java.util.Collections.emptyMap; |
44 | 45 |
|
@@ -66,21 +67,32 @@ public static void generate(ArchetypeGenerationRequest request, List<String> dep |
66 | 67 | checkMavenVersion(); |
67 | 68 | checkJavaVersion(); |
68 | 69 |
|
69 | | - Aether aether = EngineFacade.<RepositorySystemSession>invoke(request, "getRepositorySession") |
70 | | - // archetype-common >= 3.3.0 |
71 | | - .map(repoSession -> new Aether(repoSession, request.getRemoteArtifactRepositories())) |
72 | | - // archetype-common < 3.3.0 |
73 | | - .orElseGet(() -> { |
74 | | - ProjectBuildingRequest pbr = EngineFacade.<ProjectBuildingRequest>invoke(request, "getProjectBuildingRequest") |
75 | | - .orElseThrow(() -> new IllegalStateException("Unable to get project building request")); |
76 | | - RepositorySystemSession repoSession = EngineFacade.<RepositorySystemSession>invoke(pbr, |
77 | | - "getRepositorySession") |
78 | | - .orElseThrow(() -> new IllegalStateException("Unable to get repository system session")); |
79 | | - List<ArtifactRepository> artifactRepos = EngineFacade.<List<ArtifactRepository>>invoke(request, |
80 | | - "getRemoteArtifactRepositories") |
81 | | - .orElseThrow(() -> new IllegalStateException("Unable to get artifact repositories")); |
82 | | - return new Aether(repoSession, artifactRepos, true); |
83 | | - }); |
| 70 | + // getRepositorySession only exists in archetype-common >= 3.3.0 |
| 71 | + RepositorySystemSession repoSession = EngineFacade.<RepositorySystemSession>invoke(request, "getRepositorySession") |
| 72 | + // getProjectBuildingRequest was removed in archetype-common == 3.3.0 |
| 73 | + .or(() -> EngineFacade.<ProjectBuildingRequest>invoke(request, "getProjectBuildingRequest") |
| 74 | + .map(ProjectBuildingRequest::getRepositorySession)) |
| 75 | + .orElseThrow(() -> new IllegalStateException("Unable to get repository system session")); |
| 76 | + |
| 77 | + // getRemoteRepositories only exists in archetype-common >= 3.3.1 |
| 78 | + Aether aether = EngineFacade.<List<RemoteRepository>>invoke(request, "getRemoteRepositories") |
| 79 | + .map(remoteRepos -> new Aether(repoSession, remoteRepos)) |
| 80 | + .or(() -> EngineFacade.<List<?>>invoke(request, "getRemoteArtifactRepositories") |
| 81 | + .map(repos -> { |
| 82 | + Object repo = repos.isEmpty() ? null : repos.get(0); |
| 83 | + if (repo instanceof ArtifactRepository) { |
| 84 | + // getRemoteArtifactRepositories returns List<ArtifactRepository> in archetype-common != 3.3.0 |
| 85 | + return new Aether(repoSession, asListOf(repos, ArtifactRepository.class), true); |
| 86 | + } else if (repo instanceof RemoteRepository) { |
| 87 | + // getRemoteArtifactRepositories returns List<RemoteRepository> in archetype-common == 3.3.0 |
| 88 | + return new Aether(repoSession, asListOf(repos, RemoteRepository.class)); |
| 89 | + } else { |
| 90 | + // empty repository, or unsupported repository type |
| 91 | + return new Aether(repoSession, List.of()); |
| 92 | + } |
| 93 | + })) |
| 94 | + .orElseThrow(() -> new IllegalStateException("Unable to initialize aether")); |
| 95 | + |
84 | 96 | File localRepo = aether.repoSession().getLocalRepository().getBasedir(); |
85 | 97 |
|
86 | 98 | // enable mvn:// URL support |
@@ -167,4 +179,9 @@ private static <T> Optional<T> invoke(Object object, String methodName) { |
167 | 179 | throw new RuntimeException(ex); |
168 | 180 | } |
169 | 181 | } |
| 182 | + |
| 183 | + @SuppressWarnings("unchecked") |
| 184 | + private static <T> List<T> asListOf(List<?> list, Class<T> type) { |
| 185 | + return (List<T>) list; |
| 186 | + } |
170 | 187 | } |
0 commit comments