Why do I need to reference a custom gradle config with square brackets? - kotlin

I created a gradle build config just to download some dependencies. The documentation has been sparse, so I've piece together this working snippet based on random snippets and guesses.
configurations {
create("downloadDeps")
}
dependencies {
// JSON
configurations["downloadDeps"]("com.fasterxml.jackson.core:jackson-databind:2.13.3")
configurations["downloadDeps"]("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3")
}
repositories {
// internal repository
maven {
url = uri("...")
credentials {
username = System.getenv("ARTIFACTORY_USER") ?: System.getProperty("ARTIFACTORY_USER") as String
password = System.getenv("ARTIFACTORY_TOKEN") ?: System.getProperty("ARTIFACTORY_TOKEN") as String
}
}
}
tasks.register<Copy> ("downloadDeps") {
from(configurations["downloadDeps"])
into("lib/")
}
If I reference the "downloadDeps" dependency like configuration.downloadDeps or downloadDeps("com.fasterxml.jackson.core:jackson-databind:2.13.3"). I get an error about an unresolved reference to "downloadDeps".
Why does implementation("...") or configuration.implementation.get() work?

The documentation #Slaw provided helped me understand why I can do something like this:
implementation("group:artifact:1.0.0")
but not
myCustomConfig("group:artifact:1.0.0")
implementation being declared that way is supported because it comes from a plugin (the Kotlin/Java plugins)
The simplest way to associate a dependency with myCustomConfig would be to do this (see these docs):
"myCustomConfig"("group:artifact:1.0.0")

Related

How to add SolanaKT to my project kotlin?

I have an existing kotlin project. Now I'm trying to add the SolanaKT library to it.
I have added the JitPack repository maven { url 'https://jitpack.io' } and dependency implementation 'com.github.metaplex-foundation:SolanaKT:2.0.1'.
Then I did a gradle sync.
I try using:
val endPoint = RPCEndpoint.devnetSolana
val network = HttpNetworkingRouter(endPoint)
val solana = Solana(network)
But i get error Unresolved reference: RPCEndpoint.
What am I doing wrong? Is there anything else I should do (maybe copy some files to the project) ?

Unable to add duplicate strategy in gradle

Disclaimer: I am new to gradle, so it's likely I'm missing something simple.
Background: I am splitting a service into two services that will live in the same repo. The second service (tester) uses code from the first service (engine), which I have managed to tell gradle. But I have an issue.
Building the tester fails, giving the following error:
Execution failed for task ':engine:processResources'.
Entry application.properties is a duplicate but no duplicate handling strategy has been set
It seems this is a common issue in gradle that someone has already posted about. Unfortunately, pasting each of the responses in my build file has yielded nothing.
I've also tried the kotlin solutions in this thread with similar results: I keep getting the duplicate strategy not set error.
Surely I'm missing something but I can't for the life of me figure out what.
Tester build.gradle.kts Code
plugins {
kotlin("jvm") version "1.5.10"
kotlin("plugin.serialization") version "1.5.30"
id("net.linguica.maven-settings") version "0.5"
}
group = "my.group"
version = "1.0"
repositories {
mavenCentral()
maven(url = "my/url") {
name = "maven-snapshots"
authentication {
create<BasicAuthentication>("basic")
}
}
maven(url = "my/other/url") {
name = "maven-releases"
authentication {
create<BasicAuthentication>("basic")
}
}
}
//just trying all the things
rootProject.tasks.named("processResources", Copy::class.java) {
duplicatesStrategy = DuplicatesStrategy.WARN
}
tasks.withType<ProcessResources>() {
duplicatesStrategy = DuplicatesStrategy.WARN
}
tasks.withType<Copy>() {
duplicatesStrategy = DuplicatesStrategy.WARN
}
tasks.withType<Jar>() {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
dependencies {
implementation(project(":engine"))
}

How to list configured routes in Ktor

I am setting up a project with multiple modules that contain different versions of api.
To verify correct route configuration I would like to print configured routes to application log like it is done in spring framework.
Is it possible and what should I use for that?
You can do it by recursively traversing routes starting from the root all the way down the tree and filtering in only ones with the HTTP method selector. This solution is described here.
fun Application.module() {
// routing configuration goes here
val root = feature(Routing)
val allRoutes = allRoutes(root)
val allRoutesWithMethod = allRoutes.filter { it.selector is HttpMethodRouteSelector }
allRoutesWithMethod.forEach {
println("route: $it")
}
}
fun allRoutes(root: Route): List<Route> {
return listOf(root) + root.children.flatMap { allRoutes(it) }
}
Please note that the code above must be placed after the routing configuration.

How to programmatically parse/retrieve user's parameters in Intellij project from Gradle build file

I need to retrieve some user specific parameters from Gradle build file in an Intellij project (build.gradle.kts)
Here a "build.gradle.kts" file content example I need to parse:
cutomParameters {
param1.set("any value")
sub_parameters1 {
sub_parameter1_1.set("foo")
}
subParameters2 {
subParameter21("foo")
subParameter22 {
subParameter221.set("foo")
}
}
}
I tried some code like this:
val connection: ProjectConnection =
GradleConnector.newConnector().forProjectDirectory(File(projectPath)).connect()
val model = connection.model(GradleBuild::class.java)
I can get the gradle build file using model.get().buildFile but how to retrieve the custom parameters described in the previous example?

Ktor - post unhanldled error with coroutines

I a new to Kotlin and Ktor in particular, so I have tried to do simple post request. As you can see below, there is nothing special.
routing {
post("/articles/add"){
val post = call.receive<ArticleRequest>()
println(post)
}
Error shown in logs is below and I don't understand why I should use here coroutines.
ERROR Application - Unhandled: POST - /articles/add
java.lang.IllegalStateException: Using blocking primitives on this dispatcher is not allowed. Consider using async channel instead or use blocking primitives in withContext(Dispatchers.IO) instead.
I am using 1.4.2 version. I would appreciate any help.
If you are using Jackson this is a bug and there is a suggested workaround:
routing {
post("/articles/add") {
with(Dispatchers.IO) {
val post = call.receive<ArticleRequest>()
println(post)
}
}
}
Or you can rollback to 1.4.1 until the bug is solved.
I've experienced the same issue after upgrading to ktor 1.4.2 and Kotlin 1.4.20, and I used both Moshi and Gson on this specific project but I don't believe they are causing this issue.
If you have a 'gradle.properties' file, add these ( or whatever version you wish to use ) :
ktor_version=1.3.2
kotlin_version=1.3.70.
Otherwise, in your 'build.gradle' file, create variables for different version :
buildscript {
ext.kotlin_version = '1.3.70'
ext.ktor_version = '1.3.2'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Then sync your gradle, run project.. all should be good.
However if you still experience some gradle-related issue, try this :
go to gradle (folder) -> wrapper -> open gradle_wrapper.properties and make sure the url has version 6.x.x or 5.x.x.
Mine looks like this currently:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists