From 3260b9b6177d3eb560548ab556ec77bec8484ae1 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Wed, 16 Jun 2021 11:03:58 +0200 Subject: [PATCH] Dev UI - handle file protocol for DevConsoleRuntimeTemplateInfoBuildItem - resolves #17915 - follows up on #17007 --- .../deployment/util/ArtifactInfoUtil.java | 47 +++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/util/ArtifactInfoUtil.java b/core/deployment/src/main/java/io/quarkus/deployment/util/ArtifactInfoUtil.java index 03c7d237923c0..84f57bfed31bc 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/util/ArtifactInfoUtil.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/util/ArtifactInfoUtil.java @@ -42,9 +42,9 @@ public static Map.Entry groupIdAndArtifactId(Class clazz) { public static Map.Entry groupIdAndArtifactId(Class clazz, CurateOutcomeBuildItem curateOutcomeBuildItem) { try { - URL jarLocation = clazz.getProtectionDomain().getCodeSource().getLocation(); - if (jarLocation.toString().endsWith(".jar")) { - try (FileSystem fs = FileSystems.newFileSystem(Paths.get(jarLocation.toURI()), + URL codeLocation = clazz.getProtectionDomain().getCodeSource().getLocation(); + if (codeLocation.toString().endsWith(".jar")) { + try (FileSystem fs = FileSystems.newFileSystem(Paths.get(codeLocation.toURI()), Thread.currentThread().getContextClassLoader())) { Entry ret = groupIdAndArtifactId(fs); if (ret == null) { @@ -55,7 +55,7 @@ public static Map.Entry groupIdAndArtifactId(Class clazz, } } else if (curateOutcomeBuildItem != null) { // this is needed only for QuarkusDevModeTest inside Quarkus where the class is read from the corresponding directory - Path path = Paths.get(jarLocation.toURI()); + Path path = Paths.get(codeLocation.toURI()); for (AppDependency i : curateOutcomeBuildItem.getEffectiveModel().getFullDeploymentDeps()) { for (Path p : i.getArtifact().getPaths()) { if (path.equals(p)) { @@ -69,6 +69,30 @@ public static Map.Entry groupIdAndArtifactId(Class clazz, } } return new AbstractMap.SimpleEntry<>("unspecified", "unspecified"); + } else if ("file".equals(codeLocation.getProtocol())) { + // E.g. /quarkus/extensions/arc/deployment/target/classes/io/quarkus/arc/deployment/devconsole + // This can happen if you run an example app in dev mode + // and this app is part of a multi-module project which also declares the extension + // Just try to locate the pom.properties file in the target/maven-archiver directory + // Note that this hack will not work if addMavenDescriptor=false or if the pomPropertiesFile is overriden + Path location = Paths.get(codeLocation.toURI()); + while (!isDeploymentTargetClasses(location) && location.getParent() != null) { + location = location.getParent(); + } + if (location != null) { + Path mavenArchiver = location.getParent().resolve("maven-archiver"); + if (mavenArchiver.toFile().canRead()) { + Entry ret = groupIdAndArtifactId(mavenArchiver); + if (ret == null) { + throw new RuntimeException( + "Unable to determine groupId and artifactId of the extension that contains " + + clazz.getName() + + " because the directory doesn't contain the necessary metadata"); + } + return ret; + } + } + return new AbstractMap.SimpleEntry<>("unspecified", "unspecified"); } else { return new AbstractMap.SimpleEntry<>("unspecified", "unspecified"); } @@ -78,6 +102,21 @@ public static Map.Entry groupIdAndArtifactId(Class clazz, } } + static boolean isDeploymentTargetClasses(Path location) { + if (!location.getFileName().toString().equals("classes")) { + return false; + } + Path target = location.getParent(); + if (target == null || !target.getFileName().toString().equals("target")) { + return false; + } + Path deployment = location.getParent().getParent(); + if (deployment == null || !deployment.getFileName().toString().equals("deployment")) { + return false; + } + return true; + } + /** * Returns a Map.Entry containing the groupId and the artifactId of the module the contains the BuildItem *