From 041e1f0377f05dd32d92a729c0327164b696b0d0 Mon Sep 17 00:00:00 2001 From: JF Denise Date: Fri, 13 Jan 2023 18:29:43 +0100 Subject: [PATCH] Fix for WFMP-178, Upgrade to wildfly-channel 1.0.0.Beta4 --- .../AbstractProvisionServerMojo.java | 6 +- .../provision/ChannelConfiguration.java | 111 ++++++++++++++++++ ...ChannelMavenArtifactRepositoryManager.java | 48 +++++--- pom.xml | 10 +- 4 files changed, 155 insertions(+), 20 deletions(-) create mode 100644 plugin/src/main/java/org/wildfly/plugin/provision/ChannelConfiguration.java diff --git a/plugin/src/main/java/org/wildfly/plugin/provision/AbstractProvisionServerMojo.java b/plugin/src/main/java/org/wildfly/plugin/provision/AbstractProvisionServerMojo.java index 3d2040cd..754bc72c 100644 --- a/plugin/src/main/java/org/wildfly/plugin/provision/AbstractProvisionServerMojo.java +++ b/plugin/src/main/java/org/wildfly/plugin/provision/AbstractProvisionServerMojo.java @@ -48,7 +48,6 @@ import org.jboss.galleon.util.IoUtils; import org.jboss.galleon.xml.ProvisioningXmlWriter; import org.wildfly.channel.UnresolvedMavenArtifactException; -import org.wildfly.channel.maven.ChannelCoordinate; import org.wildfly.plugin.common.PropertyNames; import org.wildfly.plugin.common.Utils; import org.wildfly.plugin.core.GalleonUtils; @@ -178,7 +177,7 @@ abstract class AbstractProvisionServerMojo extends AbstractMojo { String layersConfigurationFileName; @Parameter(alias = "channels", required = false) - List channels; + List channels; private Path wildflyDir; @@ -203,8 +202,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { : new MavenArtifactRepositoryManager(repoSystem, repoSession, repositories); } else { try { - artifactResolver = offlineProvisioning ? new ChannelMavenArtifactRepositoryManager(channels, repoSystem, repoSession) - : new ChannelMavenArtifactRepositoryManager(channels, repoSystem, repoSession, repositories); + artifactResolver = new ChannelMavenArtifactRepositoryManager(channels, repoSystem, repoSession, repositories, getLog(), offlineProvisioning); } catch (MalformedURLException | UnresolvedMavenArtifactException ex) { throw new MojoExecutionException(ex.getLocalizedMessage(), ex); } diff --git a/plugin/src/main/java/org/wildfly/plugin/provision/ChannelConfiguration.java b/plugin/src/main/java/org/wildfly/plugin/provision/ChannelConfiguration.java new file mode 100644 index 00000000..0205822d --- /dev/null +++ b/plugin/src/main/java/org/wildfly/plugin/provision/ChannelConfiguration.java @@ -0,0 +1,111 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wildfly.plugin.provision; + +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; +import org.eclipse.aether.repository.RemoteRepository; +import org.wildfly.channel.Channel; +import org.wildfly.channel.ChannelManifestCoordinate; +import org.wildfly.channel.ChannelMapper; +import org.wildfly.channel.InvalidChannelMetadataException; +import org.wildfly.channel.Repository; + +/** + * A channel configuration. One of url or manifest coordinates. + * + * @author jdenise + */ +public class ChannelConfiguration { + + private URL url; + private ChannelManifestCoordinate manifestCoordinate; + + /** + * @return the url + */ + public URL getUrl() { + return url; + } + + /** + * @return the manifestCoordinate + */ + public ChannelManifestCoordinate getManifestCoordinate() { + return manifestCoordinate; + } + + private void validate() throws MojoExecutionException { + if (getUrl() != null) { + if (getManifestCoordinate() != null) { + throw new MojoExecutionException("Invalid Channel. A manifest-coordinate is specified although an URL is provided."); + } + } else { + if (getManifestCoordinate() == null) { + throw new MojoExecutionException("Invalid Channel. No manifest-coordinate or URL specified."); + } else { + ChannelManifestCoordinate coordinates = getManifestCoordinate(); + if (coordinates.getUrl() == null) { + if (coordinates.getGroupId() == null) { + throw new MojoExecutionException("Invalid Channel. Manifest groupId is null."); + } + if (coordinates.getArtifactId() == null) { + throw new MojoExecutionException("Invalid Channel. Manifest artifactId is null."); + } + } else { + if (coordinates.getGroupId() != null) { + throw new MojoExecutionException("Invalid Channel. Manifest groupId is set although an URL is provided."); + } + if (coordinates.getArtifactId() != null) { + throw new MojoExecutionException("Invalid Channel. Manifest artifactId is set although an URL is provided."); + } + } + } + } + } + + public Channel toChannel(Set remoteRepositories, List repositories, Log log) throws MojoExecutionException { + validate(); + Channel channel; + if (getUrl() == null) { + List repos = new ArrayList<>(); + for (RemoteRepository r : repositories) { + repos.add(new Repository(r.getId(), r.getUrl())); + } + channel = new Channel(null, null, null, repos, getManifestCoordinate(), null, null); + } else { + try { + channel = ChannelMapper.from(getUrl()); + for (Repository r : channel.getRepositories()) { + if (!remoteRepositories.contains(r.getId())) { + log.warn("Repository id " + r.getId() + " defined in channel " + getUrl() + + " is not found in the configured Maven " + + "repositories. Will create a new repository."); + } + } + } catch (InvalidChannelMetadataException ex) { + throw new MojoExecutionException("Invalid Channel: " + + (ex.getValidationMessages() == null ? "" : ex.getValidationMessages()), ex); + } + } + return channel; + } +} diff --git a/plugin/src/main/java/org/wildfly/plugin/provision/ChannelMavenArtifactRepositoryManager.java b/plugin/src/main/java/org/wildfly/plugin/provision/ChannelMavenArtifactRepositoryManager.java index 09ffadd6..3fe03cf8 100644 --- a/plugin/src/main/java/org/wildfly/plugin/provision/ChannelMavenArtifactRepositoryManager.java +++ b/plugin/src/main/java/org/wildfly/plugin/provision/ChannelMavenArtifactRepositoryManager.java @@ -19,8 +19,14 @@ import java.io.IOException; import java.net.MalformedURLException; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.function.Function; import java.util.regex.Pattern; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.eclipse.aether.DefaultRepositorySystemSession; @@ -33,30 +39,44 @@ import org.wildfly.channel.Channel; import org.wildfly.channel.ChannelManifest; import org.wildfly.channel.ChannelSession; +import org.wildfly.channel.Repository; import org.wildfly.channel.UnresolvedMavenArtifactException; -import org.wildfly.channel.maven.ChannelCoordinate; import org.wildfly.channel.maven.VersionResolverFactory; +import static org.wildfly.channel.maven.VersionResolverFactory.DEFAULT_REPOSITORY_MAPPER; import org.wildfly.channel.spi.ChannelResolvable; import org.wildfly.prospero.metadata.ProsperoMetadataUtils; public class ChannelMavenArtifactRepositoryManager implements MavenRepoManager, ChannelResolvable { private final ChannelSession channelSession; - private final List channels; - public ChannelMavenArtifactRepositoryManager(List channelCoords, - RepositorySystem system, RepositorySystemSession contextSession) throws MalformedURLException, UnresolvedMavenArtifactException { - this(channelCoords, system, contextSession, null); - } - - public ChannelMavenArtifactRepositoryManager(List channelCoords, - RepositorySystem system, - RepositorySystemSession contextSession, - List repositories) throws MalformedURLException, UnresolvedMavenArtifactException { + private final List channels = new ArrayList<>(); + + public ChannelMavenArtifactRepositoryManager(List channels, + RepositorySystem system, + RepositorySystemSession contextSession, + List repositories, Log log, boolean offline) throws MalformedURLException, UnresolvedMavenArtifactException, MojoExecutionException { + if (channels.isEmpty()) { + throw new MojoExecutionException("No channel specified."); + } DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession(); session.setLocalRepositoryManager(contextSession.getLocalRepositoryManager()); - VersionResolverFactory factory = new VersionResolverFactory(system, session); - channels = factory.resolveChannels(channelCoords, repositories); - channelSession = new ChannelSession(channels, factory); + session.setOffline(offline); + Map mapping = new HashMap<>(); + for (RemoteRepository r : repositories) { + mapping.put(r.getId(), r); + } + for (ChannelConfiguration channelConfiguration : channels) { + this.channels.add(channelConfiguration.toChannel(mapping.keySet(), repositories, log)); + } + Function mapper = r -> { + RemoteRepository rep = mapping.get(r.getId()); + if (rep == null) { + rep = DEFAULT_REPOSITORY_MAPPER.apply(r); + } + return rep; + }; + VersionResolverFactory factory = new VersionResolverFactory(system, session, mapper); + channelSession = new ChannelSession(this.channels, factory); } @Override diff --git a/pom.xml b/pom.xml index 7c127620..5fd75b4e 100644 --- a/pom.xml +++ b/pom.xml @@ -96,8 +96,8 @@ 19.0.1.Final 26.1.2.Final - 1.0.0.Beta4-SNAPSHOT - 1.0.0.Beta5-SNAPSHOT + 1.0.0.Beta4 + 1.0.0.Beta5 1 3.3.9 @@ -358,6 +358,12 @@ org.wildfly.prospero prospero-metadata ${version.org.wildfly.prospero} + + + org.wildfly.channel + channel-core + +