From 9e3441c22880df85c20692e64d27d87f6c917ad9 Mon Sep 17 00:00:00 2001 From: Kathryn Kodama Date: Wed, 15 Dec 2021 20:13:24 -0500 Subject: [PATCH] Disable feature generation if the binary scanner jar cannot be found (#680) * Disable feature generation if the binary scanner jar cannot be found Signed-off-by: Kathryn Kodama * Revert binary app scanner jar to correct name Signed-off-by: Kathryn Kodama * Update error messgae to Gradle task name Signed-off-by: Kathryn Kodama --- .../tools/gradle/tasks/DevTask.groovy | 41 +++++++++++++++++-- .../gradle/tasks/GenerateFeaturesTask.groovy | 11 +++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/main/groovy/io/openliberty/tools/gradle/tasks/DevTask.groovy b/src/main/groovy/io/openliberty/tools/gradle/tasks/DevTask.groovy index 58f01dd8..60286124 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/tasks/DevTask.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/tasks/DevTask.groovy @@ -790,8 +790,15 @@ class DevTask extends AbstractFeatureTask { runGenerateFeaturesTask(gradleBuildLauncher, options); return true; // successfully generated features } catch (BuildException e) { - // log as error instead of throwing an exception so we do not flood console with stacktrace - logger.error(e.getMessage() + ".\n To disable the automatic generation of features, type 'g' and press Enter."); + // log errors instead of throwing an exception so we do not flood console with stacktrace + Exception pluginEx = getPluginExecutionException(e); + if (pluginEx != null) { + // PluginExecutionException indicates that the binary scanner jar could not be found + logger.error(pluginEx.getMessage() + ".\nDisabling the automatic generation of features."); + setFeatureGeneration(false); + } else { + logger.error(e.getMessage() + ".\nTo disable the automatic generation of features, type 'g' and press Enter."); + } return false; } finally { gradleConnection.close(); @@ -1025,7 +1032,16 @@ class DevTask extends AbstractFeatureTask { try { runGenerateFeaturesTask(gradleBuildLauncher, true); } catch (BuildException e) { - throw new BuildException(e.getCause().getMessage() + " To disable the automatic generation of features, start dev mode with --generateFeatures=false.", e); + Exception pluginEx = getPluginExecutionException(e); + if (pluginEx != null) { + // PluginExecutionException indicates that the binary scanner jar could not be found + logger.error(pluginEx.getMessage() + ".\nDisabling the automatic generation of features."); + generateFeatures = false; + } else if (e.getCause() != null) { + throw new BuildException(e.getCause().getMessage() + " To disable the automatic generation of features, start dev mode with --generateFeatures=false.", e.getCause()); + } else { + throw new BuildException("Failed to run the generateFeaturesTask. To disable the automatic generation of features, start dev mode with --generateFeatures=false.", e) + } } } if (!container) { @@ -1196,4 +1212,23 @@ class DevTask extends AbstractFeatureTask { buildLauncher.run(); } + /** + * Traces root causes of the passed exception and returns a PluginExecutionException if found + * @param e Exception to search + * @return PluginExecutionException or null if could not be found + */ + Exception getPluginExecutionException(Exception exception) { + Exception rootCause = exception; + while (rootCause.getCause() != null && rootCause.getCause() != exception) { + // compare class strings to verify if a PluginExecutionException is present + // using "rootCause instanceof PluginExecutionException" will return false + if (rootCause.getClass().toString().equals(PluginExecutionException.toString())) { + logger.debug("Found PluginExecutionException indicating that the binary-app-scanner.jar could not be resolved") + return rootCause; + } + rootCause = rootCause.getCause(); + } + return null; + } + } diff --git a/src/main/groovy/io/openliberty/tools/gradle/tasks/GenerateFeaturesTask.groovy b/src/main/groovy/io/openliberty/tools/gradle/tasks/GenerateFeaturesTask.groovy index b85c241d..8bb3ba3d 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/tasks/GenerateFeaturesTask.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/tasks/GenerateFeaturesTask.groovy @@ -199,14 +199,19 @@ class GenerateFeaturesTask extends AbstractFeatureTask { * Downloads it first from connected repositories such as Maven Central if a newer release is available than the cached version. * Note: Maven updates artifacts daily by default based on the last updated timestamp. Users should use 'mvn -U' to force updates if needed. * - * @return The File object of the binary scanner jar in the local cache. - * @throws PluginExecutionException + * @return The File object of the binary-app-scanner.jar in the local cache. + * @throws PluginExecutionException indicates the binary-app-scanner.jar could not be found */ private File getBinaryScannerJarFromRepository() throws PluginExecutionException { try { return ArtifactDownloadUtil.downloadBuildArtifact(project, BINARY_SCANNER_MAVEN_GROUP_ID, BINARY_SCANNER_MAVEN_ARTIFACT_ID, BINARY_SCANNER_MAVEN_TYPE, BINARY_SCANNER_MAVEN_VERSION); } catch (Exception e) { - throw new PluginExecutionException("Could not retrieve the binary scanner jar. Ensure you have a connection to Maven Central or another repository that contains the jar configured in your build.gradle: " + e.getMessage(), e); + throw new PluginExecutionException("Could not retrieve the artifact " + BINARY_SCANNER_MAVEN_GROUP_ID + "." + + BINARY_SCANNER_MAVEN_ARTIFACT_ID + + " needed for generateFeatures. Ensure you have a connection to Maven Central or another repository that contains the " + + BINARY_SCANNER_MAVEN_GROUP_ID + "." + BINARY_SCANNER_MAVEN_ARTIFACT_ID + + ".jar configured in your build.gradle", + e); } }