From 84b2d8e06ae9dd91b1ce273b3a4051f538379f5a Mon Sep 17 00:00:00 2001 From: Klaus Lehner <172195+klu2@users.noreply.github.com> Date: Tue, 17 May 2022 16:46:14 +0200 Subject: [PATCH] add support to automatic apply application framework support for Spring and Micronaut --- build.gradle.kts | 5 ++ gradle/libs.versions.toml | 4 ++ .../AutoConfigureGradlePlugin.kt | 1 + .../development/DevelopmentExtension.kt | 24 +++++++ .../application/shadow/ShadowExtension.kt | 21 ++++++ .../springboot/SpringBootExtension.kt | 28 ++++++++ .../gradle/api/project/ProjectExt.kt | 12 ++++ .../gradle/autoconfigure/git/GitExtension.kt | 17 +++++ .../java/ApplicationFramework.kt | 7 ++ .../autoconfigure/java/JavaConfigurePlugin.kt | 65 ++++++++++--------- .../java/JavaConfigurePluginExtension.kt | 1 + .../java/PopulateManifestAction.kt | 36 ++++++++++ .../gradle/autoconfigure/util/BuildUtils.kt | 9 +++ .../autoconfigure/util/EnvironmentUtils.kt | 23 +++++++ .../main/java/io/cloudflight/gradle/Foo.java | 4 ++ .../single-kotlin-module-server/build.gradle | 2 +- .../AutoconfigureGradlePluginTest.kt | 5 +- .../java/JavaConfigurePluginTest.kt | 56 +++++++++++++--- .../kotlin/KotlinConfigurePluginTest.kt | 16 +++-- .../report/ReportConfigurePluginTest.kt | 2 +- .../autoconfigure/test/util/ProjectFixture.kt | 9 ++- 21 files changed, 295 insertions(+), 52 deletions(-) create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/development/DevelopmentExtension.kt create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/shadow/ShadowExtension.kt create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/springboot/SpringBootExtension.kt create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/extentions/gradle/api/project/ProjectExt.kt create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/git/GitExtension.kt create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/ApplicationFramework.kt create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/PopulateManifestAction.kt create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/util/BuildUtils.kt create mode 100644 src/main/kotlin/io/cloudflight/gradle/autoconfigure/util/EnvironmentUtils.kt diff --git a/build.gradle.kts b/build.gradle.kts index 0041e42..9e9292e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -38,6 +38,11 @@ dependencies { implementation(libs.kotlin.gradleplugin) implementation(libs.kotlin.noarg) + implementation(libs.git.properties.plugin) + implementation(libs.spring.boot.plugin) + implementation(libs.shadow.plugin) + + testImplementation(libs.bundles.testImplementationDependencies) testRuntimeOnly(libs.junit.engine) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0adf8da..fab8fbd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,6 +13,10 @@ kotlin-noarg = { module = "org.jetbrains.kotlin:kotlin-noarg", version.ref = "ko license-plugin = { module = "io.cloudflight.license.gradle:license-gradle-plugin", version = "1.0.3" } +git-properties-plugin = { module = "com.gorylenko.gradle-git-properties:gradle-git-properties", version = "2.4.1" } +spring-boot-plugin = { module = "org.springframework.boot:spring-boot-gradle-plugin", version = "2.6.7" } +shadow-plugin = { module = "gradle.plugin.com.github.johnrengelman:shadow", version = "7.1.2" } + maven-artifact = { module = "org.apache.maven:maven-artifact", version.ref = "maven-artifact" } junit-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "jupiter" } diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/AutoConfigureGradlePlugin.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/AutoConfigureGradlePlugin.kt index 79dac77..26e9fed 100644 --- a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/AutoConfigureGradlePlugin.kt +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/AutoConfigureGradlePlugin.kt @@ -47,6 +47,7 @@ class AutoConfigureGradlePlugin : Plugin { companion object { const val EXTENSION_NAME = "autoConfigure" + const val TASK_GROUP = "cloudflight" } private fun applyPlugins(project: Project, autoConfigure: AutoConfigureExtension) { diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/development/DevelopmentExtension.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/development/DevelopmentExtension.kt new file mode 100644 index 0000000..a78db84 --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/development/DevelopmentExtension.kt @@ -0,0 +1,24 @@ +package io.cloudflight.gradle.autoconfigure.application.development + +import io.cloudflight.gradle.autoconfigure.AutoConfigureGradlePlugin +import org.gradle.api.Project +import org.gradle.api.plugins.JavaPlugin +import org.gradle.api.tasks.WriteProperties +import org.gradle.language.jvm.tasks.ProcessResources + +object DevelopmentExtension { + + fun create(project: Project) { + val propertiesTask = + project.tasks.create("clfDevelopmentProperties", WriteProperties::class.java) { + it.property("development.name", project.name) + it.property("development.group", project.group.toString()) + it.property("development.version", project.version.toString()) + it.encoding = "UTF-8" + it.group = AutoConfigureGradlePlugin.TASK_GROUP + it.setOutputFile(project.layout.buildDirectory.file("generated/resources/development/development.properties")) + } + project.tasks.named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME, ProcessResources::class.java).get() + .from(propertiesTask) + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/shadow/ShadowExtension.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/shadow/ShadowExtension.kt new file mode 100644 index 0000000..5ad6aa0 --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/shadow/ShadowExtension.kt @@ -0,0 +1,21 @@ +package io.cloudflight.gradle.autoconfigure.application.shadow + +import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin +import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import org.gradle.api.Project +import org.gradle.api.plugins.ApplicationPlugin + +object ShadowExtension { + + fun create(project: Project) { + project.plugins.apply(ShadowPlugin::class.java) + project.plugins.apply(ApplicationPlugin::class.java) + } + + fun configure(project: Project) { + val shadowJar = project.tasks.getByName(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME) as ShadowJar + shadowJar.mergeServiceFiles() + } + +} \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/springboot/SpringBootExtension.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/springboot/SpringBootExtension.kt new file mode 100644 index 0000000..1083f0f --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/application/springboot/SpringBootExtension.kt @@ -0,0 +1,28 @@ +package io.cloudflight.gradle.autoconfigure.application.springboot + +import io.cloudflight.gradle.autoconfigure.java.PopulateManifestAction +import org.gradle.api.Project +import org.gradle.api.plugins.JavaPlugin +import org.springframework.boot.gradle.plugin.SpringBootPlugin +import org.springframework.boot.gradle.tasks.bundling.BootJar + +object SpringBootExtension { + fun create(project: Project) { + project.plugins.apply(SpringBootPlugin::class.java) + } + + fun configure(project: Project) { + val boot = project.tasks.getByName(SpringBootPlugin.BOOT_JAR_TASK_NAME) as BootJar + boot.archiveBaseName.set(project.name) + boot.archiveFileName.set("${project.name}.jar") + boot.doFirst(PopulateManifestAction) + + val springBoot = project.extensions.getByType(org.springframework.boot.gradle.dsl.SpringBootExtension::class.java) + springBoot.buildInfo() + + // we don't need the plain archive, see https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#packaging-executable.and-plain-archives + project.tasks.named(JavaPlugin.JAR_TASK_NAME) { + it.enabled = false + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/extentions/gradle/api/project/ProjectExt.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/extentions/gradle/api/project/ProjectExt.kt new file mode 100644 index 0000000..2fecc73 --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/extentions/gradle/api/project/ProjectExt.kt @@ -0,0 +1,12 @@ +package io.cloudflight.gradle.autoconfigure.extentions.gradle.api.project + +import org.gradle.api.Plugin +import org.gradle.api.Project + +fun Project.withPlugin(plugin: Class>, callable: () -> Any) { + if (plugins.hasPlugin(plugin)) { + callable.invoke() + } else { + plugins.withType(plugin).whenPluginAdded { callable.invoke() } + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/git/GitExtension.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/git/GitExtension.kt new file mode 100644 index 0000000..758f582 --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/git/GitExtension.kt @@ -0,0 +1,17 @@ +package io.cloudflight.gradle.autoconfigure.git + +import com.gorylenko.GitPropertiesPlugin +import com.gorylenko.GitPropertiesPluginExtension +import org.gradle.api.Project + +object GitExtension { + + fun create(project:Project) { + project.plugins.apply(GitPropertiesPlugin::class.java) + } + + fun configure(project:Project) { + val gitProperties = project.extensions.getByType(GitPropertiesPluginExtension::class.java) + gitProperties.customProperty("gradle.version", project.gradle.gradleVersion) + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/ApplicationFramework.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/ApplicationFramework.kt new file mode 100644 index 0000000..2ed9c19 --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/ApplicationFramework.kt @@ -0,0 +1,7 @@ +package io.cloudflight.gradle.autoconfigure.java + +enum class ApplicationFramework { + SpringBoot, + Micronaut, + Other +} \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePlugin.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePlugin.kt index 5fe097f..a7ef852 100644 --- a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePlugin.kt +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePlugin.kt @@ -1,25 +1,29 @@ package io.cloudflight.gradle.autoconfigure.java -import io.cloudflight.gradle.autoconfigure.extentions.gradle.api.java.archives.attributes +import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin +import com.gorylenko.GitPropertiesPlugin +import io.cloudflight.gradle.autoconfigure.application.development.DevelopmentExtension +import io.cloudflight.gradle.autoconfigure.application.shadow.ShadowExtension +import io.cloudflight.gradle.autoconfigure.application.springboot.SpringBootExtension import io.cloudflight.gradle.autoconfigure.extentions.gradle.api.plugins.apply import io.cloudflight.gradle.autoconfigure.extentions.gradle.api.plugins.create import io.cloudflight.gradle.autoconfigure.extentions.gradle.api.plugins.getByType +import io.cloudflight.gradle.autoconfigure.extentions.gradle.api.project.withPlugin import io.cloudflight.gradle.autoconfigure.extentions.gradle.api.tasks.named -import org.gradle.api.Action +import io.cloudflight.gradle.autoconfigure.git.GitExtension +import io.cloudflight.gradle.autoconfigure.util.BuildUtils import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.Task import org.gradle.api.plugins.JavaLibraryPlugin import org.gradle.api.plugins.JavaPlugin import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.tasks.compile.JavaCompile import org.gradle.api.tasks.testing.Test import org.gradle.jvm.tasks.Jar -import org.gradle.jvm.toolchain.JavaToolchainService import org.gradle.testing.jacoco.plugins.JacocoPlugin -import org.slf4j.LoggerFactory +import org.springframework.boot.gradle.plugin.SpringBootPlugin + -private const val GRADLE_VERSION = "Gradle-Version" class JavaConfigurePlugin : Plugin { override fun apply(project: Project) { @@ -34,6 +38,7 @@ class JavaConfigurePlugin : Plugin { encoding.convention(JAVA_ENCODING) vendorName.convention(VENDOR_NAME) applicationBuild.convention(false) + applicationFramework.convention(ApplicationFramework.SpringBoot) } val javaPluginExtension = extensions.getByType(JavaPluginExtension::class) @@ -52,6 +57,21 @@ class JavaConfigurePlugin : Plugin { if (!javaConfigureExtension.applicationBuild.get()) { javaPluginExtension.withSourcesJar() + } else { + when (javaConfigureExtension.applicationFramework.get()) { + ApplicationFramework.SpringBoot -> { + if (BuildUtils.isIntegrationBuild()) { + GitExtension.create(project) + SpringBootExtension.create(project) + } else { + DevelopmentExtension.create(project) + } + } + ApplicationFramework.Micronaut -> { + ShadowExtension.create(project) + } + else -> {} + } } val compileJava = tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile::class) @@ -64,35 +84,20 @@ class JavaConfigurePlugin : Plugin { it.options.encoding = javaConfigureExtension.encoding.get() } } - } - - private object PopulateManifestAction : Action { - override fun execute(t: Task) { - val jar = t as Jar - val project = t.project - val javaConfigureExtension = project.extensions.getByType(JavaConfigurePluginExtension::class) - val javaPluginExtension = project.extensions.getByType(JavaPluginExtension::class) - val configuration = - project.configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME) - val classpath = configuration.files.joinToString(" ") { it.name } - val javaToolChains = project.extensions.getByType(JavaToolchainService::class.java) - val compiler = javaToolChains.compilerFor(javaPluginExtension.toolchain).get().metadata - val createdBy = compiler.javaRuntimeVersion + " (" + compiler.vendor + ")" - jar.manifest.attributes( - "Class-Path" to classpath, - "Created-By" to createdBy, - "Implementation-Vendor" to javaConfigureExtension.vendorName.get(), - "Implementation-Title" to project.name, - "Implementation-Version" to project.version, - GRADLE_VERSION to project.gradle.gradleVersion - ) + // TODO pull that out of the JavaConfigurePlugin? + project.withPlugin(SpringBootPlugin::class.java) { + SpringBootExtension.configure(project) + } + project.withPlugin(ShadowPlugin::class.java) { + ShadowExtension.configure(project) + } + project.withPlugin(GitPropertiesPlugin::class.java) { + GitExtension.configure(project) } } companion object { - private val LOG = LoggerFactory.getLogger(JavaConfigurePlugin::class.java) - const val EXTENSION_NAME = "javaConfigure" } } \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePluginExtension.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePluginExtension.kt index 161aecc..7f68ca3 100644 --- a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePluginExtension.kt +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePluginExtension.kt @@ -6,6 +6,7 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion abstract class JavaConfigurePluginExtension { abstract val applicationBuild: Property + abstract val applicationFramework: Property abstract val languageVersion: Property abstract val encoding: Property abstract val vendorName: Property diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/PopulateManifestAction.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/PopulateManifestAction.kt new file mode 100644 index 0000000..e38d0f2 --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/java/PopulateManifestAction.kt @@ -0,0 +1,36 @@ +package io.cloudflight.gradle.autoconfigure.java + +import io.cloudflight.gradle.autoconfigure.extentions.gradle.api.java.archives.attributes +import io.cloudflight.gradle.autoconfigure.extentions.gradle.api.plugins.getByType +import org.gradle.api.Action +import org.gradle.api.Task +import org.gradle.api.plugins.JavaPlugin +import org.gradle.api.plugins.JavaPluginExtension +import org.gradle.jvm.tasks.Jar +import org.gradle.jvm.toolchain.JavaToolchainService + +internal object PopulateManifestAction : Action { + override fun execute(t: Task) { + val jar = t as Jar + val project = t.project + val javaConfigureExtension = project.extensions.getByType(JavaConfigurePluginExtension::class) + val javaPluginExtension = project.extensions.getByType(JavaPluginExtension::class) + val configuration = + project.configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME) + val classpath = configuration.files.joinToString(" ") { it.name } + val javaToolChains = project.extensions.getByType(JavaToolchainService::class.java) + val compiler = javaToolChains.compilerFor(javaPluginExtension.toolchain).get().metadata + val createdBy = compiler.javaRuntimeVersion + " (" + compiler.vendor + ")" + + jar.manifest.attributes( + "Class-Path" to classpath, + "Created-By" to createdBy, + "Implementation-Vendor" to javaConfigureExtension.vendorName.get(), + "Implementation-Title" to project.name, + "Implementation-Version" to project.version, + GRADLE_VERSION to project.gradle.gradleVersion + ) + } + + private const val GRADLE_VERSION = "Gradle-Version" +} \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/util/BuildUtils.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/util/BuildUtils.kt new file mode 100644 index 0000000..c3be047 --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/util/BuildUtils.kt @@ -0,0 +1,9 @@ +package io.cloudflight.gradle.autoconfigure.util + +object BuildUtils { + + fun isIntegrationBuild(): Boolean { + return EnvironmentUtils.isDefaultBuild() || EnvironmentUtils.isVerifyBuild() || EnvironmentUtils.isPublishBuild() + } + +} \ No newline at end of file diff --git a/src/main/kotlin/io/cloudflight/gradle/autoconfigure/util/EnvironmentUtils.kt b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/util/EnvironmentUtils.kt new file mode 100644 index 0000000..f64fd0a --- /dev/null +++ b/src/main/kotlin/io/cloudflight/gradle/autoconfigure/util/EnvironmentUtils.kt @@ -0,0 +1,23 @@ +package io.cloudflight.gradle.autoconfigure.util + +object EnvironmentUtils { + fun isDefaultBuild(): Boolean { + return getBoolean(ENV_DEFAULT_BUILD) + } + + fun isVerifyBuild(): Boolean { + return getBoolean(ENV_VERIFY_BUILD) + } + + fun isPublishBuild(): Boolean { + return getBoolean(ENV_PUBLISH_BUILD) + } + + private fun getBoolean(name: String): Boolean { + return System.getenv(name).toBoolean() + } + + const val ENV_DEFAULT_BUILD = "DEFAULT_BUILD" + const val ENV_VERIFY_BUILD = "VERIFY_BUILD" + const val ENV_PUBLISH_BUILD = "PUBLISH_BUILD" +} \ No newline at end of file diff --git a/src/test/fixtures/java/single-java-module-application/src/main/java/io/cloudflight/gradle/Foo.java b/src/test/fixtures/java/single-java-module-application/src/main/java/io/cloudflight/gradle/Foo.java index 9351e19..2b4335d 100644 --- a/src/test/fixtures/java/single-java-module-application/src/main/java/io/cloudflight/gradle/Foo.java +++ b/src/test/fixtures/java/single-java-module-application/src/main/java/io/cloudflight/gradle/Foo.java @@ -1,4 +1,8 @@ package io.cloudflight.gradle; public class Foo { + + public static void main(String[] args) { + + } } \ No newline at end of file diff --git a/src/test/fixtures/kotlin/single-kotlin-module-server/build.gradle b/src/test/fixtures/kotlin/single-kotlin-module-server/build.gradle index b9f6ca0..c5c6e98 100644 --- a/src/test/fixtures/kotlin/single-kotlin-module-server/build.gradle +++ b/src/test/fixtures/kotlin/single-kotlin-module-server/build.gradle @@ -1,6 +1,5 @@ plugins { id "io.cloudflight.autoconfigure.kotlin-configure" - id 'org.springframework.boot' version '2.6.7' } repositories { @@ -15,6 +14,7 @@ javaConfigure { languageVersion = JavaLanguageVersion.of(8) encoding = "UTF-8" vendorName = "Cloudflight XYZ" + applicationBuild = true } dependencies { diff --git a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/AutoconfigureGradlePluginTest.kt b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/AutoconfigureGradlePluginTest.kt index 0b3bd46..76e2b6c 100644 --- a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/AutoconfigureGradlePluginTest.kt +++ b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/AutoconfigureGradlePluginTest.kt @@ -107,6 +107,7 @@ private val AUTOCONFIGURE_FIXTURE_PATH = Paths.get("autoconfigure") private fun autoconfigureFixture( fixtureName: String, gradleVersion: String? = null, - testWork: ProjectFixture.() -> T + environment: Map = emptyMap(), + testWork: ProjectFixture .() -> T ): T = - useFixture(AUTOCONFIGURE_FIXTURE_PATH, fixtureName, gradleVersion, testWork) + useFixture(AUTOCONFIGURE_FIXTURE_PATH, fixtureName, gradleVersion, environment, testWork) diff --git a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePluginTest.kt b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePluginTest.kt index 2f61dca..4baa7b3 100644 --- a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePluginTest.kt +++ b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/java/JavaConfigurePluginTest.kt @@ -3,11 +3,13 @@ package io.cloudflight.gradle.autoconfigure.java import io.cloudflight.gradle.autoconfigure.test.util.ProjectFixture import io.cloudflight.gradle.autoconfigure.test.util.normalizedOutput import io.cloudflight.gradle.autoconfigure.test.util.useFixture +import io.cloudflight.gradle.autoconfigure.util.EnvironmentUtils import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.Arguments.arguments import org.junit.jupiter.params.provider.MethodSource +import java.nio.file.Path import java.nio.file.Paths import java.util.jar.Attributes.Name import java.util.jar.Manifest @@ -19,12 +21,15 @@ data class TestOptions( val languageVersion: Int, val encoding: String, val createsSourceJar: Boolean, + val hasVersionSuffixOnJar: Boolean = true, val implementationVendor: String, val inferModulePath: Boolean, val successfulTestCount: Int? = null, val gradleVersion: String? = null, + val environment: Map = emptyMap(), val checkConfigurationInTestOutput: Boolean = true, - val classpath: String = "" + val classpath: String = "", + val additionalChecks: ((fixtureDir: Path) -> (Unit))? = null ) class JavaConfigurePluginTest { @@ -41,7 +46,7 @@ class JavaConfigurePluginTest { @MethodSource("singleJavaModuleArguments") fun `the supplied options are used to configure the JavaPlugin`( options: TestOptions - ): Unit = javaFixture(options.fixtureName, options.gradleVersion) { + ): Unit = javaFixture(options.fixtureName, options.gradleVersion, options.environment) { val result = runCleanBuild() if (options.checkConfigurationInTestOutput) { assertThat(result.normalizedOutput).contains( @@ -63,23 +68,27 @@ class JavaConfigurePluginTest { } val outJarDirPath = fixtureDir.resolve("build/libs") - val outJarLibPath = outJarDirPath.resolve("$fixtureName-1.0.0.jar") - assertThat(outJarLibPath).exists().isRegularFile() + val versionSuffix = if (options.hasVersionSuffixOnJar) "-1.0.0" else "" + val outJarLibPath = outJarDirPath.resolve("$fixtureName$versionSuffix.jar") + assertThat(outJarLibPath).exists().isRegularFile - val outJarSourcesPath = outJarDirPath.resolve("$fixtureName-1.0.0-sources.jar") + val outJarSourcesPath = outJarDirPath.resolve("$fixtureName$versionSuffix-sources.jar") if (options.createsSourceJar) { - assertThat(outJarSourcesPath).exists().isRegularFile() + assertThat(outJarSourcesPath).exists().isRegularFile } else { assertThat(outJarSourcesPath).doesNotExist() } - val manifestPath = fixtureDir.resolve("build/tmp/jar/MANIFEST.MF") + val manifestPath = + if (options.hasVersionSuffixOnJar) fixtureDir.resolve("build/tmp/jar/MANIFEST.MF") else fixtureDir.resolve("build/tmp/bootJar/MANIFEST.MF") val manifest = Manifest(manifestPath.inputStream()).mainAttributes assertThat(manifest) .containsEntry(Name.CLASS_PATH, options.classpath) .containsEntry(Name.IMPLEMENTATION_VENDOR, options.implementationVendor) .containsEntry(Name.IMPLEMENTATION_TITLE, fixtureName) .containsEntry(Name.IMPLEMENTATION_VERSION, "1.0.0") + + options.additionalChecks?.invoke(fixtureDir) } companion object { @@ -147,8 +156,30 @@ class JavaConfigurePluginTest { languageVersion = 11, encoding = "UTF-8", createsSourceJar = false, + hasVersionSuffixOnJar = false, implementationVendor = "Cloudflight Test Vendor", - inferModulePath = true + inferModulePath = true, + environment = mapOf(EnvironmentUtils.ENV_DEFAULT_BUILD to true.toString()), + additionalChecks = { fixtureDir -> + val developmentProperties = + fixtureDir.resolve("build/resources/main/development.properties") + assertThat(developmentProperties).doesNotExist() + } + ) + ), + arguments( + TestOptions( + fixtureName = "single-java-module-application", + languageVersion = 11, + encoding = "UTF-8", + createsSourceJar = false, + implementationVendor = "Cloudflight Test Vendor", + inferModulePath = true, + additionalChecks = { fixtureDir -> + val developmentProperties = + fixtureDir.resolve("build/resources/main/development.properties") + assertThat(developmentProperties).exists() + } ) ), arguments( @@ -190,5 +221,10 @@ class JavaConfigurePluginTest { } private val JAVA_FIXTURE_PATH = Paths.get("java") -private fun javaFixture(fixtureName: String, gradleVersion: String?, testWork: ProjectFixture.() -> T): T = - useFixture(JAVA_FIXTURE_PATH, fixtureName, gradleVersion, testWork) \ No newline at end of file +private fun javaFixture( + fixtureName: String, + gradleVersion: String?, + environment: Map, + testWork: ProjectFixture.() -> T +): T = + useFixture(JAVA_FIXTURE_PATH, fixtureName, gradleVersion, environment, testWork) \ No newline at end of file diff --git a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/kotlin/KotlinConfigurePluginTest.kt b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/kotlin/KotlinConfigurePluginTest.kt index b00490b..b01cde1 100644 --- a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/kotlin/KotlinConfigurePluginTest.kt +++ b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/kotlin/KotlinConfigurePluginTest.kt @@ -22,6 +22,7 @@ data class TestOptions( val encoding: String, val testPlatformMessage: String, val createsSourceJar: Boolean, + val hasVersionSuffixOnJar: Boolean = true, val implementationVendor: String, val inferModulePath: Boolean, val gradleVersion: String? = null, @@ -39,12 +40,15 @@ class KotlinConfigurePluginTest { val result = run("clean", "build", "dependencies", "--configuration", "runtimeClasspath") val outJarDirPath = fixtureDir.resolve("build/libs") - val outJarLibPath = outJarDirPath.resolve("$fixtureName-1.0.0.jar") - assertThat(outJarLibPath).exists().isRegularFile() + val versionSuffix = if (options.hasVersionSuffixOnJar) "-1.0.0" else "" + val outJarLibPath = outJarDirPath.resolve("$fixtureName$versionSuffix.jar") + assertThat(outJarLibPath).exists().isRegularFile - val outJarSourcesPath = outJarDirPath.resolve("$fixtureName-1.0.0-sources.jar") + assertThat(outJarLibPath).exists().isRegularFile + + val outJarSourcesPath = outJarDirPath.resolve("$fixtureName$versionSuffix-sources.jar") if (options.createsSourceJar) { - assertThat(outJarSourcesPath).exists().isRegularFile() + assertThat(outJarSourcesPath).exists().isRegularFile } else { assertThat(outJarSourcesPath).doesNotExist() } @@ -116,7 +120,7 @@ class KotlinConfigurePluginTest { languageVersion = 8, encoding = "UTF-8", testPlatformMessage = "Enabled Junit5 as test platform", - createsSourceJar = true, + createsSourceJar = false, implementationVendor = "Cloudflight XYZ", inferModulePath = true, kotlinVersion = currentKotlinVersion @@ -146,4 +150,4 @@ private fun BuildResult.extractJavaToolchainJdkHome(): String { private val KOTLIN_FIXTURE_PATH = Paths.get("kotlin") private fun javaFixture(fixtureName: String, gradleVersion: String?, testWork: ProjectFixture.() -> T): T = - useFixture(KOTLIN_FIXTURE_PATH, fixtureName, gradleVersion, testWork) \ No newline at end of file + useFixture(KOTLIN_FIXTURE_PATH, fixtureName, gradleVersion, emptyMap(), testWork) \ No newline at end of file diff --git a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/report/ReportConfigurePluginTest.kt b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/report/ReportConfigurePluginTest.kt index 0150bbe..f7161de 100644 --- a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/report/ReportConfigurePluginTest.kt +++ b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/report/ReportConfigurePluginTest.kt @@ -55,4 +55,4 @@ class ReportConfigurePluginTest { private val REPORT_FIXTURE_PATH = Paths.get("report") private fun javaFixture(fixtureName: String, gradleVersion: String?, testWork: ProjectFixture.() -> T): T = - useFixture(REPORT_FIXTURE_PATH, fixtureName, gradleVersion, testWork) \ No newline at end of file + useFixture(REPORT_FIXTURE_PATH, fixtureName, gradleVersion, emptyMap(), testWork) \ No newline at end of file diff --git a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/test/util/ProjectFixture.kt b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/test/util/ProjectFixture.kt index 41847ed..7b48fdd 100644 --- a/src/test/kotlin/io/cloudflight/gradle/autoconfigure/test/util/ProjectFixture.kt +++ b/src/test/kotlin/io/cloudflight/gradle/autoconfigure/test/util/ProjectFixture.kt @@ -8,7 +8,7 @@ import java.nio.file.Paths private val FIXTURES_BASE_DIR = Paths.get("src", "test", "fixtures") -internal class ProjectFixture(fixtureBaseDir: Path, val fixtureName: String, val gradleVersion: String? = null) { +internal class ProjectFixture(fixtureBaseDir: Path, val fixtureName: String, val gradleVersion: String? = null, val environment:Map? = null) { val fixtureDir: Path = FIXTURES_BASE_DIR.resolve(fixtureBaseDir).resolve(fixtureName) @@ -27,9 +27,13 @@ internal class ProjectFixture(fixtureBaseDir: Path, val fixtureName: String, val } fun createRunner(first: String, vararg tasks: String): GradleRunner { + val sysEnv = mutableMapOf() + sysEnv.putAll(System.getenv()) + environment?.let { sysEnv.putAll(it) } var runner = GradleRunner.create() .withProjectDir(fixtureDir.toFile()) .withPluginClasspath() + .withEnvironment(sysEnv) .withArguments(first, *tasks) if (gradleVersion != null) { @@ -44,8 +48,9 @@ internal fun useFixture( fixtureBaseDir: Path, fixtureName: String, gradleVersion: String?, + environment: Map? = emptyMap(), testWork: ProjectFixture.() -> T ): T { - val fixture = ProjectFixture(fixtureBaseDir, fixtureName, gradleVersion) + val fixture = ProjectFixture(fixtureBaseDir, fixtureName, gradleVersion, environment) return fixture.testWork() }