I'm trying to move from groovy to kts, but sometim...
# gradle
j
I'm trying to move from groovy to kts, but sometimes for some projects I get unresolved for
test
.
Copy code
plugins {
    kotlin("jvm") version ("1.3.31")
    ...
}
dependencies {
   val junitVersion = "5.4.2"
   ...
   testRuntime("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
}
tasks {
    test { // <-- this is unresolved

    }
   ...
}
What more is required for
test
to exist?
m
j
is that the same as writing
id("org.gradle.java")
?
m
test
should exist because kotlin
jvm
plugin applies the
java
plugin - what version of gradle are you using? does it work on CLI?
πŸ‘ 1
j
not sure,
is there a way that I can define that within the kts file itself?
g
Yes, test accessor should exist if you apply Kotlin plugin Which version of Gradle do you use?
j
I can see the gradle wrapper is 4.10.3
g
This is too old version of Gradle, it doesn't support static accessors for tasks
If you want to use Kotlin DSL I highly recommend start from migration to the latest stable Gradle
If for some reason you don't want, you can get and configure type by name: tasks { configure<Test>("test") { ... } }
j
I ran
gradlew.bat wrapper --gradle-version 5.4.1
and still doesn't reconize test
I would love to upgrade, just need to figure out how to do it properly
g
Are sure that this taks is successful?
You need valid config before update wrapper
Comment your test task, update Gradle wrapper (you can just change version in gradle-wrapper.properties manually), run some task, so new version will be downloaded, then uncomment test task config
j
ah, sorry I ran it sucessfully, but then the actual download started in the background.
Intellij is resolving a bunch of stuff automatically right now for me. Seems like it's gonna work now
g
Yeah, should work, but be ready that it may be some problems with tooling after update, sometimes it requires to invalidate project caches of something is not resolved in IDE but works in command line
j
Didn't work, I'm trying the invalidate cache and restart feature in intellij
g
What exactly doesn't work?
Try run from command line and check for errors
j
It's working now from command line πŸ‘
Thanks for your patience πŸ™‚ I will need to get myself a faster computer πŸ˜› Intellij is still doing some background tasks
there, it's finished now. It worked fine from command line, but everything is unresolved within my build.gradle.kts file now
message has been deleted
closing and opening the file again started 5 processes, and now it's fine
g
Should work on latest IDEA, again, can recommend only restart IDE, sometimes happening for me after project update to new version, but after that usually works fine
Yeah, now should be fine πŸ‘ Tooling not perfect yet, especially work with caches, but now should work fine
j
one last question πŸ™‚
m
For me running another Gradle Sync triggers a little bar where there is an action
Apply Dependencies
(usually after a few seconds delay). Using that action helps πŸ˜…
j
updating gradle with running
gradlew.bat wrapper --gradle-version 5.4.1
is that a proper way of upgrading?
m
Yeah, but you can also specify it in the
build.gradle.kts
directly:
Copy code
tasks {
	withType<Wrapper> {
		distributionType = Wrapper.DistributionType.ALL
		gradleVersion = "5.4.1"
	}
}
And then just
gradlew.bat wrapper
g
Yeah, it will update Gradle version in wrapper options and update Gradle wrapper jar (but using existing version of wrapper and usually it's not necessary
j
thanks, just what I wanted πŸ™‚
g
I don't use this task anymore, easier update gradle-wrapper.properties manually. also just less unnecessary configs in root build.gradle
j
by just upgrading "distributionUrl" within gradle-wrapper.properties? I never really trusted that one, (not sure why πŸ˜›)
if you think that's fine, i'll stick with that one πŸ™‚
g
Always works for me, this url never changed at least from Gradle 2.x, as I remember 😬
πŸ‘ 1
Wrapper task also updates wrapper jar, but it also not necessary, at least for minor versions
m
I prefer to have as much config in one place as possible. Gradle projects become scattered so quickly. Wrapper properties, build.gradle, settings.gradle, buildSrc, gradle.properties 😡
j
true, my biggest problem is just that no one does it in the same way, making it hard to find easy copy and paste solutions on the internet
g
But update Gradle is a bit different from update dependencies. I agree with you in general but this additional config also very easy misuse: Update Gradle version in task config Do not run wrapper task And now you think that you use new Gradle, but you actually still on old, so now you have 2 configs with different versions and confusion (happened with me a few times)
no one does it in the same way
Agree, also it's very flexible and inflexible at the same time
j
I love gradle for being so flexible, but would love for jetbrains and kotlin gradle script to be a bit more opinionated, so there would be more inspections within intellij on how to properly to do gradle stuff πŸ˜›
g
It's already a bit more opinionated, because encouraging static types, and many old Gradle practices are not pleasant to use with Kotlin dsl
πŸ‘ 1
In terms of configs it also recommended to use buildSrc instead of gradle.properties or ext object, but not everything can be included there
m
Yeah, writing own Gradle plugins for own opinionated configurations is the way to go πŸ˜‚ https://github.com/fluidsonic/fluid-stdlib/blob/master/build.gradle.kts?ts=4
Next level: own Gradle Wrapper? πŸ€”
g
Yes, actually own opionated plugin is solution for many problems with shared configuration
Even without publishing, just put my-plugin.gradle.kts to buildSrc/src/kotlin
With precompiled script plugins it's much less boilerplate
w
at my company, i wrote 4 plugins that leverage existing OSS gradle plugins and apply our company's opinions on top of them, and it has helped the devs who don't care to dig through gradle config a lot.
so +1 to that approach