diff --git a/src/main/groovy/io/openliberty/tools/gradle/tasks/AbstractServerTask.groovy b/src/main/groovy/io/openliberty/tools/gradle/tasks/AbstractServerTask.groovy index 5fbedf11..0f5b4cdd 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/tasks/AbstractServerTask.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/tasks/AbstractServerTask.groovy @@ -208,6 +208,20 @@ abstract class AbstractServerTask extends AbstractLibertyTask { } } + // Use this method to copy over the server.xml file from the defaultServer template. + // Returns true if the server.xml file does not exist and the defaultServer template server.xml file is copied over, false otherwise. + protected boolean copyDefaultServerTemplate(File installDir, File serverDir) { + File serverXmlFile = new File(serverDir, "server.xml") + if (!serverXmlFile.exists()) { + File defaultServerTemplate = new File(installDir, "templates/servers/defaultServer/server.xml") + if (defaultServerTemplate.exists()) { + Files.copy(defaultServerTemplate.toPath(), serverXmlFile.toPath(), StandardCopyOption.REPLACE_EXISTING) + return true; + } + } + return false; + } + protected void copyConfigDirectory() { //merge default server.env with one in config directory File configDirServerEnv = new File(server.configDirectory, "server.env") diff --git a/src/main/groovy/io/openliberty/tools/gradle/tasks/CreateTask.groovy b/src/main/groovy/io/openliberty/tools/gradle/tasks/CreateTask.groovy index cd792daa..7db22dc4 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/tasks/CreateTask.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/tasks/CreateTask.groovy @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2014, 2022. + * (C) Copyright IBM Corporation 2014, 2023. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +33,7 @@ class CreateTask extends AbstractServerTask { group 'Liberty' }) outputs.upToDateWhen { - getServerDir(project).exists() && new File(getServerDir(project), 'server.xml') + getServerDir(project).exists() && (new File(getServerDir(project), 'server.xml')).exists() } } @@ -76,7 +76,9 @@ class CreateTask extends AbstractServerTask { void create() { //Checking etc/server.env for outputDirs Liberty.checkEtcServerEnvProperties(project) - if(!getServerDir(project).exists()){ + File serverDir = getServerDir(project) + File serverXmlFile = new File(serverDir, "server.xml") + if(!serverDir.exists()){ def params = buildLibertyMap(project); if (server.template != null && server.template.length() != 0) { params.put('template', server.template) @@ -85,6 +87,10 @@ class CreateTask extends AbstractServerTask { params.put('noPassword', server.noPassword) } executeServerCommand(project, 'create', params) + } else if (copyDefaultServerTemplate(getInstallDir(project), serverDir)) { + // copied defaultServer template server.xml over for rare case that the server.xml disappears from an existing Liberty server (issue 850) + // if the project contains its own server.xml file, it will get copied over in copyConfigFiles() next + logger.warn("The " + serverXmlFile.getAbsolutePath() + " does not exist. Copying over the defaultServer template server.xml file.") } copyConfigFiles() writeServerPropertiesToXml(project) diff --git a/src/main/groovy/io/openliberty/tools/gradle/tasks/RunTask.groovy b/src/main/groovy/io/openliberty/tools/gradle/tasks/RunTask.groovy index 00c670f3..be34e5ae 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/tasks/RunTask.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/tasks/RunTask.groovy @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2014, 2019. + * (C) Copyright IBM Corporation 2014, 2023. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,10 +32,19 @@ class RunTask extends AbstractServerTask { void run() { addShutdownHook { if (isLibertyInstalledAndValid(project)) { - if (getServerDir(project).exists()) { - ServerTask serverTaskStop = createServerTask(project, "stop"); - serverTaskStop.setUseEmbeddedServer(server.embedded) - serverTaskStop.execute() + File serverDir = getServerDir(project) + if (serverDir.exists()) { + // copy default server template server.xml file over if the server.xml file does not exist so that the server can be stopped + boolean defaultServerTemplateUsed = copyDefaultServerTemplate(getInstallDir(project), serverDir) + File serverXmlFile = new File(getServerDir(project),"server.xml") + if (serverXmlFile.exists()) { + ServerTask serverTaskStop = createServerTask(project, "stop"); + serverTaskStop.setUseEmbeddedServer(server.embedded) + serverTaskStop.execute() + } + if (defaultServerTemplateUsed) { + serverXmlFile.delete() // delete the temporary copy of the server.xml file + } } } } @@ -58,6 +67,6 @@ class RunTask extends AbstractServerTask { run_process.inputStream.eachLine { println it } - } + } } } diff --git a/src/main/groovy/io/openliberty/tools/gradle/tasks/StatusTask.groovy b/src/main/groovy/io/openliberty/tools/gradle/tasks/StatusTask.groovy index d96fc7ce..5f597d0f 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/tasks/StatusTask.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/tasks/StatusTask.groovy @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2014, 2019. + * (C) Copyright IBM Corporation 2014, 2023. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,9 +29,23 @@ class StatusTask extends AbstractServerTask { @TaskAction void status() { - def status_process = new ProcessBuilder(buildCommand("status")).redirectErrorStream(true).start() - status_process.inputStream.eachLine { - println it + if (isLibertyInstalledAndValid(project)) { + File serverDir = getServerDir(project) + if (serverDir.exists()) { + File serverXmlFile = new File(serverDir,"server.xml") + if (serverXmlFile.exists()) { + def status_process = new ProcessBuilder(buildCommand("status")).redirectErrorStream(true).start() + status_process.inputStream.eachLine { + println it + } + } else { + logger.error ('The server status cannot be checked. There is no server.xml file in the server.') + } + } else { + logger.error ('The server status cannot be checked. The server has not been created.') + } + } else { + logger.error ('The server status cannot be checked. The runtime has not been installed.') } } diff --git a/src/main/groovy/io/openliberty/tools/gradle/tasks/StopTask.groovy b/src/main/groovy/io/openliberty/tools/gradle/tasks/StopTask.groovy index c58de8c8..3522b38b 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/tasks/StopTask.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/tasks/StopTask.groovy @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2014, 2020. + * (C) Copyright IBM Corporation 2014, 2023. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,12 +31,26 @@ class StopTask extends AbstractServerTask { @TaskAction void stop() { if (isLibertyInstalledAndValid(project)) { - if (getServerDir(project).exists()) { - ServerTask serverTaskStop = createServerTask(project, "stop"); - serverTaskStop.setUseEmbeddedServer(server.embedded) - serverTaskStop.execute() + File serverDir = getServerDir(project) + if (serverDir.exists()) { + File serverXmlFile = new File(serverDir,"server.xml") + boolean defaultServerTemplateUsed = copyDefaultServerTemplate(getInstallDir(project),serverDir) + if (serverXmlFile.exists()) { + ServerTask serverTaskStop = createServerTask(project, "stop"); + serverTaskStop.setUseEmbeddedServer(server.embedded) + serverTaskStop.execute() + } else { + logger.error ('The server cannot be stopped. There is no server.xml file in the server.') + } + + if (defaultServerTemplateUsed) { + logger.warn ('The server.xml file was missing in the server during the stop task. Copied the defaultServer template server.xml file into the server temporarily so the stop task could be completed.') + if (!serverXmlFile.delete()) { + logger.error('Could not delete the server.xml file copied from the defaultServer template after stopping the server.') + } + } } else { - logger.error ('There is no server to stop. The server has not been created.') + logger.error ('There is no server to stop. The server has not been created.') } } else { logger.error ('There is no server to stop. The runtime has not been installed.') diff --git a/src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy b/src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy index 333d6b89..e521645c 100644 --- a/src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy +++ b/src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy @@ -28,6 +28,7 @@ class LibertyTest extends AbstractIntegrationTest{ static File resourceDir = new File("build/resources/test/liberty-test") static File buildDir = new File(integTestDir, "/liberty-test") static String buildFilename = "build.gradle" + static File serverXmlFile = new File(buildDir, "/build/wlp/usr/servers/LibertyProjectServer/server.xml") @BeforeClass public static void setup() { @@ -180,10 +181,30 @@ class LibertyTest extends AbstractIntegrationTest{ throw new AssertionError ("Fail on task libertyStart after second clean.", e) } + // try deleting the server.xml and see if we can recover + assert serverXmlFile.exists() : 'server.xml file does not exist in LibertyProjectServer' + assert serverXmlFile.delete() : 'server.xml could not be deleted in LibertyProjectServer' + try{ runTasks(buildDir, 'libertyStop') } catch (Exception e) { - throw new AssertionError ("Fail on task libertyStop after libertyStart.", e) + throw new AssertionError ("Fail on task libertyStop after deleting server.xml.", e) + } + + assert !serverXmlFile.exists() : 'server.xml file unexpectedly exists in LibertyProjectServer after libertyStop' + + try{ + runTasks(buildDir, 'libertyStatus') + } catch (Exception e) { + throw new AssertionError ("Fail on task libertyStatus after deleting server.xml.", e) + } + + assert serverXmlFile.exists() : 'server.xml file does not exist in LibertyProjectServer after libertyStatus' + + try{ + runTasks(buildDir, 'clean') + } catch (Exception e) { + throw new AssertionError ("Fail on task clean after deleting server.xml.", e) } }