Skip to content

Commit

Permalink
Disable feature generation if the binary scanner jar cannot be found (#…
Browse files Browse the repository at this point in the history
…680)

* Disable feature generation if the binary scanner jar cannot be found

Signed-off-by: Kathryn Kodama <[email protected]>

* Revert binary app scanner jar to correct name

Signed-off-by: Kathryn Kodama <[email protected]>

* Update error messgae to Gradle task name

Signed-off-by: Kathryn Kodama <[email protected]>
  • Loading branch information
kathrynkodama committed Dec 16, 2021
1 parent bad4ec7 commit 9e3441c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
41 changes: 38 additions & 3 deletions src/main/groovy/io/openliberty/tools/gradle/tasks/DevTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 9e3441c

Please sign in to comment.