From b4b2ffe41bd48313cb27bfe51d0254ab713fd4c4 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 6 Aug 2024 16:17:20 +0200 Subject: [PATCH] Allow not detecting the extension We actually have some level of support for building an extension with Gradle so we need to make it work. However, at the moment, it's close to impossible to determine the extension we are in in a Gradle extension. So we allow not detecting the extension and disable the config doc generation in this case. --- .../ExtensionAnnotationProcessor.java | 34 +++++++++++-------- .../documentation/config/model/Extension.java | 6 +++- .../processor/util/ExtensionUtil.java | 21 ++++++++---- .../annotation/processor/util/FilerUtil.java | 14 +++++--- .../annotation/processor/util/Utils.java | 2 +- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/core/processor/src/main/java/io/quarkus/annotation/processor/ExtensionAnnotationProcessor.java b/core/processor/src/main/java/io/quarkus/annotation/processor/ExtensionAnnotationProcessor.java index fa3758b7c61a7..c224ba30c764b 100644 --- a/core/processor/src/main/java/io/quarkus/annotation/processor/ExtensionAnnotationProcessor.java +++ b/core/processor/src/main/java/io/quarkus/annotation/processor/ExtensionAnnotationProcessor.java @@ -16,6 +16,7 @@ import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic; +import javax.tools.Diagnostic.Kind; import org.jboss.jdeparser.JDeparser; @@ -38,19 +39,6 @@ public class ExtensionAnnotationProcessor extends AbstractProcessor { public synchronized void init(ProcessingEnvironment processingEnv) { super.init(processingEnv); - List extensionProcessors = new ArrayList<>(); - extensionProcessors.add(new ExtensionBuildProcessor()); - - boolean skipDocs = Boolean.getBoolean("skipDocs") || Boolean.getBoolean("quickly"); - boolean generateDoc = !skipDocs && !"false".equals(processingEnv.getOptions().get(Options.GENERATE_DOC)); - - // for now, we generate the old config doc by default but we will change this behavior soon - if (generateDoc) { - extensionProcessors.add(new ConfigDocExtensionProcessor()); - } - - this.extensionProcessors = Collections.unmodifiableList(extensionProcessors); - utils = new Utils(processingEnv); boolean useConfigMapping = !Boolean @@ -65,7 +53,25 @@ public synchronized void init(ProcessingEnvironment processingEnv) { + " config implementation is deprecated. Please migrate to use @ConfigMapping: https://quarkus.io/guides/writing-extensions#configuration"); } - for (ExtensionProcessor extensionProcessor : extensionProcessors) { + List extensionProcessors = new ArrayList<>(); + extensionProcessors.add(new ExtensionBuildProcessor()); + + boolean skipDocs = Boolean.getBoolean("skipDocs") || Boolean.getBoolean("quickly"); + boolean generateDoc = !skipDocs && !"false".equals(processingEnv.getOptions().get(Options.GENERATE_DOC)); + + // for now, we generate the old config doc by default but we will change this behavior soon + if (generateDoc) { + if (extension.detected()) { + extensionProcessors.add(new ConfigDocExtensionProcessor()); + } else { + processingEnv.getMessager().printMessage(Kind.WARNING, + "We could not detect the groupId and artifactId of this module (maybe you are using Gradle to build your extension?). The generation of the configuration documentation has been disabled."); + } + } + + this.extensionProcessors = Collections.unmodifiableList(extensionProcessors); + + for (ExtensionProcessor extensionProcessor : this.extensionProcessors) { extensionProcessor.init(config, utils); } } diff --git a/core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/model/Extension.java b/core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/model/Extension.java index 2a13590b6d686..a6c2941fea672 100644 --- a/core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/model/Extension.java +++ b/core/processor/src/main/java/io/quarkus/annotation/processor/documentation/config/model/Extension.java @@ -5,7 +5,11 @@ import com.fasterxml.jackson.annotation.JsonIgnore; public record Extension(String groupId, String artifactId, String name, - NameSource nameSource) implements Comparable { + NameSource nameSource, boolean detected) implements Comparable { + + public static Extension createNotDetected() { + return new Extension("not.detected", "not.detected", "Not detected", NameSource.NONE, false); + } @Override public final String toString() { diff --git a/core/processor/src/main/java/io/quarkus/annotation/processor/util/ExtensionUtil.java b/core/processor/src/main/java/io/quarkus/annotation/processor/util/ExtensionUtil.java index 7d834e1551186..6634c70469e59 100644 --- a/core/processor/src/main/java/io/quarkus/annotation/processor/util/ExtensionUtil.java +++ b/core/processor/src/main/java/io/quarkus/annotation/processor/util/ExtensionUtil.java @@ -4,6 +4,8 @@ import java.util.Map; import java.util.Optional; +import javax.annotation.processing.ProcessingEnvironment; +import javax.tools.Diagnostic.Kind; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -26,9 +28,11 @@ public final class ExtensionUtil { private static final String NAME_COMMON_SUFFIX = " - Common"; private static final String NAME_INTERNAL_SUFFIX = " - Internal"; + private final ProcessingEnvironment processingEnv; private final FilerUtil filerUtil; - ExtensionUtil(FilerUtil filerUtil) { + ExtensionUtil(ProcessingEnvironment processingEnv, FilerUtil filerUtil) { + this.processingEnv = processingEnv; this.filerUtil = filerUtil; } @@ -37,7 +41,11 @@ public final class ExtensionUtil { * One option would be to pass it through the annotation processor but it's not exactly ideal. */ public Extension getExtension() { - Path pom = filerUtil.getPomPath(); + Optional pom = filerUtil.getPomPath(); + + if (pom.isEmpty()) { + return Extension.createNotDetected(); + } Document doc; @@ -45,13 +53,13 @@ public Extension getExtension() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); DocumentBuilder db = dbf.newDocumentBuilder(); - doc = db.parse(pom.toFile()); + doc = db.parse(pom.get().toFile()); doc.getDocumentElement().normalize(); } catch (Exception e) { throw new IllegalStateException("Unable to parse pom file: " + pom, e); } - return getExtensionFromPom(pom, doc); + return getExtensionFromPom(pom.get(), doc); } private Extension getExtensionFromPom(Path pom, Document doc) { @@ -99,7 +107,8 @@ private Extension getExtensionFromPom(Path pom, Document doc) { } if (groupId == null || groupId.isBlank() || artifactId == null || artifactId.isBlank()) { - throw new IllegalStateException("Unable to determine artifact coordinates from: " + pom); + processingEnv.getMessager().printMessage(Kind.WARNING, "Unable to determine artifact coordinates from: " + pom); + return Extension.createNotDetected(); } boolean commonOrInternal = false; @@ -142,7 +151,7 @@ private Extension getExtensionFromPom(Path pom, Document doc) { } } - return new Extension(groupId, artifactId, name, nameSource); + return new Extension(groupId, artifactId, name, nameSource, true); } private Optional getExtensionNameFromExtensionMetadata() { diff --git a/core/processor/src/main/java/io/quarkus/annotation/processor/util/FilerUtil.java b/core/processor/src/main/java/io/quarkus/annotation/processor/util/FilerUtil.java index 176dded94bf89..158f9d1621703 100644 --- a/core/processor/src/main/java/io/quarkus/annotation/processor/util/FilerUtil.java +++ b/core/processor/src/main/java/io/quarkus/annotation/processor/util/FilerUtil.java @@ -133,12 +133,18 @@ public Path getTargetPath() { } } - public Path getPomPath() { + public Optional getPomPath() { try { - return Paths.get(processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "dummy").toUri()) - .getParent().getParent().getParent().resolve("pom.xml").toAbsolutePath(); + Path pomPath = Paths.get(processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "dummy").toUri()) + .getParent().getParent().getParent().resolve("pom.xml"); + + if (!Files.isReadable(pomPath)) { + return Optional.empty(); + } + + return Optional.of(pomPath.toAbsolutePath()); } catch (IOException e) { - throw new IllegalStateException("Unable to determine path to pom.xml"); + return Optional.empty(); } } diff --git a/core/processor/src/main/java/io/quarkus/annotation/processor/util/Utils.java b/core/processor/src/main/java/io/quarkus/annotation/processor/util/Utils.java index 7888817d3f14b..66b3c2b161b07 100644 --- a/core/processor/src/main/java/io/quarkus/annotation/processor/util/Utils.java +++ b/core/processor/src/main/java/io/quarkus/annotation/processor/util/Utils.java @@ -15,7 +15,7 @@ public Utils(ProcessingEnvironment processingEnv) { this.elementUtil = new ElementUtil(processingEnv); this.accessorGenerator = new AccessorGenerator(processingEnv, elementUtil); this.filerUtil = new FilerUtil(processingEnv); - this.extensionUtil = new ExtensionUtil(filerUtil); + this.extensionUtil = new ExtensionUtil(processingEnv, filerUtil); } public ElementUtil element() {