diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 6c47cc5a8..18c92c48c 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -89,6 +89,7 @@ jobs: artifact-name: helidon-cli-dist artifact-path: cli/impl/target/helidon-cli.zip run: | + mvn --version mvn ${MAVEN_ARGS} build-cache:go-offline mvn ${MAVEN_ARGS} -T8 \ -Dorg.slf4j.simpleLogger.showThreadName=true \ diff --git a/cli/tests/functional/src/test/java/io/helidon/build/cli/tests/CliMavenTest.java b/cli/tests/functional/src/test/java/io/helidon/build/cli/tests/CliMavenTest.java index e2a18dd7f..cdd18cf2f 100644 --- a/cli/tests/functional/src/test/java/io/helidon/build/cli/tests/CliMavenTest.java +++ b/cli/tests/functional/src/test/java/io/helidon/build/cli/tests/CliMavenTest.java @@ -95,7 +95,7 @@ void testWrongMavenVersion() throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Path mavenBinDir = mavenDirectory.resolve("apache-maven-3.1.1/bin"); List mavenArgs = List.of( - "archetype:generate", + "org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate", "-DinteractiveMode=false", "-DarchetypeGroupId=io.helidon.archetypes", "-DarchetypeArtifactId=helidon", @@ -266,7 +266,7 @@ private void runMissingValueTest(List args, String mavenVersion) throws private void missingArtifactGroupPackageValues(String mavenVersion) { List args = List.of( "-B", - "archetype:generate", + "org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate", "-DinteractiveMode=false", "-DarchetypeGroupId=io.helidon.archetypes", "-DarchetypeArtifactId=helidon", @@ -281,7 +281,7 @@ private void missingArtifactGroupPackageValues(String mavenVersion) { private void missingFlavorValue(String mavenVersion) { List args = List.of( "-B", - "archetype:generate", + "org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate", "-DinteractiveMode=false", "-DarchetypeGroupId=io.helidon.archetypes", "-DarchetypeArtifactId=helidon", @@ -298,7 +298,7 @@ private void missingFlavorValue(String mavenVersion) { private void missingBaseValue(String mavenVersion) { List args = List.of( "-B", - "archetype:generate", + "org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate", "-DinteractiveMode=false", "-DarchetypeGroupId=io.helidon.archetypes", "-DarchetypeArtifactId=helidon", diff --git a/maven-plugins/helidon-archetype-maven-plugin/pom.xml b/maven-plugins/helidon-archetype-maven-plugin/pom.xml index be2655035..2d437cc80 100644 --- a/maven-plugins/helidon-archetype-maven-plugin/pom.xml +++ b/maven-plugins/helidon-archetype-maven-plugin/pom.xml @@ -60,6 +60,11 @@ maven-core provided + + org.apache.maven.resolver + maven-resolver-util + compile + org.apache.maven maven-model diff --git a/maven-plugins/helidon-archetype-maven-plugin/src/it/projects/test1/src/main/archetype/files/pom.xml.mustache b/maven-plugins/helidon-archetype-maven-plugin/src/it/projects/test1/src/main/archetype/files/pom.xml.mustache index f20ae8d8e..2c2615c8d 100644 --- a/maven-plugins/helidon-archetype-maven-plugin/src/it/projects/test1/src/main/archetype/files/pom.xml.mustache +++ b/maven-plugins/helidon-archetype-maven-plugin/src/it/projects/test1/src/main/archetype/files/pom.xml.mustache @@ -29,7 +29,18 @@ UTF-8 - 1.8 - 1.8 + 11 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + + diff --git a/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/IntegrationTestMojo.java b/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/IntegrationTestMojo.java index b5c5a1c36..e8bed27c3 100644 --- a/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/IntegrationTestMojo.java +++ b/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/IntegrationTestMojo.java @@ -49,6 +49,7 @@ import io.helidon.build.common.ansi.AnsiConsoleInstaller; import io.helidon.build.maven.archetype.config.Validation; +import org.apache.maven.RepositoryUtils; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -58,17 +59,17 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; +import org.apache.maven.project.artifact.ProjectArtifact; import org.apache.maven.shared.invoker.DefaultInvocationRequest; import org.apache.maven.shared.invoker.InvocationOutputHandler; import org.apache.maven.shared.invoker.InvocationRequest; import org.apache.maven.shared.invoker.InvocationResult; import org.apache.maven.shared.invoker.Invoker; import org.apache.maven.shared.invoker.MavenInvocationException; -import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException; -import org.apache.maven.shared.transfer.project.NoFileAssignedException; -import org.apache.maven.shared.transfer.project.install.ProjectInstaller; -import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest; import org.codehaus.plexus.PlexusContainer; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.installation.InstallRequest; +import org.eclipse.aether.installation.InstallationException; import static io.helidon.build.common.FileUtils.ensureDirectory; import static io.helidon.build.common.FileUtils.unique; @@ -107,7 +108,6 @@ public class IntegrationTestMojo extends AbstractMojo { */ @Parameter(defaultValue = "${session}", readonly = true, required = true) private MavenSession session; - /** * Skip the integration test. */ @@ -244,7 +244,7 @@ public class IntegrationTestMojo extends AbstractMojo { private Map invokerEnvVars; @Component - private ProjectInstaller installer; + private RepositorySystem repoSystem; @Component private PluginContainerManager pluginContainerManager; @@ -495,11 +495,13 @@ private void mavenCompatGenerate(String archetypeGroupId, Path basedir) throws MojoExecutionException { // pre-install the archetype JAR so that the post-generate script can resolve it - ProjectInstallerRequest projectInstallerRequest = new ProjectInstallerRequest().setProject(project); try { - installer.install(session.getProjectBuildingRequest(), projectInstallerRequest); - } catch (IOException | ArtifactInstallerException | NoFileAssignedException ex) { - throw new MojoExecutionException("Unable to pre-install archetype artifact", ex); + InstallRequest request = new InstallRequest(); + request.addArtifact(RepositoryUtils.toArtifact(new ProjectArtifact(project))); + request.addArtifact(RepositoryUtils.toArtifact(project.getArtifact())); + repoSystem.install(session.getRepositorySession(), request); + } catch (InstallationException ex) { + throw new MojoExecutionException("Unable to pre-install project", ex); } PlexusContainer container = pluginContainerManager.create(MAVEN_ARCHETYPE_PLUGIN, @@ -529,7 +531,7 @@ private void invokePostArchetypeGenerationGoals(File basedir) throws IOException .setUserSettingsFile(session.getRequest().getUserSettingsFile()) .setLocalRepositoryDirectory(localRepo) .setBaseDirectory(basedir) - .setGoals(List.of(testGoal)) + .addArgs(List.of(testGoal)) .setProfiles(testProfiles) .setBatchMode(true) .setShowErrors(true) diff --git a/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/MavenArchetypeGenerator.java b/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/MavenArchetypeGenerator.java index be27757c3..13942f44f 100644 --- a/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/MavenArchetypeGenerator.java +++ b/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/MavenArchetypeGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Oracle and/or its affiliates. + * Copyright (c) 2023, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,7 +67,7 @@ static void generate(PlexusContainer container, .setPackage(properties.getProperty("package")) .setOutputDirectory(basedir.toString()) .setProperties(properties) - .setProjectBuildingRequest(session.getProjectBuildingRequest()); + .setRepositorySession(session.getRepositorySession()); ArchetypeGenerationResult result = new ArchetypeGenerationResult(); diff --git a/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/Aether.java b/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/Aether.java index 621b2da6e..5efef31f6 100644 --- a/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/Aether.java +++ b/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/Aether.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2022 Oracle and/or its affiliates. + * Copyright (c) 2020, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,17 +18,11 @@ import java.io.File; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; -import org.apache.maven.cli.transfer.Slf4jMavenTransferListener; import org.apache.maven.repository.internal.MavenRepositorySystemUtils; -import org.apache.maven.settings.Mirror; -import org.apache.maven.settings.Settings; -import org.apache.maven.settings.building.DefaultSettingsBuilder; -import org.apache.maven.settings.building.DefaultSettingsBuilderFactory; -import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; -import org.apache.maven.settings.building.SettingsBuildingException; import org.apache.maven.wagon.Wagon; import org.apache.maven.wagon.providers.http.HttpWagon; import org.codehaus.plexus.DefaultPlexusContainer; @@ -36,18 +30,13 @@ import org.codehaus.plexus.PlexusContainerException; import org.codehaus.plexus.component.composition.CycleDetectedInComponentGraphException; import org.codehaus.plexus.component.repository.ComponentDescriptor; -import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.collection.CollectRequest; import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory; import org.eclipse.aether.graph.Dependency; import org.eclipse.aether.internal.transport.wagon.PlexusWagonProvider; -import org.eclipse.aether.repository.Authentication; -import org.eclipse.aether.repository.LocalRepository; -import org.eclipse.aether.repository.MirrorSelector; -import org.eclipse.aether.repository.Proxy; -import org.eclipse.aether.repository.ProxySelector; import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.repository.RepositoryPolicy; import org.eclipse.aether.resolution.ArtifactRequest; @@ -59,9 +48,6 @@ import org.eclipse.aether.transport.wagon.WagonProvider; import org.eclipse.aether.transport.wagon.WagonTransporterFactory; import org.eclipse.aether.util.filter.DependencyFilterUtils; -import org.eclipse.aether.util.repository.AuthenticationBuilder; -import org.eclipse.aether.util.repository.DefaultMirrorSelector; -import org.eclipse.aether.util.repository.DefaultProxySelector; import static java.util.stream.Collectors.toList; @@ -70,123 +56,42 @@ */ final class Aether { - private final Settings settings; private final List remoteRepos; private final RepositorySystem repoSystem; - private final DefaultRepositorySystemSession repoSession; + private final RepositorySystemSession repoSession; /** - * Create a new Aether instance. + * Create a new instance. * - * @param localRepoDir local repository directory - * @param remoteArtifactRepos remote artifacts repositories - * @param activeProfiles list of profile ids to activate + * @param repoSession repository system session + * @param remoteRepos remote repositories */ - @SuppressWarnings("unchecked") - Aether(File localRepoDir, List remoteArtifactRepos, List activeProfiles) { - try { - PlexusContainer container = new DefaultPlexusContainer(); - container.addComponentDescriptor(plexusDesc(Wagon.class, "http", HttpWagon.class)); - container.addComponentDescriptor(plexusDesc(Wagon.class, "https", HttpWagon.class)); - repoSystem = MavenRepositorySystemUtils - .newServiceLocator() - .setServices(WagonProvider.class, new PlexusWagonProvider(container)) - .addService(TransporterFactory.class, WagonTransporterFactory.class) - .addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class) - .getService(RepositorySystem.class); - settings = settings(); - if (activeProfiles != null) { - settings.setActiveProfiles(activeProfiles); - } - repoSession = MavenRepositorySystemUtils.newSession(); - repoSession.setTransferListener(new Slf4jMavenTransferListener()); - repoSession.setProxySelector(proxySelector()); - repoSession.setMirrorSelector(mirrorSelector()); - repoSession.setLocalRepositoryManager(repoSystem - .newLocalRepositoryManager(repoSession, new LocalRepository(localRepoDir))); - remoteRepos = remoteArtifactRepos.stream().map(this::remoteRepo).collect(toList()); - } catch (CycleDetectedInComponentGraphException | PlexusContainerException ex) { - throw new IllegalStateException(ex); - } - } - - @SuppressWarnings({"unchecked", "rawtypes", "SameParameterValue"}) - private static ComponentDescriptor plexusDesc(Class roleClass, String roleHint, Class implClass) { - ComponentDescriptor desc = new ComponentDescriptor(); - desc.setRoleClass(roleClass); - desc.setRoleHint(roleHint); - desc.setImplementationClass(implClass); - desc.setInstantiationStrategy("per-lookup"); - return desc; - } - - private static Settings settings() { - DefaultSettingsBuilder builder = new DefaultSettingsBuilderFactory().newInstance(); - DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest(); - String userSettings = System.getProperty("org.apache.maven.user-settings"); - if (userSettings == null) { - File userHome = new File(System.getProperty("user.home")).getAbsoluteFile(); - request.setUserSettingsFile(new File(userHome, "/.m2/settings.xml")); - } else { - request.setUserSettingsFile(new File(userSettings)); - } - String globalSettings = System.getProperty("org.apache.maven.global-settings"); - if (globalSettings != null) { - request.setGlobalSettingsFile(new File(globalSettings)); - } - try { - return builder.build(request).getEffectiveSettings(); - } catch (SettingsBuildingException ex) { - throw new IllegalStateException(ex); - } + Aether(RepositorySystemSession repoSession, List remoteRepos) { + this.repoSession = repoSession; + this.repoSystem = repoSystem(); + this.remoteRepos = remoteRepos; } - private MirrorSelector mirrorSelector() { - DefaultMirrorSelector selector = new DefaultMirrorSelector(); - List mirrors = settings.getMirrors(); - if (mirrors != null) { - for (Mirror mirror : mirrors) { - selector.add(mirror.getId(), mirror.getUrl(), mirror.getLayout(), false, - mirror.getMirrorOf(), mirror.getMirrorOfLayouts()); - } - } - return selector; - } - - private ProxySelector proxySelector() { - org.apache.maven.settings.Proxy proxyDef = settings.getActiveProxy(); - if (proxyDef != null) { - Authentication auth = null; - if (proxyDef.getUsername() != null) { - auth = new AuthenticationBuilder().addString(proxyDef.getUsername(), proxyDef.getPassword()).build(); - } - DefaultProxySelector selector = new DefaultProxySelector(); - Proxy proxy = new Proxy(proxyDef.getProtocol(), proxyDef.getHost(), proxyDef.getPort(), auth); - selector.add(proxy, proxyDef.getNonProxyHosts()); - return selector; - } - return null; + /** + * Create a new instance. + * + * @param repoSession repository system session + * @param artifactRepos remote artifact repositories + * @param ignored used for pseudo overload of {@code artifactRepos} + */ + Aether(RepositorySystemSession repoSession, List artifactRepos, boolean ignored) { + this.repoSession = repoSession; + this.repoSystem = repoSystem(); + this.remoteRepos = artifactRepos.stream().map(this::remoteRepo).collect(Collectors.toList()); } - private RemoteRepository remoteRepo(ArtifactRepository aRepo) { - RemoteRepository.Builder builder = new RemoteRepository.Builder(aRepo.getId(), aRepo.getLayout().getId(), - aRepo.getUrl()); - ArtifactRepositoryPolicy releases = aRepo.getReleases(); - if (releases != null) { - RepositoryPolicy releasePolicy = new RepositoryPolicy(releases.isEnabled(), releases.getUpdatePolicy(), - releases.getChecksumPolicy()); - builder.setReleasePolicy(releasePolicy); - } - ArtifactRepositoryPolicy snapshots = aRepo.getSnapshots(); - if (snapshots != null) { - RepositoryPolicy snapshotPolicy = new RepositoryPolicy(snapshots.isEnabled(), snapshots.getUpdatePolicy(), - snapshots.getChecksumPolicy()); - builder.setSnapshotPolicy(snapshotPolicy); - } - RemoteRepository repository = builder.build(); - return new RemoteRepository.Builder(repository) - .setProxy(repoSession.getProxySelector().getProxy(repository)) - .build(); + /** + * Get the repository system session. + * + * @return RepositorySystemSession + */ + RepositorySystemSession repoSession() { + return repoSession; } /** @@ -247,4 +152,52 @@ List resolveDependencies(List coords) { } return files; } + + private RemoteRepository remoteRepo(ArtifactRepository repo) { + RemoteRepository.Builder builder = new RemoteRepository.Builder(repo.getId(), repo.getLayout().getId(), + repo.getUrl()); + ArtifactRepositoryPolicy releases = repo.getReleases(); + if (releases != null) { + RepositoryPolicy releasePolicy = new RepositoryPolicy(releases.isEnabled(), releases.getUpdatePolicy(), + releases.getChecksumPolicy()); + builder.setReleasePolicy(releasePolicy); + } + ArtifactRepositoryPolicy snapshots = repo.getSnapshots(); + if (snapshots != null) { + RepositoryPolicy snapshotPolicy = new RepositoryPolicy(snapshots.isEnabled(), snapshots.getUpdatePolicy(), + snapshots.getChecksumPolicy()); + builder.setSnapshotPolicy(snapshotPolicy); + } + RemoteRepository repository = builder.build(); + return new RemoteRepository.Builder(repository) + .setProxy(repoSession.getProxySelector().getProxy(repository)) + .build(); + } + + @SuppressWarnings({"unchecked", "rawtypes", "SameParameterValue"}) + private static ComponentDescriptor plexusDesc(Class roleClass, String roleHint, Class implClass) { + ComponentDescriptor desc = new ComponentDescriptor(); + desc.setRoleClass(roleClass); + desc.setRoleHint(roleHint); + desc.setImplementationClass(implClass); + desc.setInstantiationStrategy("per-lookup"); + return desc; + } + + @SuppressWarnings("unchecked") + private static RepositorySystem repoSystem() { + try { + PlexusContainer container = new DefaultPlexusContainer(); + container.addComponentDescriptor(plexusDesc(Wagon.class, "http", HttpWagon.class)); + container.addComponentDescriptor(plexusDesc(Wagon.class, "https", HttpWagon.class)); + return MavenRepositorySystemUtils + .newServiceLocator() + .setServices(WagonProvider.class, new PlexusWagonProvider(container)) + .addService(TransporterFactory.class, WagonTransporterFactory.class) + .addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class) + .getService(RepositorySystem.class); + } catch (CycleDetectedInComponentGraphException | PlexusContainerException ex) { + throw new IllegalStateException(ex); + } + } } diff --git a/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/EngineFacade.java b/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/EngineFacade.java index b540836a3..64467ee1e 100644 --- a/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/EngineFacade.java +++ b/maven-plugins/helidon-archetype-maven-plugin/src/main/java/io/helidon/build/maven/archetype/postgenerate/EngineFacade.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023 Oracle and/or its affiliates. + * Copyright (c) 2020, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; @@ -30,12 +31,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Properties; import org.apache.maven.archetype.ArchetypeGenerationRequest; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.versioning.ComparableVersion; import org.apache.maven.project.ProjectBuildingRequest; +import org.eclipse.aether.RepositorySystemSession; import static java.util.Collections.emptyMap; @@ -52,44 +55,6 @@ private EngineFacade() { private static final String MAVEN_CORE_POM_PROPERTIES = "META-INF/maven/org.apache.maven/maven-core/pom.properties"; - private static String getMavenVersion() { - // see org.apache.maven.rtinfo.internal.DefaultRuntimeInformation - Properties props = new Properties(); - try { - props.load(EngineFacade.class.getClassLoader().getResourceAsStream(MAVEN_CORE_POM_PROPERTIES)); - } catch (IOException ex) { - throw new UncheckedIOException(ex); - } - String version = props.getProperty("version", "").trim(); - return version.startsWith("${") ? "" : version; - } - - private static void checkMavenVersion() { - String mavenVersion = getMavenVersion(); - ComparableVersion minMavenVersion = new ComparableVersion("3.2.5"); - if (!mavenVersion.isEmpty() && new ComparableVersion(mavenVersion).compareTo(minMavenVersion) < 0) { - throw new IllegalStateException("Requires Maven >= 3.2.5"); - } - } - - private static void checkJavaVersion() { - try { - // using reflection to run on Java 8 - Method versionMethod = Runtime.class.getMethod("version"); - Runtime runtime = Runtime.getRuntime(); - Object version = versionMethod.invoke(runtime); - Method featureMethod = version.getClass().getMethod("feature"); - Object feature = featureMethod.invoke(version); - if (feature instanceof Integer && (int) feature < 11) { - throw new IllegalStateException(); - } - } catch (NoSuchMethodException | IllegalStateException ex) { - throw new IllegalStateException("Requires Java >= 11"); - } catch (Throwable ex) { - throw new IllegalStateException("Unable to verify Java version", ex); - } - } - /** * Generate a project. * @@ -101,10 +66,22 @@ public static void generate(ArchetypeGenerationRequest request, List dep checkMavenVersion(); checkJavaVersion(); - ProjectBuildingRequest mavenRequest = request.getProjectBuildingRequest(); - File localRepo = mavenRequest.getRepositorySession().getLocalRepository().getBasedir(); - List remoteRepos = mavenRequest.getRemoteRepositories(); - Aether aether = new Aether(localRepo, remoteRepos, mavenRequest.getActiveProfileIds()); + Aether aether = EngineFacade.invoke(request, "getRepositorySession") + // archetype-common >= 3.3.0 + .map(repoSession -> new Aether(repoSession, request.getRemoteArtifactRepositories())) + // archetype-common < 3.3.0 + .orElseGet(() -> { + ProjectBuildingRequest pbr = EngineFacade.invoke(request, "getProjectBuildingRequest") + .orElseThrow(() -> new IllegalStateException("Unable to get project building request")); + RepositorySystemSession repoSession = EngineFacade.invoke(pbr, + "getRepositorySession") + .orElseThrow(() -> new IllegalStateException("Unable to get repository system session")); + List artifactRepos = EngineFacade.>invoke(request, + "getRemoteArtifactRepositories") + .orElseThrow(() -> new IllegalStateException("Unable to get artifact repositories")); + return new Aether(repoSession, artifactRepos, true); + }); + File localRepo = aether.repoSession().getLocalRepository().getBasedir(); // enable mvn:// URL support System.setProperty(MAVEN_URL_REPO_PROPERTY, localRepo.getAbsolutePath()); @@ -125,8 +102,6 @@ public static void generate(ArchetypeGenerationRequest request, List dep Properties archetypeProps = request.getProperties(); Map props = new HashMap<>(); archetypeProps.stringPropertyNames().forEach(k -> props.put(k, archetypeProps.getProperty(k))); - Properties userProps = request.getProjectBuildingRequest().getUserProperties(); - userProps.stringPropertyNames().forEach(k -> props.putIfAbsent(k, userProps.getProperty(k))); // resolve the archetype JAR from the local repository File archetypeFile = aether.resolveArtifact(request.getArchetypeGroupId(), request.getArchetypeArtifactId(), @@ -142,4 +117,54 @@ public static void generate(ArchetypeGenerationRequest request, List dep throw new IllegalStateException(ioe); } } + + private static String getMavenVersion() { + // see org.apache.maven.rtinfo.internal.DefaultRuntimeInformation + Properties props = new Properties(); + try { + props.load(EngineFacade.class.getClassLoader().getResourceAsStream(MAVEN_CORE_POM_PROPERTIES)); + } catch (IOException ex) { + throw new UncheckedIOException(ex); + } + String version = props.getProperty("version", "").trim(); + return version.startsWith("${") ? "" : version; + } + + private static void checkMavenVersion() { + String mavenVersion = getMavenVersion(); + ComparableVersion minMavenVersion = new ComparableVersion("3.2.5"); + if (!mavenVersion.isEmpty() && new ComparableVersion(mavenVersion).compareTo(minMavenVersion) < 0) { + throw new IllegalStateException("Requires Maven >= 3.2.5"); + } + } + + private static void checkJavaVersion() { + try { + // using reflection to run on Java 8 + Method versionMethod = Runtime.class.getMethod("version"); + Runtime runtime = Runtime.getRuntime(); + Object version = versionMethod.invoke(runtime); + Method featureMethod = version.getClass().getMethod("feature"); + Object feature = featureMethod.invoke(version); + if (feature instanceof Integer && (int) feature < 11) { + throw new IllegalStateException(); + } + } catch (NoSuchMethodException | IllegalStateException ex) { + throw new IllegalStateException("Requires Java >= 11"); + } catch (Throwable ex) { + throw new IllegalStateException("Unable to verify Java version", ex); + } + } + + @SuppressWarnings("unchecked") + private static Optional invoke(Object object, String methodName) { + try { + Method method = object.getClass().getMethod(methodName); + return Optional.ofNullable((T) method.invoke(object)); + } catch (NoSuchMethodException ex) { + return Optional.empty(); + } catch (IllegalAccessException | InvocationTargetException ex) { + throw new RuntimeException(ex); + } + } } diff --git a/pom.xml b/pom.xml index 358b100f9..3440c9a78 100644 --- a/pom.xml +++ b/pom.xml @@ -122,18 +122,17 @@ Changing these version requires approval for a new third party dependency! --> 3.10 - 3.2.0 + 3.3.0 2.5.11 9.5 10.12.6 2.2 1.11.1 2.3.23 - 2.4.16 2.8.9 32.0.1-jre 1.3 - 3.0.1 + 3.3.0 2.11.0 3.1.2 1.18 @@ -154,8 +153,8 @@ 3.4.0 3.3.1 3.3.0 - 1.4.1 - 3.6.2 + 1.6.3 + 3.8.8 5.10.0 0.9.6 3.10.0 @@ -196,7 +195,7 @@ 0.7.9 3.0.2 3.6.2 - 3.2.2 + 3.8.0 1.16 2.1.0 3.10.2 @@ -467,13 +466,6 @@ install - - - org.codehaus.groovy - groovy - ${version.lib.groovy} - - org.codehaus.mojo @@ -517,7 +509,7 @@ - ${top.parent.basedir}/etc/dependency-check-suppression.xml + ${maven.multiModuleProjectDirectory}/etc/dependency-check-suppression.xml @@ -932,6 +924,11 @@ maven-resolver-connector-basic ${version.lib.maven-resolver} + + org.apache.maven.resolver + maven-resolver-util + ${version.lib.maven-resolver} + org.apache.maven maven-embedder