diff --git a/src/main/groovy/com/netflix/gradle/plugins/application/OspackageApplicationSpringBootPlugin.groovy b/src/main/groovy/com/netflix/gradle/plugins/application/OspackageApplicationSpringBootPlugin.groovy index 4c753b5d..6c565f52 100644 --- a/src/main/groovy/com/netflix/gradle/plugins/application/OspackageApplicationSpringBootPlugin.groovy +++ b/src/main/groovy/com/netflix/gradle/plugins/application/OspackageApplicationSpringBootPlugin.groovy @@ -17,6 +17,7 @@ package com.netflix.gradle.plugins.application import groovy.transform.CompileDynamic +import org.gradle.api.GradleException import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.distribution.plugins.DistributionPlugin @@ -24,6 +25,7 @@ import org.gradle.api.invocation.Gradle import org.gradle.api.plugins.ApplicationPlugin import org.gradle.api.plugins.JavaPlugin import org.gradle.api.tasks.application.CreateStartScripts +import org.gradle.tooling.BuildException import org.gradle.util.GradleVersion /** @@ -75,15 +77,23 @@ class OspackageApplicationSpringBootPlugin implements Plugin { } } } - try { + + if(GradleVersion.current().baseVersion < GradleVersion.version('6.4').baseVersion) { + // Allow the springBoot extension configuration to propagate to the application plugin + if (project.application.mainClassName == null) { + project.application.mainClassName = project.springBoot.mainClassName + } + if(!project.application.mainClassName) { + throw new GradleException("mainClassName should be configured in order to generate a valid debian file. Ex. mainClassName = 'com.netflix.app.MyApp'") + } + } else { // Allow the springBoot extension configuration to propagate to the application plugin if (!project.application.mainClass.isPresent()) { project.application.mainClass.set(project.springBoot.mainClassName) } - } catch (MissingPropertyException ignore) { - // Releases prior to Gradle 6.4 - if (project.application.mainClassName == null) { - project.application.mainClassName = project.springBoot.mainClassName + + if(!project.application.mainClass.isPresent()) { + throw new GradleException("mainClass should be configured in order to generate a valid debian file. Ex. mainClass = 'com.netflix.app.MyApp'") } } } diff --git a/src/test/groovy/com/netflix/gradle/plugins/application/OspackageApplicationSpringBootPluginLauncherSpec.groovy b/src/test/groovy/com/netflix/gradle/plugins/application/OspackageApplicationSpringBootPluginLauncherSpec.groovy index 6e3927ca..e2b7b3b7 100644 --- a/src/test/groovy/com/netflix/gradle/plugins/application/OspackageApplicationSpringBootPluginLauncherSpec.groovy +++ b/src/test/groovy/com/netflix/gradle/plugins/application/OspackageApplicationSpringBootPluginLauncherSpec.groovy @@ -19,9 +19,15 @@ package com.netflix.gradle.plugins.application import com.google.common.base.Throwables import com.netflix.gradle.plugins.deb.Scanner import nebula.test.IntegrationSpec +import org.junit.Rule +import org.junit.contrib.java.lang.system.ProvideSystemProperty import spock.lang.Unroll class OspackageApplicationSpringBootPluginLauncherSpec extends IntegrationSpec { + + @Rule + public final ProvideSystemProperty ignoreDeprecations = new ProvideSystemProperty("ignoreDeprecations", "true") + def 'plugin throws exception if spring-boot plugin not applied'() { buildFile << """ ${applyPlugin(OspackageApplicationSpringBootPlugin)} @@ -180,4 +186,48 @@ class OspackageApplicationSpringBootPluginLauncherSpec extends IntegrationSpec { bootVersion | fileMode '2.4.2' | 493 } + + @Unroll + def 'application fails if mainClass is not present'() { + final applicationDir = "$moduleName-boot" + final startScript = file("build/install/$applicationDir/bin/$moduleName") + + buildFile << buildScript(bootVersion, startScript) + buildFile << """ + mainClassName = null + mainClass.set(null) + """ + + when: + def result = runTasksWithFailure('runStartScript') + + then: + result.standardError.contains("mainClass should be configured in order to generate a valid debian file. Ex. mainClass = 'com.netflix.app.MyApp'") + + where: + bootVersion | _ + '2.4.2' | _ + } + + @Unroll + def 'application fails if mainClassName is not present (old versions of Gradle)'() { + final applicationDir = "$moduleName-boot" + final startScript = file("build/install/$applicationDir/bin/$moduleName") + + gradleVersion = '6.3' + buildFile << buildScript(bootVersion, startScript) + buildFile << """ + mainClassName = null + """ + + when: + def result = runTasksWithFailure('runStartScript') + + then: + result.standardError.contains("mainClassName should be configured in order to generate a valid debian file. Ex. mainClassName = 'com.netflix.app.MyApp'") + + where: + bootVersion | _ + '2.4.2' | _ + } }