Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework gradle-build system #7

Merged
merged 7 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions .github/workflows/gradle.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Java CI
name: Build

on: [push]

Expand All @@ -14,11 +14,11 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 16
java-version: 21
cache: 'gradle'
- name: Build with Gradle
run: ./gradlew clean build test
- uses: actions/upload-artifact@v2-preview
- uses: actions/upload-artifact@v4
with:
name: artifact
path: build/libs/*
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 16
java-version: 21
cache: 'gradle'
- name: Build with Gradle
run: ./gradlew publish
Expand Down
123 changes: 7 additions & 116 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,131 +1,22 @@
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")
api ( libs.flow.math )
api ( libs.gson )

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

tasks.withType(JavaCompile::class).configureEach {
options.apply {
encoding = "utf-8"
}
}
compileOnly ( libs.jetbrains.annotations )
compileOnly ( libs.lombok )

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)"
)
}
annotationProcessor ( libs.lombok )
}

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)

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
artifactId = "bluemap-${project.name}"
version = project.version.toString()

from(components["java"])
Expand Down
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"))
}
}
}
78 changes: 78 additions & 0 deletions buildSrc/src/main/kotlin/bluemap.base.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
plugins {
java
`java-library`
`maven-publish`
id ( "com.diffplug.spotless" )
}

group = "de.bluecolored"
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 = JavaLanguageVersion.of(21)
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()
}
}

publishing {
repositories {
maven {
name = "bluecolored"
url = uri( "https://repo.bluecolored.de/releases" )

credentials {
username = project.findProperty("bluecoloredUsername") as String? ?: System.getenv("BLUECOLORED_USERNAME")
password = project.findProperty("bluecoloredPassword") as String? ?: System.getenv("BLUECOLORED_PASSWORD")
}
}
}
}
44 changes: 44 additions & 0 deletions buildSrc/src/main/kotlin/versioning.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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()) return inputStream.bufferedReader().readText().trim()
logger.warn("Failed to execute command '$cmd': $error")
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.10.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading