Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support extension registry when adding or listing extensions in plugins #10062

Merged
merged 3 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package io.quarkus.gradle.tasks;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

import java.net.URL;
import java.util.List;
import java.util.Set;

import org.gradle.api.GradleException;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import io.quarkus.devtools.commands.AddExtensions;
import io.quarkus.registry.DefaultExtensionRegistry;

public class QuarkusAddExtension extends QuarkusPlatformTask {

Expand All @@ -20,6 +24,7 @@ public QuarkusAddExtension() {
}

private List<String> extensionsToAdd;
private List<String> registries;

@Option(option = "extensions", description = "Configures the extensions to be added.")
public void setExtensionsToAdd(List<String> extensionsToAdd) {
Expand All @@ -31,6 +36,17 @@ public List<String> getExtensionsToAdd() {
return extensionsToAdd;
}

@Optional
@Input
public List<String> getRegistries() {
return registries;
}

@Option(description = "The extension registry URLs to be used", option = "registry")
public void setRegistries(List<String> registry) {
this.registries = registry;
}

@TaskAction
public void addExtension() {
Set<String> extensionsSet = getExtensionsToAdd()
Expand All @@ -40,9 +56,15 @@ public void addExtension() {
.collect(toSet());

try {
new AddExtensions(getQuarkusProject())
.extensions(extensionsSet)
.execute();
AddExtensions addExtensions = new AddExtensions(getQuarkusProject())
.extensions(extensionsSet);
if (registries != null && !registries.isEmpty()) {
List<URL> urls = registries.stream()
.map(QuarkusAddExtension::toURL)
.collect(toList());
addExtensions.extensionRegistry(DefaultExtensionRegistry.fromURLs(urls));
}
addExtensions.execute();
} catch (Exception e) {
throw new GradleException("Failed to add extensions " + getExtensionsToAdd(), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package io.quarkus.gradle.tasks;

import static java.util.stream.Collectors.toList;

import java.net.URL;
import java.util.List;

import org.gradle.api.GradleException;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

import io.quarkus.devtools.commands.ListExtensions;
import io.quarkus.registry.DefaultExtensionRegistry;

public class QuarkusListExtensions extends QuarkusPlatformTask {

Expand All @@ -16,6 +22,8 @@ public class QuarkusListExtensions extends QuarkusPlatformTask {

private String searchPattern;

private List<String> registries;

@Input
public boolean isAll() {
return all;
Expand Down Expand Up @@ -48,18 +56,35 @@ public void setSearchPattern(String searchPattern) {
this.searchPattern = searchPattern;
}

@Optional
@Input
public List<String> getRegistries() {
return registries;
}

@Option(description = "The extension registry URLs to be used", option = "registry")
public void setRegistries(List<String> registry) {
this.registries = registry;
}

public QuarkusListExtensions() {
super("Lists the available quarkus extensions");
}

@TaskAction
public void listExtensions() {
try {
new ListExtensions(getQuarkusProject())
ListExtensions listExtensions = new ListExtensions(getQuarkusProject())
.all(isAll())
.format(getFormat())
.search(getSearchPattern())
.execute();
.search(getSearchPattern());
if (registries != null && !registries.isEmpty()) {
List<URL> urls = registries.stream()
.map(QuarkusListExtensions::toURL)
.collect(toList());
listExtensions.extensionRegistry(DefaultExtensionRegistry.fromURLs(urls));
}
listExtensions.execute();
} catch (Exception e) {
throw new GradleException("Unable to list extensions", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.gradle.tasks;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -78,4 +80,13 @@ protected BuildFile getGradleBuildFile() {
protected QuarkusProject getQuarkusProject() {
return QuarkusProject.of(getProject().getProjectDir().toPath(), platformDescriptor(), getGradleBuildFile());
}

protected static URL toURL(String url) {
try {
return new URL(url);
} catch (MalformedURLException e) {
throw new GradleException("Malformed URL:" + url, e);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.quarkus.maven;

import static java.util.stream.Collectors.toSet;

import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -14,6 +17,7 @@
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.platform.tools.MessageWriter;
import io.quarkus.registry.DefaultExtensionRegistry;

/**
* Allow adding an extension to an existing pom.xml file.
Expand All @@ -36,6 +40,12 @@ public class AddExtensionMojo extends QuarkusProjectMojoBase {
@Parameter(property = "extension")
String extension;

/**
* The URL where the registry is.
*/
@Parameter(property = "registry", alias = "quarkus.extension.registry")
List<URL> registries;

@Override
protected void validateParameters() throws MojoExecutionException {
if ((StringUtils.isBlank(extension) && (extensions == null || extensions.isEmpty())) // None are set
Expand All @@ -53,13 +63,16 @@ public void doExecute(final QuarkusProject quarkusProject, final MessageWriter l
} else {
// Parse the "extension" just in case it contains several comma-separated values
// https://github.com/quarkusio/quarkus/issues/2393
ext.addAll(Arrays.stream(extension.split(",")).map(s -> s.trim()).collect(Collectors.toSet()));
ext.addAll(Arrays.stream(extension.split(",")).map(String::trim).collect(toSet()));
}

try {
final QuarkusCommandOutcome outcome = new AddExtensions(quarkusProject)
.extensions(ext.stream().map(String::trim).collect(Collectors.toSet()))
.execute();
AddExtensions addExtensions = new AddExtensions(quarkusProject)
.extensions(ext.stream().map(String::trim).collect(toSet()));
if (registries != null && !registries.isEmpty()) {
addExtensions.extensionRegistry(DefaultExtensionRegistry.fromURLs(registries));
}
final QuarkusCommandOutcome outcome = addExtensions.execute();
if (!outcome.isSuccess()) {
throw new MojoExecutionException("Unable to add extensions");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.quarkus.maven;

import java.net.URL;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import io.quarkus.devtools.commands.ListExtensions;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.platform.tools.MessageWriter;
import io.quarkus.registry.DefaultExtensionRegistry;

/**
* List the available extensions.
Expand Down Expand Up @@ -36,14 +40,23 @@ public class ListExtensionsMojo extends QuarkusProjectMojoBase {
@Parameter(property = "searchPattern", alias = "quarkus.extension.searchPattern")
protected String searchPattern;

/**
* The extension registry URLs
*/
@Parameter(property = "registry", alias = "quarkus.extension.registry")
List<URL> registries;

@Override
public void doExecute(final QuarkusProject quarkusProject, final MessageWriter log) throws MojoExecutionException {
try {
new ListExtensions(quarkusProject)
ListExtensions listExtensions = new ListExtensions(quarkusProject)
.all(all)
.format(format)
.search(searchPattern)
.execute();
.search(searchPattern);
if (registries != null && !registries.isEmpty()) {
listExtensions.extensionRegistry(DefaultExtensionRegistry.fromURLs(registries));
}
listExtensions.execute();
} catch (Exception e) {
throw new MojoExecutionException("Failed to list extensions", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
Expand All @@ -18,9 +19,12 @@
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactResult;

import io.quarkus.bootstrap.resolver.BootstrapAppModelResolver;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.bootstrap.resolver.maven.workspace.ModelUtils;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.platform.descriptor.CombinedQuarkusPlatformDescriptor;
Expand Down Expand Up @@ -75,7 +79,7 @@ private QuarkusPlatformDescriptor resolvePlatformDescriptor(final MessageWriter
.setRemoteRepositories(repos).build();
if (project.getFile() != null) {
final List<Artifact> descrArtifactList = new ArrayList<>(2);
for (Dependency dep : getManagedDependencies()) {
for (Dependency dep : getManagedDependencies(mvn)) {
if ((dep.getScope() == null || !dep.getScope().equals("import"))
&& (dep.getType() == null || !dep.getType().equals("pom"))) {
continue;
Expand Down Expand Up @@ -164,9 +168,36 @@ private String resolveValue(String expr) throws IOException {
return expr;
}

private List<Dependency> getManagedDependencies() throws IOException {
final DependencyManagement managed = project.getModel().getDependencyManagement();
return managed != null ? managed.getDependencies()
: Collections.emptyList();
private List<Dependency> getManagedDependencies(MavenArtifactResolver resolver) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @ia3andy this is going to help collect platform BOMs from the parents.

List<Dependency> managedDependencies = new ArrayList<>();
Model model = project.getOriginalModel();
DependencyManagement managed = model.getDependencyManagement();
if (managed != null) {
managedDependencies.addAll(managed.getDependencies());
}
Parent parent;
while ((parent = model.getParent()) != null) {
try {
ArtifactResult result = resolver.resolve(new DefaultArtifact(
parent.getGroupId(),
parent.getArtifactId(),
"pom",
ModelUtils.resolveVersion(parent.getVersion(), model)));
model = ModelUtils.readModel(result.getArtifact().getFile().toPath());
managed = model.getDependencyManagement();
if (managed != null) {
// Alexey Loubyansky: In Maven whatever is imported first has a priority
// So to match the maven way, we should be reading the root parent first
managedDependencies.addAll(0, managed.getDependencies());
}
} catch (BootstrapMavenException e) {
// ignore
if (getLog().isDebugEnabled()) {
getLog().debug("Error while resolving descriptor", e);
}
break;
}
}
return managedDependencies;
}
}
6 changes: 6 additions & 0 deletions independent-projects/tools/devtools-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
<groupId>jakarta.websocket</groupId>
<artifactId>jakarta.websocket-api</artifactId>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>2.8.8</version>
<scope>provided</scope>
</dependency>
aloubyansky marked this conversation as resolved.
Show resolved Hide resolved
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.quarkus.devtools.project.extensions.ExtensionManager;
import io.quarkus.platform.tools.ToolsConstants;
import io.quarkus.platform.tools.ToolsUtils;
import io.quarkus.registry.ExtensionRegistry;
import java.util.Set;

/**
Expand All @@ -21,6 +22,7 @@ public class AddExtensions {
public static final String EXTENSIONS = ToolsUtils.dotJoin(ToolsConstants.QUARKUS, NAME, "extensions");
public static final String OUTCOME_UPDATED = ToolsUtils.dotJoin(ToolsConstants.QUARKUS, NAME, "outcome", "updated");
public static final String EXTENSION_MANAGER = ToolsUtils.dotJoin(ToolsConstants.QUARKUS, NAME, "extension-manager");
public static final String EXTENSION_REGISTRY = ToolsUtils.dotJoin(ToolsConstants.QUARKUS, NAME, "extension-registry");

private final QuarkusCommandInvocation invocation;
private final AddExtensionsCommandHandler handler = new AddExtensionsCommandHandler();
Expand All @@ -34,6 +36,11 @@ public AddExtensions extensionManager(ExtensionManager extensionManager) {
return this;
}

public AddExtensions extensionRegistry(ExtensionRegistry extensionRegistry) {
invocation.setValue(EXTENSION_REGISTRY, requireNonNull(extensionRegistry, "extensionRegistry is required"));
return this;
}

public AddExtensions extensions(Set<String> extensions) {
invocation.setValue(EXTENSIONS, extensions);
return this;
Expand Down
Loading