diff --git a/src/main/groovy/io/openliberty/tools/gradle/LibertyTasks.groovy b/src/main/groovy/io/openliberty/tools/gradle/LibertyTasks.groovy index d7a0aab8..10d896d6 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/LibertyTasks.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/LibertyTasks.groovy @@ -1,5 +1,5 @@ /* - * (C) Copyright IBM Corporation 2018, 2021. + * (C) Copyright IBM Corporation 2018, 2023. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,6 +79,13 @@ public class LibertyTasks { project.cleanDirs { dependsOn 'libertyStop' } + + // The war, ear and java plugins all apply the base plugin which defines the clean task. + // This code ensures the libertyStop task is invoked before clean so that a Liberty server is not orphaned. + if (project.plugins.hasPlugin('base')) { + def tasks = project.tasks + tasks.getByName('clean').dependsOn 'libertyStop' + } project.prepareFeature { dependsOn 'installLiberty' diff --git a/src/main/groovy/io/openliberty/tools/gradle/tasks/CleanTask.groovy b/src/main/groovy/io/openliberty/tools/gradle/tasks/CleanTask.groovy index 0fe67e27..659e235a 100644 --- a/src/main/groovy/io/openliberty/tools/gradle/tasks/CleanTask.groovy +++ b/src/main/groovy/io/openliberty/tools/gradle/tasks/CleanTask.groovy @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corporation 2015, 2019. + * (C) Copyright IBM Corporation 2015, 2023. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,15 +29,26 @@ class CleanTask extends AbstractServerTask { @TaskAction void cleanDirectories() { - def params = buildLibertyMap(project); - params.put('logs', server.cleanDir.logs) - params.put('workarea', server.cleanDir.workarea) - params.put('dropins', server.cleanDir.dropins) - params.put('apps', server.cleanDir.apps) - params.remove('timeout') - project.ant.taskdef(name: 'cleanDir', - classname: 'io.openliberty.tools.ant.CleanTask', - classpath: project.buildscript.configurations.classpath.asPath) - project.ant.cleanDir(params) + // first make sure there is a Liberty installation that needs cleaning as a previous clean may have occurred + File installDir = getInstallDir(project) + if (isLibertyInstalled(installDir)) { + def params = buildLibertyMap(project); + params.put('logs', server.cleanDir.logs) + params.put('workarea', server.cleanDir.workarea) + params.put('dropins', server.cleanDir.dropins) + params.put('apps', server.cleanDir.apps) + params.remove('timeout') + project.ant.taskdef(name: 'cleanDir', + classname: 'io.openliberty.tools.ant.CleanTask', + classpath: project.buildscript.configurations.classpath.asPath) + project.ant.cleanDir(params) + } else { + logger.info("There is no Liberty server to clean. The runtime has not been installed.") + } + } + + private boolean isLibertyInstalled(File installDir) { + boolean installationExists = installDir.exists() && new File(installDir,"lib/ws-launch.jar").exists() + return installationExists } } diff --git a/src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy b/src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy index 7e9b1a0e..4ac53556 100644 --- a/src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy +++ b/src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy @@ -1,5 +1,5 @@ /* - * (C) Copyright IBM Corporation 2015, 2017. + * (C) Copyright IBM Corporation 2015, 2023. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -147,9 +147,44 @@ class LibertyTest extends AbstractIntegrationTest{ try{ runTasks(buildDir, 'cleanDirs') } catch (Exception e) { - throw new AssertionError ("Fail on task Clean. "+e) + throw new AssertionError ("Fail on task cleanDirs. "+e) + } + + try{ + runTasks(buildDir, 'libertyStart') + } catch (Exception e) { + throw new AssertionError ("Fail on task libertyStart after cleanDirs. "+e) + } + + try{ + runTasks(buildDir, 'clean') + } catch (Exception e) { + throw new AssertionError ("Fail on task clean while Liberty server is running "+e) + } + + try{ + runTasks(buildDir, 'clean') + } catch (Exception e) { + throw new AssertionError ("Fail on task clean after clean. "+e) } - } + try{ + runTasks(buildDir, 'cleanDirs') + } catch (Exception e) { + throw new AssertionError ("Fail on task cleanDirs after clean. "+e) + } + + try{ + runTasks(buildDir, 'libertyStart') + } catch (Exception e) { + throw new AssertionError ("Fail on task libertyStart after second clean. "+e) + } + + try{ + runTasks(buildDir, 'libertyStop') + } catch (Exception e) { + throw new AssertionError ("Fail on task libertyStop after libertyStart. "+e) + } + } } diff --git a/src/test/resources/dev-test/basic-dev-project/build.gradle b/src/test/resources/dev-test/basic-dev-project/build.gradle index 4b853828..ed1fa7c4 100644 --- a/src/test/resources/dev-test/basic-dev-project/build.gradle +++ b/src/test/resources/dev-test/basic-dev-project/build.gradle @@ -40,5 +40,3 @@ dependencies { testImplementation "org.glassfish:javax.json:1.0.4" testImplementation "javax.xml.bind:jaxb-api:2.3.1" } - -clean.dependsOn "libertyStop" \ No newline at end of file diff --git a/src/test/resources/generate-features-test/basic-dev-project/build.gradle b/src/test/resources/generate-features-test/basic-dev-project/build.gradle index 3b3a10ad..c3619c43 100644 --- a/src/test/resources/generate-features-test/basic-dev-project/build.gradle +++ b/src/test/resources/generate-features-test/basic-dev-project/build.gradle @@ -43,5 +43,3 @@ dependencies { testImplementation "org.glassfish:javax.json:1.0.4" testImplementation "javax.xml.bind:jaxb-api:2.3.1" } - -clean.dependsOn "libertyStop" \ No newline at end of file diff --git a/src/test/resources/generate-features-test/restful/build.gradle b/src/test/resources/generate-features-test/restful/build.gradle index bf92c927..fe13ae88 100644 --- a/src/test/resources/generate-features-test/restful/build.gradle +++ b/src/test/resources/generate-features-test/restful/build.gradle @@ -46,5 +46,3 @@ dependencies { testImplementation "org.glassfish:javax.json:1.0.4" testImplementation "javax.xml.bind:jaxb-api:2.3.1" } - -clean.dependsOn "libertyStop" \ No newline at end of file diff --git a/src/test/resources/liberty-test/build.gradle b/src/test/resources/liberty-test/build.gradle index 71f6d9fe..74a25d15 100644 --- a/src/test/resources/liberty-test/build.gradle +++ b/src/test/resources/liberty-test/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'liberty' +apply plugin: 'java' buildscript { repositories { diff --git a/src/test/resources/loose-ear-earlibs-test/SampleEAR/build.gradle b/src/test/resources/loose-ear-earlibs-test/SampleEAR/build.gradle index 97663586..710b97db 100644 --- a/src/test/resources/loose-ear-earlibs-test/SampleEAR/build.gradle +++ b/src/test/resources/loose-ear-earlibs-test/SampleEAR/build.gradle @@ -51,6 +51,5 @@ liberty { } } -clean.dependsOn 'libertyStop' deploy.dependsOn 'ear' ear.dependsOn ':SampleEJB:jar', ':SampleWAR:war', ':SampleWAR2:war' diff --git a/src/test/resources/loose-ear-test/ejb-ear/build.gradle b/src/test/resources/loose-ear-test/ejb-ear/build.gradle index e7207d74..923e0c80 100644 --- a/src/test/resources/loose-ear-test/ejb-ear/build.gradle +++ b/src/test/resources/loose-ear-test/ejb-ear/build.gradle @@ -51,6 +51,5 @@ liberty { } } -clean.dependsOn 'libertyStop' deploy.dependsOn 'ear' ear.dependsOn ':ejb-war:jar', ':ejb-war:war' diff --git a/src/test/resources/multi-module-loose-ear-test/ejb-ear/build.gradle b/src/test/resources/multi-module-loose-ear-test/ejb-ear/build.gradle index 1c3468b4..9864d3e6 100644 --- a/src/test/resources/multi-module-loose-ear-test/ejb-ear/build.gradle +++ b/src/test/resources/multi-module-loose-ear-test/ejb-ear/build.gradle @@ -50,6 +50,5 @@ liberty { } } -clean.dependsOn 'libertyStop' deploy.dependsOn 'ear' ear.dependsOn ':ejb-war:jar', ':ejb-war:war'