Skip to content

Commit

Permalink
Rework gradle-build setup
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Jul 14, 2024
1 parent 4f8e814 commit c16c16a
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 258 deletions.
24 changes: 24 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Normalize as LF in the repository, OS native locally
* text=auto

*.bat text eol=crlf
gradlew text eol=lf
*.sh text eol=lf
*.conf text eol=lf

*.java text
*.java diff=java

# Binary files that should not be modified
*.dat binary
*.db binary
*.icns binary
*.ico binary
*.jar binary
*.jks binary
*.jpg binary
*.key binary
*.png binary
*.ttf binary
*.wav binary
JavaApplicationStub binary
133 changes: 6 additions & 127 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,134 +1,13 @@
import java.io.IOException
import java.util.concurrent.TimeoutException

plugins {
java
`java-library`
`maven-publish`
id("com.diffplug.spotless") version "6.1.2"
}

fun String.runCommand(): String = ProcessBuilder(split("\\s(?=(?:[^'\"`]*(['\"`])[^'\"`]*\\1)*[^'\"`]*$)".toRegex()))
.directory(projectDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.apply {
if (!waitFor(10, TimeUnit.SECONDS)) {
throw TimeoutException("Failed to execute command: '" + this@runCommand + "'")
}
}
.run {
val error = errorStream.bufferedReader().readText().trim()
if (error.isNotEmpty()) {
throw IOException(error)
}
inputStream.bufferedReader().readText().trim()
}

val gitHash = "git rev-parse --verify HEAD".runCommand()
val clean = "git status --porcelain".runCommand().isEmpty()
val lastTag = "git describe --tags --abbrev=0".runCommand()
val lastVersion = lastTag.substring(1) // remove the leading 'v'
val commits = "git rev-list --count $lastTag..HEAD".runCommand()
println("Git hash: $gitHash" + if (clean) "" else " (dirty)")

group = "de.bluecolored.bluemap"
version = lastVersion +
(if (commits == "0") "" else "-$commits") +
(if (clean) "" else "-dirty")

println("Version: $version")

val javaTarget = 16
java {
sourceCompatibility = JavaVersion.toVersion(javaTarget)
targetCompatibility = JavaVersion.toVersion(javaTarget)

withSourcesJar()
withJavadocJar()
}

repositories {
mavenCentral()
bluemap.base
}

dependencies {
api ("com.flowpowered:flow-math:1.0.3")
api ("com.google.code.gson:gson:2.8.0")

compileOnly ("org.jetbrains:annotations:23.0.0")
}

spotless {
java {
target ("src/*/java/**/*.java")

licenseHeaderFile("LICENSE_HEADER")
indentWithSpaces()
trimTrailingWhitespace()
}
}

tasks.withType(JavaCompile::class).configureEach {
options.apply {
encoding = "utf-8"
}
}

tasks.withType(AbstractArchiveTask::class).configureEach {
isReproducibleFileOrder = true
isPreserveFileTimestamps = false
}

tasks.javadoc {
options {
(this as? StandardJavadocDocletOptions)?.apply {
links(
"https://docs.oracle.com/en/java/javase/16/docs/api/",
"https://javadoc.io/doc/com.flowpowered/flow-math/1.0.3/",
"https://javadoc.io/doc/com.google.code.gson/gson/2.8.0/",
)
addStringOption("Xdoclint:none", "-quiet")
addBooleanOption("html5", true)
}
}
}

tasks.processResources {
from("src/main/resources") {
include("de/bluecolored/bluemap/api/version.json")
duplicatesStrategy = DuplicatesStrategy.INCLUDE

expand (
"version" to project.version,
"gitHash" to gitHash + if (clean) "" else " (dirty)"
)
}
}

publishing {
repositories {
maven {
name = "bluecolored"

val releasesRepoUrl = "https://repo.bluecolored.de/releases"
val snapshotsRepoUrl = "https://repo.bluecolored.de/snapshots"
url = uri(if (version == lastVersion) releasesRepoUrl else snapshotsRepoUrl)
api ( libs.flow.math )
api ( libs.gson )

credentials {
username = project.findProperty("bluecoloredUsername") as String? ?: System.getenv("BLUECOLORED_USERNAME")
password = project.findProperty("bluecoloredPassword") as String? ?: System.getenv("BLUECOLORED_PASSWORD")
}
}
}
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()
compileOnly ( libs.jetbrains.annotations )
compileOnly ( libs.lombok )

from(components["java"])
}
}
annotationProcessor ( libs.lombok )
}
17 changes: 17 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
fun plugin(dependency: Provider<PluginDependency>) = dependency.map {
"${it.pluginId}:${it.pluginId}.gradle.plugin:${it.version}"
}

implementation ( plugin( libs.plugins.spotless ) )
implementation ( plugin( libs.plugins.shadow ) )
}
9 changes: 9 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

// use version-catalog from root project
dependencyResolutionManagement {
versionCatalogs {
register("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}
63 changes: 63 additions & 0 deletions buildSrc/src/main/kotlin/bluemap.base.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
plugins {
java
`java-library`
id ( "com.diffplug.spotless" )
}

group = "de.bluecolored.bluemap"
version = gitVersion()

repositories {
maven ("https://repo.bluecolored.de/releases") {
content { includeGroupByRegex ("de\\.bluecolored\\..*") }
}
maven ("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") {
content { includeGroup ("org.spigotmc") }
}

mavenCentral()
maven ("https://libraries.minecraft.net")
maven ( "https://maven.minecraftforge.net" )
maven ("https://repo.papermc.io/repository/maven-public/")
}

tasks.withType(JavaCompile::class).configureEach {
options.encoding = "utf-8"
}

tasks.withType(AbstractArchiveTask::class).configureEach {
isReproducibleFileOrder = true
isPreserveFileTimestamps = false
}

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(16))
withSourcesJar()
withJavadocJar()
}

tasks.javadoc {
(options as StandardJavadocDocletOptions).apply {
links(
"https://docs.oracle.com/en/java/javase/16/docs/api/",
"https://javadoc.io/doc/com.flowpowered/flow-math/1.0.3/",
"https://javadoc.io/doc/com.google.code.gson/gson/2.8.9/",
)
addStringOption("Xdoclint:none", "-quiet")
addBooleanOption("html5", true)
}
}

tasks.test {
useJUnitPlatform()
}

spotless {
java {
target ("src/*/java/**/*.java")

licenseHeaderFile(rootProject.file("LICENSE_HEADER"))
indentWithSpaces()
trimTrailingWhitespace()
}
}
43 changes: 43 additions & 0 deletions buildSrc/src/main/kotlin/versioning.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import org.gradle.api.Project
import java.io.IOException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

fun Project.gitHash(): String {
return runCommand("git rev-parse --verify HEAD", "-")
}

fun Project.gitClean(): Boolean {
return runCommand("git status --porcelain", "NOT_CLEAN").isEmpty()
}

fun Project.gitVersion(): String {
val lastTag = if (runCommand("git tag", "").isEmpty()) "" else runCommand("git describe --tags --abbrev=0", "")
val lastVersion = if (lastTag.isEmpty()) "0.0" else lastTag.substring(1) // remove the leading 'v'
val commits = runCommand("git rev-list --count $lastTag..HEAD", "0")
val gitVersion = lastVersion +
(if (commits == "0") "" else "-$commits") +
(if (gitClean()) "" else "-dirty")

logger.lifecycle("${project.name} version: $gitVersion")

return gitVersion
}

private fun Project.runCommand(cmd: String, fallback: String? = null): String {
ProcessBuilder(cmd.split("\\s(?=(?:[^'\"`]*(['\"`])[^'\"`]*\\1)*[^'\"`]*$)".toRegex()))
.directory(projectDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.apply {
if (!waitFor(10, TimeUnit.SECONDS))
throw TimeoutException("Failed to execute command: '$cmd'")
}
.run {
val error = errorStream.bufferedReader().readText().trim()
if (error.isEmpty()) inputStream.bufferedReader().readText().trim()
if (fallback != null) return fallback
throw IOException(error)
}
}
2 changes: 0 additions & 2 deletions gradle.properties

This file was deleted.

14 changes: 14 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[versions]
junit = "5.8.2"

[libraries]
flow-math = { module = "com.flowpowered:flow-math", version = "1.0.3" }
gson = { module = "com.google.code.gson:gson", version = "2.8.9" }
jetbrains-annotations = { module = "org.jetbrains:annotations", version = "23.0.0" }
junit-core = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
junit-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
lombok = { module = "org.projectlombok:lombok", version = "1.18.32" }

[plugins]
shadow = { id = "io.github.goooler.shadow", version = "8.+" }
spotless = { id = "com.diffplug.spotless", version = "6.+" }
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit c16c16a

Please sign in to comment.