Skip to content

Commit

Permalink
add support to automatic apply application framework support for Spri…
Browse files Browse the repository at this point in the history
…ng and Micronaut
  • Loading branch information
klu2 authored May 17, 2022
1 parent 2436c8b commit 84b2d8e
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 52 deletions.
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AutoConfigureGradlePlugin : Plugin<Project> {

companion object {
const val EXTENSION_NAME = "autoConfigure"
const val TASK_GROUP = "cloudflight"
}

private fun applyPlugins(project: Project, autoConfigure: AutoConfigureExtension) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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()
}

}
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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<out Plugin<*>>, callable: () -> Any) {
if (plugins.hasPlugin(plugin)) {
callable.invoke()
} else {
plugins.withType(plugin).whenPluginAdded { callable.invoke() }
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.cloudflight.gradle.autoconfigure.java

enum class ApplicationFramework {
SpringBoot,
Micronaut,
Other
}
Original file line number Diff line number Diff line change
@@ -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<Project> {
override fun apply(project: Project) {
Expand All @@ -34,6 +38,7 @@ class JavaConfigurePlugin : Plugin<Project> {
encoding.convention(JAVA_ENCODING)
vendorName.convention(VENDOR_NAME)
applicationBuild.convention(false)
applicationFramework.convention(ApplicationFramework.SpringBoot)
}

val javaPluginExtension = extensions.getByType(JavaPluginExtension::class)
Expand All @@ -52,6 +57,21 @@ class JavaConfigurePlugin : Plugin<Project> {

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)
Expand All @@ -64,35 +84,20 @@ class JavaConfigurePlugin : Plugin<Project> {
it.options.encoding = javaConfigureExtension.encoding.get()
}
}
}

private object PopulateManifestAction : Action<Task> {
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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion
abstract class JavaConfigurePluginExtension {

abstract val applicationBuild: Property<Boolean>
abstract val applicationFramework: Property<ApplicationFramework>
abstract val languageVersion: Property<JavaLanguageVersion>
abstract val encoding: Property<String>
abstract val vendorName: Property<String>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Task> {
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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.cloudflight.gradle.autoconfigure.util

object BuildUtils {

fun isIntegrationBuild(): Boolean {
return EnvironmentUtils.isDefaultBuild() || EnvironmentUtils.isVerifyBuild() || EnvironmentUtils.isPublishBuild()
}

}
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package io.cloudflight.gradle;

public class Foo {

public static void main(String[] args) {

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
id "io.cloudflight.autoconfigure.kotlin-configure"
id 'org.springframework.boot' version '2.6.7'
}

repositories {
Expand All @@ -15,6 +14,7 @@ javaConfigure {
languageVersion = JavaLanguageVersion.of(8)
encoding = "UTF-8"
vendorName = "Cloudflight XYZ"
applicationBuild = true
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private val AUTOCONFIGURE_FIXTURE_PATH = Paths.get("autoconfigure")
private fun <T : Any> autoconfigureFixture(
fixtureName: String,
gradleVersion: String? = null,
testWork: ProjectFixture.() -> T
environment: Map<String, String> = emptyMap(),
testWork: ProjectFixture .() -> T
): T =
useFixture(AUTOCONFIGURE_FIXTURE_PATH, fixtureName, gradleVersion, testWork)
useFixture(AUTOCONFIGURE_FIXTURE_PATH, fixtureName, gradleVersion, environment, testWork)
Loading

0 comments on commit 84b2d8e

Please sign in to comment.