Skip to content

Commit c60640f

Browse files
Maven Archetype 3.3.1 compatibility (#1076)
- 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>
1 parent e20a813 commit c60640f

File tree

2 files changed

+33
-15
lines changed
  • maven-plugins/helidon-archetype-maven-plugin

2 files changed

+33
-15
lines changed

maven-plugins/helidon-archetype-maven-plugin/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<groupId>org.apache.maven</groupId>
9595
<artifactId>maven-archiver</artifactId>
9696
</dependency>
97+
<!--suppress VulnerableLibrariesLocal -->
9798
<dependency>
9899
<groupId>org.codehaus.plexus</groupId>
99100
<artifactId>plexus-archiver</artifactId>

maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/EngineFacade.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.maven.artifact.versioning.ComparableVersion;
4040
import org.apache.maven.project.ProjectBuildingRequest;
4141
import org.eclipse.aether.RepositorySystemSession;
42+
import org.eclipse.aether.repository.RemoteRepository;
4243

4344
import static java.util.Collections.emptyMap;
4445

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

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+
8496
File localRepo = aether.repoSession().getLocalRepository().getBasedir();
8597

8698
// enable mvn:// URL support
@@ -167,4 +179,9 @@ private static <T> Optional<T> invoke(Object object, String methodName) {
167179
throw new RuntimeException(ex);
168180
}
169181
}
182+
183+
@SuppressWarnings("unchecked")
184+
private static <T> List<T> asListOf(List<?> list, Class<T> type) {
185+
return (List<T>) list;
186+
}
170187
}

0 commit comments

Comments
 (0)