From 5d35455492a9e4e502d89193cc3ccc9867dd2294 Mon Sep 17 00:00:00 2001 From: malte_langkabel Date: Tue, 27 Jun 2017 11:41:44 +0200 Subject: [PATCH] logic: Maven fixes regarding JAVA_HOME variable (issue #405) * setting value for JAVA_HOME variable automatically before executing Maven if not found in system environment. * show error dialog when maven encounters an error while fetching source directories from pom * added logging when fetching maven source directories from pom --- src/lib_java/utility/utilityMaven.cpp | 125 +++++++++++++++++--------- 1 file changed, 85 insertions(+), 40 deletions(-) diff --git a/src/lib_java/utility/utilityMaven.cpp b/src/lib_java/utility/utilityMaven.cpp index 2198f3de..5b4c451b 100644 --- a/src/lib_java/utility/utilityMaven.cpp +++ b/src/lib_java/utility/utilityMaven.cpp @@ -1,16 +1,66 @@ #include "utility/utilityMaven.h" +#include "settings/ApplicationSettings.h" #include "utility/file/FilePath.h" +#include "utility/messaging/type/MessageStatus.h" #include "utility/text/TextAccess.h" #include "utility/utilityApp.h" #include "utility/utilityString.h" #include "utility/utilityXml.h" #include "utility/utility.h" +#include "Application.h" + +namespace +{ + void fetchDirectories(std::vector& pathList, std::shared_ptr xmlAccess, const std::vector& tags, const FilePath& toAppend = FilePath()) + { + std::string tagString = ""; + for (size_t i = 0; i < tags.size(); i++) + { + if (i != 0) + { + tagString += " -> "; + } + tagString += tags[i]; + } + LOG_INFO("Fetching directories for \"" + tagString + "\"."); + + std::vector fetchedDirectories = utility::getValuesOfAllXmlElementsOnPath( + xmlAccess, tags + ); + LOG_INFO("Found " + std::to_string(fetchedDirectories.size()) + " directories."); + + for (const std::string& fetchedDirectory: fetchedDirectories) + { + FilePath path(fetchedDirectory); + if (!toAppend.empty()) + { + path = path.concat(toAppend); + } + pathList.push_back(path); + } + } + + void setJavaHomeVariableIfNotExists() + { + if (getenv("JAVA_HOME") == nullptr) + { + const FilePath javaPath(ApplicationSettings::getInstance()->getJavaPath()); + const FilePath javaHomePath = javaPath.parentDirectory().parentDirectory().parentDirectory(); + + LOG_WARNING("Environment variable \"JAVA_HOME\" not found on system. Setting value to \"" + javaHomePath.str() + "\" for this process."); + + putenv(("JAVA_HOME=" + javaHomePath.str()).c_str()); + } + } +} namespace utility { bool mavenGenerateSources(const FilePath& mavenPath, const FilePath& projectDirectoryPath) { + setJavaHomeVariableIfNotExists(); + const std::string output = utility::executeProcess( "\"" + mavenPath.str() + "\" generate-sources", projectDirectoryPath.str() @@ -20,6 +70,8 @@ namespace utility bool mavenCopyDependencies(const FilePath& mavenPath, const FilePath& projectDirectoryPath, const FilePath& outputDirectoryPath) { + setJavaHomeVariableIfNotExists(); + const std::string output = utility::executeProcess( "\"" + mavenPath.str() + "\" dependency:copy-dependencies -DoutputDirectory=" + outputDirectoryPath.str(), projectDirectoryPath.str() @@ -29,12 +81,26 @@ namespace utility std::vector mavenGetAllDirectoriesFromEffectivePom(const FilePath& mavenPath, const FilePath& projectDirectoryPath, bool addTestDirectories) { + setJavaHomeVariableIfNotExists(); std::shared_ptr outputAccess = TextAccess::createFromString(utility::executeProcess( "\"" + mavenPath.str() + "\" help:effective-pom", projectDirectoryPath.str() )); + if (outputAccess->getLineCount() > 0 && utility::isPrefix("Error", utility::trim(outputAccess->getLine(1)))) + { + // TODO: move error handling to caller of this function + const std::string dialogMessage = + "The following error occurred while executing a Maven command:\n\n" + utility::replace(outputAccess->getText(), "\r\n", "\n"); + + MessageStatus(dialogMessage, true, false).dispatch(); + + Application::getInstance()->handleDialog(dialogMessage); + + return std::vector(); + } + std::string xmlContent = ""; for (std::string line: outputAccess->getAllLines()) { @@ -46,52 +112,29 @@ namespace utility } std::shared_ptr xmlAccess = TextAccess::createFromString(xmlContent); - std::vector directories; - - std::vector uncheckedDirectories; - for (std::string value: utility::getValuesOfAllXmlElementsOnPath(xmlAccess, utility::createVectorFromElements("project", "build", "sourceDirectory"))) - { - uncheckedDirectories.push_back(FilePath(value)); - } - - for (std::string value: utility::getValuesOfAllXmlElementsOnPath(xmlAccess, utility::createVectorFromElements("projects", "project", "build", "sourceDirectory"))) - { - uncheckedDirectories.push_back(FilePath(value)); - } - - for (std::string value: utility::getValuesOfAllXmlElementsOnPath(xmlAccess, utility::createVectorFromElements("project", "build", "directory"))) - { - uncheckedDirectories.push_back(FilePath(value).concat(FilePath("generated-sources"))); - } - - for (std::string value: utility::getValuesOfAllXmlElementsOnPath(xmlAccess, utility::createVectorFromElements("projects", "project", "build", "directory"))) - { - uncheckedDirectories.push_back(FilePath(value).concat(FilePath("generated-sources"))); - } + fetchDirectories(uncheckedDirectories, xmlAccess, + utility::createVectorFromElements("project", "build", "sourceDirectory")); + fetchDirectories(uncheckedDirectories, xmlAccess, + utility::createVectorFromElements("projects", "project", "build", "sourceDirectory")); + fetchDirectories(uncheckedDirectories, xmlAccess, + utility::createVectorFromElements("project", "build", "directory"), FilePath("generated-sources")); + fetchDirectories(uncheckedDirectories, xmlAccess, + utility::createVectorFromElements("projects", "project", "build", "directory"), FilePath("generated-sources")); if (addTestDirectories) { - for (std::string value: utility::getValuesOfAllXmlElementsOnPath(xmlAccess, utility::createVectorFromElements("project", "build", "testSourceDirectory"))) - { - uncheckedDirectories.push_back(FilePath(value)); - } - for (std::string value: utility::getValuesOfAllXmlElementsOnPath(xmlAccess, utility::createVectorFromElements("projects", "project", "build", "testSourceDirectory"))) - { - uncheckedDirectories.push_back(FilePath(value)); - } - - for (std::string value: utility::getValuesOfAllXmlElementsOnPath(xmlAccess, utility::createVectorFromElements("project", "build", "directory"))) - { - uncheckedDirectories.push_back(FilePath(value).concat(FilePath("generated-test-sources"))); - } - - for (std::string value: utility::getValuesOfAllXmlElementsOnPath(xmlAccess, utility::createVectorFromElements("projects", "project", "build", "directory"))) - { - uncheckedDirectories.push_back(FilePath(value).concat(FilePath("generated-test-sources"))); - } + fetchDirectories(uncheckedDirectories, xmlAccess, + utility::createVectorFromElements("project", "build", "testSourceDirectory")); + fetchDirectories(uncheckedDirectories, xmlAccess, + utility::createVectorFromElements("projects", "project", "build", "testSourceDirectory")); + fetchDirectories(uncheckedDirectories, xmlAccess, + utility::createVectorFromElements("project", "build", "directory"), FilePath("generated-test-sources")); + fetchDirectories(uncheckedDirectories, xmlAccess, + utility::createVectorFromElements("projects", "project", "build", "directory"), FilePath("generated-test-sources")); } + std::vector directories; for (const FilePath& uncheckedDirectory: uncheckedDirectories) { if (uncheckedDirectory.exists()) @@ -100,6 +143,8 @@ namespace utility } } + LOG_INFO("Found " + std::to_string(directories.size()) + " of " + std::to_string(uncheckedDirectories.size()) + " directories on system."); + return directories; } }