From 6c8c2e46dc73b762d9ae72ace80923c2a5cdaca7 Mon Sep 17 00:00:00 2001 From: Alexey Loubyansky Date: Thu, 24 Oct 2024 23:00:11 +0200 Subject: [PATCH] Introduce ApplicationModel.getApplicationModuleDirectory() --- .../pkg/builditem/CurateOutcomeBuildItem.java | 38 +++++++++++++++++++ .../bootstrap/utils/BuildToolHelper.java | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/builditem/CurateOutcomeBuildItem.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/builditem/CurateOutcomeBuildItem.java index 7c069f9753e2e0..74d1a9e56d6507 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/builditem/CurateOutcomeBuildItem.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/builditem/CurateOutcomeBuildItem.java @@ -1,11 +1,17 @@ package io.quarkus.deployment.pkg.builditem; +import java.nio.file.Files; +import java.nio.file.Path; + import io.quarkus.bootstrap.model.ApplicationModel; +import io.quarkus.bootstrap.utils.BuildToolHelper; +import io.quarkus.bootstrap.workspace.WorkspaceModule; import io.quarkus.builder.item.SimpleBuildItem; public final class CurateOutcomeBuildItem extends SimpleBuildItem { private final ApplicationModel appModel; + private Path appModuleDir; public CurateOutcomeBuildItem(ApplicationModel appModel) { this.appModel = appModel; @@ -14,4 +20,36 @@ public CurateOutcomeBuildItem(ApplicationModel appModel) { public ApplicationModel getApplicationModel() { return appModel; } + + /** + * Returns the application module directory, if the application is built from a source project. + * For a single module project it will the project directory. For a multimodule project, + * it will the directory of the application module. + *

+ * During re-augmentation of applications packaged as {@code mutable-jar} this method will return the current directory, + * since the source project might not be available anymore. + * + * @return application module directory, never null + */ + public Path getApplicationModuleDirectory() { + // modules are by default available in dev and test modes, and in prod mode if + // quarkus.bootstrap.workspace-discovery system or project property is true, + // otherwise it could be null + final WorkspaceModule module = appModel.getApplicationModule(); + return module == null ? (appModuleDir = deriveModuleDirectoryFromArtifact()) : module.getModuleDir().toPath(); + } + + private Path deriveModuleDirectoryFromArtifact() { + var paths = appModel.getAppArtifact().getResolvedPaths(); + for (var path : paths) { + if (Files.isDirectory(path)) { + var moduleDir = BuildToolHelper.getProjectDir(path); + if (moduleDir != null) { + return moduleDir; + } + } + } + // the module isn't available, return the current directory + return Path.of("").toAbsolutePath(); + } } diff --git a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/utils/BuildToolHelper.java b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/utils/BuildToolHelper.java index 637db4e7c9bd00..5c3f6427813c4d 100644 --- a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/utils/BuildToolHelper.java +++ b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/utils/BuildToolHelper.java @@ -61,7 +61,7 @@ public static Path getProjectDir(Path p) { } currentPath = currentPath.getParent(); } - log.warnv("Unable to find a project directory for {0}.", p.toString()); + log.warnv("Unable to find the project directory for {0}.", p); return null; }