Publishing to bintray with kotlin dsl script - kotlin

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 = ""
...
}
}
}

Related

KotlinJs: Unable to run jsBrowserTest in Google Cloudbuild due to "ReferenceError: setMetadataFor is not defined"

We have a very simple test in kotlinJs to check that a function we have in Javascript is read into kotlin and returns something:
JS code:
function: makeKey = (num) => { return 1000 + num }
Kotlin FFI code:
#JsNonModule
external object MyApp {
fun makeKey(num: Int): Int
}
Test Code:
class TestFFI {
#Test
fun thingsShouldWork() {
println(MyApp.makeKey(5))
assertNotNull(MyApp.makeKey(5))
assertEquals(1005, MyApp.makeKey(5))
}
}
This works locally but when we put this into a google cloud run we get the following failure: ReferenceError: setMetadataFor is not defined
setMetadataFor appears to be a function in the testJs file that is produced by the kotlin compiler and is defined as
var setMetadataFor = kotlin_kotlin.$_$.t9;
We are wondering if the Kotlin code is getting packaged wrong and not including the relevant file
We are using Kotlin 1.8 and Gradle 7.4.

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

Unresolved reference: grgit

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))

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")
}
}

Adds an implementation dependency only to the "free" product flavor in Kotlin DSL

I am in process of migrating our Groovy based scripts to Kotlin. I have been able to get most of done except not sure how to add a dependency for a particular flavour.
This is how looks like in Kotlin DSL so far but not sure why the freeImplementation("bar:2.2.8")
productFlavors {
create("free") {
...
...
}
create("paid") {
...
...
}
}
dependencies {
implementation("foo:1.2.0")
// This is not working when migrated to Kotlin DSL
freeImplementation("bar:2.2.8")
//Below was the code in Groovy which was working fine earlier
//freeImplementation "bar:2.2.8"
}
Below is the solution for it.
val freeImplementation by configurations
dependencies {
freeImplementation("bar:2.2.8")
}
Alternatively, a string literal can be used to denote a dynamic configuration:
dependencies {
"freeImplementation"("bar:2.2.8")
}