From 23d1d100957ddcdb46e83b02e914fe14a0211e4c Mon Sep 17 00:00:00 2001 From: Alexey Loubyansky Date: Wed, 21 Feb 2024 21:20:01 +0100 Subject: [PATCH] Option to warn about failures when loading workspace modules, instead of throwing errors Co-authored-by: George Gastaldi --- .../quarkus/deployment/BootstrapConfig.java | 7 +++++++ .../resolver/maven/BootstrapMavenContext.java | 13 ++++++++++++ .../maven/BootstrapMavenContextConfig.java | 13 ++++++++++++ .../maven/workspace/WorkspaceLoader.java | 8 +++++++- .../test/LocalWorkspaceDiscoveryTest.java | 20 +++++++++++++++++++ .../invalid-module/invalid-module/pom.xml | 13 ++++++++++++ .../resources/invalid-module/module/pom.xml | 13 ++++++++++++ .../src/test/resources/invalid-module/pom.xml | 14 +++++++++++++ 8 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/invalid-module/pom.xml create mode 100644 independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/module/pom.xml create mode 100644 independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/pom.xml diff --git a/core/deployment/src/main/java/io/quarkus/deployment/BootstrapConfig.java b/core/deployment/src/main/java/io/quarkus/deployment/BootstrapConfig.java index 12448f389fcd0..8f3e059be79f0 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/BootstrapConfig.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/BootstrapConfig.java @@ -24,6 +24,13 @@ public class BootstrapConfig { @ConfigItem(defaultValue = "false") Boolean workspaceDiscovery; + /** + * If set to true, workspace loader will log warnings for modules that could not be loaded for some reason + * instead of throwing errors. + */ + @ConfigItem(defaultValue = "false") + boolean warnOnFailingWorkspaceModules; + /** * By default, the bootstrap mechanism will create a shared cache of open JARs for * Quarkus classloaders to reduce the total number of opened ZIP FileSystems in dev and test modes. diff --git a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java index 5150d282ca810..1a6e83cbaca99 100644 --- a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java +++ b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContext.java @@ -96,6 +96,7 @@ public class BootstrapMavenContext { private static final String SETTINGS_SECURITY = "settings.security"; private static final String EFFECTIVE_MODEL_BUILDER_PROP = "quarkus.bootstrap.effective-model-builder"; + private static final String WARN_ON_FAILING_WS_MODULES_PROP = "quarkus.bootstrap.warn-on-failing-workspace-modules"; private static final String MAVEN_RESOLVER_TRANSPORT_KEY = "maven.resolver.transport"; private static final String MAVEN_RESOLVER_TRANSPORT_DEFAULT = "default"; @@ -112,6 +113,11 @@ public class BootstrapMavenContext { private File userSettings; private File globalSettings; private Boolean offline; + + // Typically, this property will not be enabled in Quarkus application development use-cases + // It was introduced to support use-cases of using the bootstrap resolver API beyond Quarkus application development + private Boolean warnOnFailingWorkspaceModules; + private LocalWorkspace workspace; private LocalProject currentProject; private Settings settings; @@ -152,6 +158,7 @@ public BootstrapMavenContext(BootstrapMavenContextConfig config) this.artifactTransferLogging = config.artifactTransferLogging; this.localRepo = config.localRepo; this.offline = config.offline; + this.warnOnFailingWorkspaceModules = config.warnOnFailedWorkspaceModules; this.repoSystem = config.repoSystem; this.repoSession = config.repoSession; this.remoteRepos = config.remoteRepos; @@ -273,6 +280,12 @@ public boolean isOffline() throws BootstrapMavenException { : offline; } + public boolean isWarnOnFailingWorkspaceModules() { + return warnOnFailingWorkspaceModules == null + ? warnOnFailingWorkspaceModules = Boolean.getBoolean(WARN_ON_FAILING_WS_MODULES_PROP) + : warnOnFailingWorkspaceModules; + } + public RepositorySystem getRepositorySystem() throws BootstrapMavenException { if (repoSystem == null) { initRepoSystemAndManager(); diff --git a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContextConfig.java b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContextConfig.java index 0f44dcc37a8ec..d3539c5d16b1e 100644 --- a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContextConfig.java +++ b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/BootstrapMavenContextConfig.java @@ -39,6 +39,7 @@ public class BootstrapMavenContextConfig modelProvider; protected List excludeSisuBeanPackages; protected List includeSisuBeanPackages; + protected Boolean warnOnFailedWorkspaceModules; public T excludeSisuBeanPackage(String packageName) { if (excludeSisuBeanPackages == null) { @@ -320,6 +321,18 @@ public T setProjectModelProvider(Function modelProvider) { return (T) this; } + /** + * Whether to warn about failures loading workspace modules instead of throwing errors + * + * @param warnOnFailedWorkspaceModules whether to warn about failures loading workspace modules instead of throwing errors + * @return this config instance + */ + @SuppressWarnings("unchecked") + public T setWarnOnFailedWorkspaceModules(boolean warnOnFailedWorkspaceModules) { + this.warnOnFailedWorkspaceModules = warnOnFailedWorkspaceModules; + return (T) this; + } + private BootstrapMavenOptions getInitializedCliOptions() { return cliOptions == null ? cliOptions = BootstrapMavenOptions.newInstance() : cliOptions; } diff --git a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/workspace/WorkspaceLoader.java b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/workspace/WorkspaceLoader.java index 77cd35b4963fd..604e541f2328a 100644 --- a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/workspace/WorkspaceLoader.java +++ b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/workspace/WorkspaceLoader.java @@ -61,6 +61,7 @@ private static Path locateCurrentProjectPom(Path path) throws BootstrapMavenExce private final LocalWorkspace workspace = new LocalWorkspace(); private final Path currentProjectPom; + private boolean warnOnFailingWsModules; private ModelBuilder modelBuilder; private BootstrapModelResolver modelResolver; @@ -94,6 +95,7 @@ private static Path locateCurrentProjectPom(Path path) throws BootstrapMavenExce } activeProfileIds.addAll(cliOptions.getActiveProfileIds()); inactiveProfileIds = cliOptions.getInactiveProfileIds(); + warnOnFailingWsModules = ctx.isWarnOnFailingWorkspaceModules(); } workspace.setBootstrapMavenContext(ctx); } @@ -131,10 +133,14 @@ LocalProject load() throws BootstrapMavenException { req.setProfiles(profiles); req.setRawModel(rawModel); req.setWorkspaceModelResolver(this); - LocalProject project; + LocalProject project = null; try { project = new LocalProject(modelBuilder.build(req), workspace); } catch (Exception e) { + if (warnOnFailingWsModules) { + log.warn("Failed to resolve effective model for " + rawModel.getPomFile(), e); + return; + } throw new RuntimeException("Failed to resolve the effective model for " + rawModel.getPomFile(), e); } if (currentProject.get() == null && project.getDir().equals(currentProjectPom.getParent())) { diff --git a/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/LocalWorkspaceDiscoveryTest.java b/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/LocalWorkspaceDiscoveryTest.java index 1af195dd079b7..7f2f880961e4f 100644 --- a/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/LocalWorkspaceDiscoveryTest.java +++ b/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/LocalWorkspaceDiscoveryTest.java @@ -667,6 +667,26 @@ public void testBuildDirs() throws Exception { assertEquals(parentDir.resolve("custom-target").resolve("test-classes"), parent.getTestClassesDir()); } + @Test + public void warnOnFailingWorkspaceModules() throws Exception { + final URL moduleUrl = Thread.currentThread().getContextClassLoader() + .getResource("invalid-module"); + assertNotNull(moduleUrl); + final Path moduleDir = Path.of(moduleUrl.toURI()); + assertNotNull(moduleUrl); + + final LocalWorkspace ws = new BootstrapMavenContext(BootstrapMavenContext.config() + .setOffline(true) + .setEffectiveModelBuilder(true) + .setWarnOnFailedWorkspaceModules(true) + .setCurrentProject(moduleDir.toString())) + .getWorkspace(); + + assertNotNull(ws.getProject("io.playground", "asm")); + assertNotNull(ws.getProject("io.playground", "module")); + assertEquals(2, ws.getProjects().size()); + } + private void testMavenCiFriendlyVersion(String placeholder, String testResourceDirName, String expectedResolvedVersion, boolean resolvesFromWorkspace) throws Exception { final URL module1Url = Thread.currentThread().getContextClassLoader() diff --git a/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/invalid-module/pom.xml b/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/invalid-module/pom.xml new file mode 100644 index 0000000000000..95ef0302a1f05 --- /dev/null +++ b/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/invalid-module/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + + io.playground + asm + 2.0-SNAPSHOT + ../pom.xml + + invalid-module + + diff --git a/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/module/pom.xml b/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/module/pom.xml new file mode 100644 index 0000000000000..6ab65dda5cb1b --- /dev/null +++ b/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/module/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + + io.playground + asm + 1.0-SNAPSHOT + ../pom.xml + + module + + diff --git a/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/pom.xml b/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/pom.xml new file mode 100644 index 0000000000000..f06625a636c29 --- /dev/null +++ b/independent-projects/bootstrap/maven-resolver/src/test/resources/invalid-module/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + io.playground + asm + 1.0-SNAPSHOT + pom + + module + invalid-module + + +