Skip to content

Commit

Permalink
Merge pull request #395 from nebula-plugins/fail-on-missing-mainClass
Browse files Browse the repository at this point in the history
OspackageApplicationSpringBootPlugin: fail build if mainClass/mainClassName are not configured
  • Loading branch information
rpalcolea committed Feb 18, 2021
2 parents 12b727a + 34e7bf6 commit b8b24c0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
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
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

/**
Expand Down Expand Up @@ -75,15 +77,23 @@ class OspackageApplicationSpringBootPlugin implements Plugin<Project> {
}
}
}
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'")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down Expand Up @@ -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' | _
}
}

0 comments on commit b8b24c0

Please sign in to comment.