Unresolved reference: grgit - kotlin

This is my world server land plug-in, and I don't know much about gradle and kotlin.(Know some front-end, but every line is like a mountain)
I encountered a problem when compiling. It seems that I lack something called grgit.
How to solve this problem?
Or you can try to compile this project. If you succeed, don't forget to tell me your method. Thank you very much!
import java.time.format.DateTimeFormatter
dependencies {
// Expected everywhere.
compileOnlyApi(libs.checkerqual)
// Minecraft expectations
compileOnlyApi(libs.guava)
compileOnlyApi(libs.gson)
// Platform expectations
compileOnlyApi(libs.snakeyaml)
// Adventure
api(libs.adventure)
api(libs.minimessage)
// Guice
api(libs.guice) {
exclude(group = "com.google.guava")
}
api(libs.guiceassistedinject) {
exclude("com.google.inject", "guice")
}
compileOnlyApi(libs.findbugs)
// Plugins
compileOnlyApi(libs.worldeditCore) {
exclude(group = "bukkit-classloader-check")
exclude(group = "mockito-core")
exclude(group = "dummypermscompat")
}
testImplementation(libs.worldeditCore)
compileOnlyApi(libs.fastasyncworldeditCore) { isTransitive = false }
testImplementation(libs.fastasyncworldeditCore) { isTransitive = false }
// Logging
compileOnlyApi(libs.log4j)
// Other libraries
api(libs.prtree)
api(libs.aopalliance)
api(libs.pipeline) {
exclude(group = "com.google.guava")
}
api(libs.arkitektonika)
api(libs.paster)
}
tasks.processResources {
filesMatching("plugin.properties") {
expand(
"version" to project.version.toString(),
"commit" to rootProject.grgit.head().abbreviatedId, // The error points here
"date" to rootProject.grgit.head().dateTime.format(DateTimeFormatter.ofPattern("yy.MM.dd")) // The error points here
)
}
}
Problems arising
Address of this project: https://github.com/IntellectualSites/PlotSquared
Thanks again!

First, you need to declare the grgit variable before using it, that's why you're getting the error.
Add plugin for grgit in the plugins{} block:
id("org.ajoberstar.grgit") version "4.0.2"
Import Grgit like this:
import org.ajoberstar.grgit.Grgit
Then, declare the variable grgit like this:
val grgit = Grgit.open(mapOf("currentDir" to project.rootDir))

Related

Specific gradle options for target build platform

I have a Kotlin project (not Android) that uses the LWJGL library. Under macOS, I need to add the following options to build.gradle:
project.ext.lwjglNatives = "natives-macos"
applicationDefaultJvmArgs = ["-XstartOnFirstThread"]
dependencies {
implementation platform('org.jetbrains.kotlin:kotlin-bom')
implementation platform("org.lwjgl:lwjgl-bom:3.2.3")
implementation "org.lwjgl:lwjgl"
implementation "org.lwjgl:lwjgl-openal"
runtimeOnly "org.lwjgl:lwjgl::$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-openal::$lwjglNatives"
}
On Windows, however, I need to drop applicationDefaultJvmArgs, and set lwjglNatives to:
project.ext.lwjglNatives = "natives-windows"
How can I tell gradle to do this? Basically I need some kind of target platform check.
Moreover, I need to know the target platform in Kotlin was well. How can I tell the build platform from Kotlin code?
I figured it out. The operating system can be tested like this:
import org.apache.tools.ant.taskdefs.condition.Os
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
project.ext.lwjglNatives = "natives-windows"
} else {
project.ext.lwjglNatives = "natives-macos"
applicationDefaultJvmArgs = ["-XstartOnFirstThread"]
}
To access this from Kotlin, a BuildConfig.java needs to be generated ad hoc:
tasks.register('generateSources') {
ext.outputDir = "$buildDir/generated/java"
outputs.dir outputDir
doFirst {
mkdir "$outputDir/ch/digorydoo/ksoundrender"
file("$outputDir/ch/digorydoo/ksoundrender/BuildConfig.java").text =
"""|package ch.digorydoo.ksoundrender;
|public class BuildConfig {
| public static Boolean isWindows() {
| return ${if (Os.isFamily(Os.FAMILY_WINDOWS)) "true" else "false"};
|}
|}""".stripMargin()
}
}
compileKotlin.dependsOn generateSources
sourceSets.main.java.srcDir generateSources.outputDir
Now I can just import BuildConfig from Kotlin.

Calling PCEnhancerTask from Kotlin in Gradle

I need to call the OpenJPA PCEnhancerTask class from Kotlin instead of Groovy. The following code works just fine (based on a previous solution documented here):
def openJPAClosure = {
def entityFiles = sourceSets.main.output.classesDirs.asFileTree.matching {
include 'com/company/persist/*Entity.class'
}
println "Enhancing with OpenJPA:"
entityFiles.getFiles().each {
println it
}
ant.taskdef(
name : 'openjpac',
classpath : sourceSets.main.runtimeClasspath.asPath,
classname : 'org.apache.openjpa.ant.PCEnhancerTask'
)
ant.openjpac(
classpath: sourceSets.main.runtimeClasspath.asPath,
addDefaultConstructor: false,
enforcePropertyRestrictions: true) {
entityFiles.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet)
}
}
I was looking at the documentation on how to call Ant tasks from Gradle but I could not translate all the necessary steps using the GroovyBuilder. So instead I tough of calling the PCEnhancer directly:
fun openJPAEnrich() {
val entityFiles = sourceSets.main.get().output.classesDirs.asFileTree.matching {
include("com/company/persist/*Entity.class")
}
println("Enhancing with OpenJPA, the following files...")
entityFiles.getFiles().forEach() {
println(it)
}
org.apache.openjpa.ant.PCEnhancerTask.main(asList(entityFiles))
}
But it complains about not being able to find org.apache.openjpa in the classpath (but is it listed as a compilation dependency)
My questions are:
What is the correct way to translate the original Groovy construct to Kotlin using groovyBuilder
If is not possible, how you can correctly call PCEnhancer from Kotlin in Gradle?
So I ended making it work with a custom JavaExec Gradle task:
tasks.create<JavaExec>("openJPAEnrich") {
val entityFiles = sourceSets.main.get().output.classesDirs.asFileTree.matching {
include("com/company/persist/*Entity.class")
}
println("Enhancing with OpenJPA, the following files...")
entityFiles.files.forEach() {
println(it)
}
classpath = sourceSets.main.get().runtimeClasspath
main = "org.apache.openjpa.enhance.PCEnhancer"
args(listOf("-enforcePropertyRestrictions", "true", "-addDefaultConstructor", "false"))
entityFiles.forEach { classFile -> args?.add(classFile.toString())}
}
I was tempted to build my own custom Gradle task but for this felt overkill.
Thanks.
--Jose

Get a property of task? in Gradle kotlin

I'm trying to convert my build.gradle file to build.gradle.kts.
I almost do that but only one problem left.
I don't have any idea how to convert code below.
Kotlin
import org.asciidoctor.gradle.AsciidoctorTask
...
apply(plugin = "org.asciidoctor.convert")
val snippetsDir = file("build/generated-snippets")
tasks.named<AsciidoctorTask>("asciidoctor") {
attributes(
mapOf(
"snippets" to snippetsDir
)
)
inputs.dir(snippetsDir)
dependsOn("test")
}
tasks.withType<BootJar> {
dependsOn("asciidoctor")
// This is the problem!
// from("${asciidoctor.outputDir}/html5") {
// into("static/docs")
// }
}
Please help me! Thanks :)
See Tasks documentation : you can access asciidoctor tasks using Kotlin delegated properties, and then access its properties like outputDir
tasks.withType<org.springframework.boot.gradle.tasks.bundling.BootJar> {
dependsOn("asciidoctor")
// This was the problem!
val asciidoctor by tasks.getting(AsciidoctorTask::class)
from("${asciidoctor.outputDir}/html5") {
into("static/docs")
}
}

Publishing to bintray with kotlin dsl script

I have been trying to publish my kotlin library and I was following the instructions given at https://github.com/bintray/gradle-bintray-plugin/blob/master/README.md . Fortunately I was able to migrate the most part of it to kotlin. But I can't seem to fix the error it gives for the pkg part. It says
Type mismatch: inferred type is () -> TypeVariable(_L) but
Closure<(raw) Any!>! was expected.
I just can't seem to fix this part, any examples as to how to implement this in kotlin would be great.
You can use the delegateClosureOf<...> for closures in the bintray configurations:
bintray {
...
pkg(delegateClosureOf<BintrayExtension.PackageConfig> {
repo = "maven"
...
version(delegateClosureOf<BintrayExtension.VersionConfig> {
...
})
})
}
Rather than using closure syntax, you can use .apply:
bintray {
...
pkg = PackageConfig().apply {
repo = ""
...
version = VersionConfig().apply {
name = ""
...
}
}
}

RxAlamofire: Ambiguous reference to member 'json(_:_:parameters:encoding:headers:)'

When I try to compile the code below, I get an error:
Ambiguous reference to member 'json(::parameters:encoding:headers:)'
The code was copied and pasted from a RxAlamofire Github repository page
import RxSwift
import RxAlamofire
class CurrencyRest {
static func getJson() {
let stringURL = "https://api.fixer.io/latest"
// MARK: NSURLSession simple and fast
let session = URLSession.init()
_ = session.rx.json(.get, stringURL)
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
}
}
To fix the error, session.rx.json(url:) is the way to go, it's from RxCocoa, although for RxAlamofire, you don't have to use URLSession rx extension, instead, use json(::parameters:encoding:headers:), e.g. json(.get, stringURL), which returns an Observable<Any> that you can use as JSON.