Referencing kotlin gradle script varaibles in plugins block - gradle

I've been writing Gradle plugins in several languages using their usual Groovy build script DSL for some time. As of recently I wanted to learn how to use their Kotlin build script DSL but I can't quite figured out some things.
I have an example below:
val kotlin_version = "1.2.41"
plugins {
application
kotlin("jvm").version(kotlin_version)
}
application {
mainClassName = "samples.HelloWorldKt"
}
dependencies {
compile(kotlin("stdlib"))
}
repositories {
jcenter()
}
However, when I run a simple task like 'clean', I get the following error:
* What went wrong:
Script compilation error:
Line 5: kotlin("jvm") version kotlin_version
^ Unresolved reference: kotlin_version
However, if I replace kotlin_version with a string literal, it works fine:
val kotlin_version = "1.2.41"
plugins {
application
// kotlin("jvm").version(kotlin_version)
kotlin("jvm").version("1.2.41")
}
application {
mainClassName = "samples.HelloWorldKt"
}
dependencies {
compile(kotlin("stdlib"))
}
repositories {
jcenter()
}
However, if I parameterize the dependencies block with my kotlin_version, it works perfectly fine:
dependencies {
compile(kotlin("stdlib", kotlin_version))
}
Why can't variables be referenced inside of the plugins block?

See documentation: https://docs.gradle.org/current/userguide/plugins.html#sec:constrained_syntax
Basically, it states that plugin version must be constant, literal, string.
It is achieved by DslMaker: https://kotlinlang.org/docs/reference/type-safe-builders.html#scope-control-dslmarker-since-11
If you want to reuse variable elsewhere, you could use this:
buildscript {
var kotlinVersion: String by extra { "1.2.41" }
repositories {
jcenter()
}
dependencies {
classpath(kotlin("gradle-plugin", kotlinVersion))
}
}
plugins {
application
}
application {
mainClassName = "samples.HelloWorldKt"
}
apply {
plugin("kotlin")
plugin("application")
}
val kotlinVersion: String by extra
repositories {
jcenter()
}
dependencies {
compile(kotlin("stdlib-jdk8", kotlinVersion))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
If you don't need that, you can simply inline content of variable kotlin_version

Related

Gradle, use material3 in a compose multiplatform project

I'm doing a project with Jetpack Compose Multiplatform Desktop.
I'm not very familiar with Gradle, and I would like to use Material 3 in my project.
So, I added this line in build.gradle.kts file:
implementation("androidx.compose.material3:material3:1.0.0-alpha14")
But when I try to import the lib in a Kotlin file with:
import androidx.compose.material3.*
I've got an unresolved reference issue.
This is my build.gradle.kts file:
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
}
group = "com.example"
version = "1.0-SNAPSHOT"
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
kotlin {
jvm {
compilations.all {
kotlinOptions {
jvmTarget = "18"
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn")
}
}
withJava()
}
sourceSets {
val jvmMain by getting {
dependencies {
implementation(compose.desktop.currentOs)
implementation("androidx.compose.material3:material3:1.0.0-alpha14")
}
}
val jvmTest by getting
}
}
compose.desktop {
application {
mainClass = "MainKt"
jvmArgs += listOf("-Djava.library.path=./lib")
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "FanControl"
packageVersion = "1.0.0"
}
}
}
I think I should be able to use this lib since the release note indicate it support it.
Edit1:
I have this message from Gradle when I try to sync:
Could not resolve: androidx.compose.material3:material3:1.0.0-alpha14
Finally found what was the problem, it was the wrong library,
change
implementation("androidx.compose.material3:material3:1.0.0-alpha14")
with
implementation("org.jetbrains.compose.material3:material3-desktop:1.2.1")

Gradle - create catalog can not import the dependencies

I'm using gradle 7-4-1 and I'm trying to use a catalog to share dependencies between subprojects as documentation
https://docs.gradle.org/current/userguide/platforms.html#sec:sharing-catalogs
This is my settings.gradle.kts
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
version("log4j", "2.17.1")
library("log4j-api", "org.apache.logging.log4j", "log4j-api").versionRef("log4j")
library("log4j-core", "org.apache.logging.log4j", "log4j-core").versionRef("log4j")
library("log4j-slf4j-impl", "org.apache.logging.log4j", "log4j-slf4j-impl").versionRef("log4j")
bundle("log4j", listOf("log4j-api", "log4j-core", "log4j-slf4j-impl"))
}
}
}
rootProject.name = "gawds-db"
include("db-server", "db-client", "db-common")
enableFeaturePreview("VERSION_CATALOGS")
And I'm trying to import into my build.gradle.kts (located in my subproject)
plugins {
`maven-publish`
application
`java-library`
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
repositories {
mavenCentral()
}
configure<JavaApplication> {
mainClass.set("com.gawds.db.MainApp")
}
tasks.compileJava {
options.isIncremental = true
options.isFork = true
options.isFailOnError = false
}
tasks.named<Test>("test") {
useJUnitPlatform()
}
dependencies {
implementation(libs.log4j.api)
implementation(libs.log4j.core)
implementation(libs.log4j.log4j.slf4j.impl)
}
Note: also tried
implementation(libs.bundles.log4j)
But neither referencing libs.alias nor libs.bundles.alias-bundle work.
My project structure is:
/
| settings.gradle.kts
| db-server /
| build.gradle.kts
Just in case helps someone else. First, not sure why intellij marks as red, however from cmd line looks like it works.
From settings.gradle.kts get rid of
enableFeaturePreview("VERSION_CATALOGS")
This was required in previous versions but not in gradle 7.4.x
Also u need to add the repos in the catalog section, see below:
dependencyResolutionManagement {
repositories {
mavenCentral()
}
versionCatalogs {
create("libs") {
version("log4jVersion", "2.17.1")
library("log4j-api", "org.apache.logging.log4j", "log4j-api").versionRef("log4jVersion")
library("log4j-core", "org.apache.logging.log4j", "log4j-core").versionRef("log4jVersion")
library("log4j-slf4j-impl", "org.apache.logging.log4j", "log4j-slf4j-impl").versionRef("log4jVersion")
bundle("log4j", listOf("log4j-api", "log4j-core", "log4j-slf4j-impl"))
}
}
}
rootProject.name = "gawds-db"
include("db-server", "db-client", "db-common")
Another important point that I did wrong in my tests are:
Assuming this alias:
log4j-slf4j-impl
the typesafe equivalent is
log4j.slf4j.impl
so we need to call in the build.gradle.kts:
implementation(libs.log4j.slf4j.impl)
Also u can print the alias to check the name using:
val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
println("${versionCatalog.libraryAliases}")
To improve the previous code, we can create a task to print the catalog alias:
abstract class PrintCatalogAlias #Inject constructor(private val r : Project) : DefaultTask() {
#org.gradle.api.tasks.TaskAction
fun catalogAlias() {
val versionCatalog = r.rootProject.project.extensions.getByType<VersionCatalogsExtension>().named("libs")
println("${versionCatalog.libraryAliases}")
}
}
tasks.register<PrintCatalogAlias>("catalog")

Gradle platform with java-test-fixtures

I am configuring dependency versions for an multiproject gradle build in a centralized way. This way it works:
// root build.gradle.kts -- test configuration centralized
subprojects {
apply(plugin = "java-library")
repositories {
jcenter()
}
val junitVersion = "5.5.2"
dependencies {
"testImplementation"(platform("org.junit:junit-bom:$junitVersion"))
}
}
This way does not. While configuration phase is OK:
// root build.gradle.kts -- test && test fixtures configuration centralized
subprojects {
apply(plugin = "java-library")
repositories {
jcenter()
}
val junitVersion = "5.5.2"
dependencies {
"testImplementation"(platform("org.junit:junit-bom:$junitVersion"))
}
if (convention.findPlugin(JavaTestFixturesPlugin::class.java) != null) {
dependencies {
"testFixturesApi"(platform("org.junit:junit-bom:$junitVersion"))
}
}
}
...yet compileTestFixturesKotlin task fails. And the error is:
Could not find org.junit.jupiter:junit-jupiter-api:.
What's wrong with java-test-fixtures plugin? Or with my code maybe?

Getting newly published package from JitPack fails

I'm trying to publish my java library to JitPack using the maven-publish plugin in Gradle. I have done all that the docs have said, and JitPack says publishing was a success, but it seems like I cannot install my library, even if I just copy and paste straight from JitPack's repository.
I tried pushing straight to master on the github repo. I also changed the artifact id to sertain-core and the version to 1.0.0, as that is what is specified in the publish block. I even checked the repository url and downloaded the jar manually, and it worked fine. It seems that the only problem is downloading the jar using it's maven coordinates.
Here's my library's build file (the dependencies block is in another file and works fine):
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val ktlint by configurations.creating
plugins {
kotlin("jvm") version "1.3.50"
`maven-publish`
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect", "1.3.50"))
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.3.1")
implementation("org.jetbrains.kotlin", "kotlin-reflect", "1.3.50")
implementation("edu.wpi.first.wpilibj", "wpilibj-java", "2019.4.1")
implementation("edu.wpi.first.hal", "hal-java", "2019.4.1")
implementation("edu.wpi.first.ntcore", "ntcore-java", "2019.4.1")
implementation("com.ctre.phoenix", "api-java", "5.14.1")
ktlint("com.pinterest:ktlint:0.34.2")
}
tasks {
val ktlint by creating(JavaExec::class) {
group = "verification"
description = "Check Kotlin code style."
classpath = configurations["ktlint"]
main = "com.pinterest.ktlint.Main"
args = listOf("src/**/*.kt")
}
"check" {
dependsOn(ktlint)
}
create("ktlintFormat", JavaExec::class) {
group = "formatting"
description = "Fix Kotlin code style deviations."
classpath = configurations["ktlint"]
main = "com.pinterest.ktlint.Main"
args = listOf("-F", "src/**/*.kt")
}
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.freeCompilerArgs += setOf("-Xuse-experimental=kotlin.Experimental")
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "org.sert2521.sertain"
artifactId = "sertain-core"
version = "1.0.0"
from(components["java"])
artifact("$buildDir/libs/${project.name}.jar")
}
}
}
And here's the build file of the project that should install the library
plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.50"
id "edu.wpi.first.GradleRIO" version "2019.4.1"
}
ext.kotlinVersion = "1.3.50"
tasks.whenTaskAdded { task ->
if (task.name == "deploy" || task.name == "deployMain" || task.name == "simulateJava") task.dependsOn "assemble"
}
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
maven { url "http://first.wpi.edu/FRC/roborio/maven/release" }
maven { url "http://devsite.ctr-electronics.com/maven/release" }
maven { url "https://www.kauailabs.com/maven2" }
maven { url "http://www.revrobotics.com/content/sw/max/sdk/maven/" }
maven { url 'https://jitpack.io' }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1"
compile "com.kauailabs.navx.frc:navx-java:3.1.344"
compile "org.jetbrains.kotlin:kotlin-reflect:1.3.50"
compile wpi.deps.wpilib()
compile wpi.deps.vendor.java()
nativeZip wpi.deps.vendor.jni(wpi.platforms.roborio)
nativeDesktopZip wpi.deps.vendor.jni(wpi.platforms.desktop)
implementation 'com.github.SouthEugeneRoboticsTeam:sertain:publishing-f3bdecc967-1'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
deploy {
targets {
roboRIO("roborio") {
team = frc.getTeamOrDefault(2521)
}
}
artifacts {
frcJavaArtifact("frcJava") {
targets << "roborio"
debug = frc.getDebugOrDefault(false)
}
fileTreeArtifact("frcStaticFileDeploy") {
files = fileTree(dir: "src/main/deploy")
targets << "roborio"
directory = "/home/lvuser/deploy"
}
}
}
jar {
from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
manifest {
attributes(
"Main-Class": "org.sert2521.example.MainKt"
)
}
}
wrapper {
gradleVersion = "5.0"
}
There are no error messages when publishing the library. When installing the library, there is only the usual error from gradle not being able to find a dependency.

Kotlin Test Coverage

Does anyone know if a good test coverage tool (preferably Gradle plugin) exists for Kotlin? I've looked into JaCoCo a bit, but it doesn't seem to reliably support Kotlin.
As requested, here is an example build.gradle that uses Kotlin, and incorporates both Jacoco and Sonarqube integration, produces a jar and sources, and ties in Detekt for static analysis.
I had to manually add a couple things as my work build has jacoco applied by an in-house plugin.
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.10'
id 'org.springframework.boot' version '1.5.9.RELEASE'
id 'io.spring.dependency-management' version '1.0.4.RELEASE'
id 'io.gitlab.arturbosch.detekt' version '1.0.0.RC6'
id "org.sonarqube" version "2.6.2".
}
apply plugin: 'jacoco'
ext {
springBootVersion = '1.5.9.RELEASE'
springCloudVersion = 'Dalston.SR4'
kotlinVersion = '1.2.10'
detektVersion = '1.0.0.RC6'.
}
//======================= Project Info =============================================
group = 'group'
version = '0.14'
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-starter- parent:$springBootVersion")
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$springCloudVersion"
}
}
repositories {
jcenter()
}
//======================= Dependencies =============================================
dependencies {
// Version MUST be explicitly set here or else dependent projects will not be able to build as Gradle will not know
// what version to use
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
compile 'org.springframework.boot:spring-boot-starter-web'
compile('org.springframework.ws:spring-ws-support') {
exclude(module: 'javax.mail')
}
compile 'org.springframework.boot:spring-boot-starter-actuator'
// Spring security
compile 'org.springframework.security:spring-security-web'
compile 'org.springframework.security:spring-security-config'
compile 'javax.servlet:javax.servlet-api'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
compile('org.apache.httpcomponents:httpclient')
compile 'com.nimbusds:nimbus-jose-jwt:4.23'
}
//======================= Tasks =============================================
defaultTasks 'build'
tasks.bootRepackage.enabled = false
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = 1.8
freeCompilerArgs = ["-Xjsr305=strict"]
}
}
compileJava.options.encoding = 'UTF-8'
test.testLogging.exceptionFormat = 'full'
// ********************************
ext.coverageExclusions = [
// Configuration
'com.bns.pm.config.*',
// data classes
'com.bns.pm.domain.*',
// Account Service domain objects
'com.bns.pm.account.domain.*',
// Other items
'com.bns.pm.exceptions.DataPowerFaultException.Companion',
'com.bns.pm.controllers.ServiceExceptionHandler*',
'com.bns.pm.service.callback.DPWebServiceMessageCallback',
'com.bns.pm.service.HealthCheckService',
'com.bns.pm.util.SystemPropertiesUtilKt',
]
check.dependsOn jacocoTestCoverageVerification
jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
// White list
excludes = coverageExclusions
limit {
minimum = 0.70
}
}
}
}
jacocoTestReport {
description 'Generates Code coverage report. Fails build if it does not meet minimum coverage.'
reports {
xml.enabled = true //XML required by coveralls and for the below coverage checks
html.enabled = true
csv.enabled = false
}
def reportExclusions = coverageExclusions.collect {
it.replaceAll('\\.', '/') + (it.endsWith('*') ? '' : '*')
}
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, excludes: reportExclusions)
})
}
}
test.finalizedBy jacocoTestReport
afterEvaluate {
sonarqube {
properties {
property 'sonar.jacoco.reportPath', "${buildDir}/jacoco/test.exec"
property "detekt.sonar.kotlin.config.path", "detekt.yml"
property 'sonar.java.binaries', "$projectDir/build/classes/kotlin"
property 'sonar.coverage.exclusions', coverageExclusions.collect {
'**/' + it.replaceAll('\\.', '/') + (it.endsWith('*') ? '' : '*')
}
}
}
}
// Ensure source code is published to Artifactory
// Have to redefine publishing with new name as Accelerator Plugin already defined mavenJava
task sourceJar(type: Jar) {
from sourceSets.main.allSource
classifier 'sources'
}
publishing {
publications {
mavenJava2(MavenPublication) {
from components.java
artifact(sourceJar) {
classifier = 'sources'
}
}
}
}
artifactory {
contextUrl = "${artifactory_contextUrl}"
publish {
repository {
repoKey = "${artifactory_projectRepoKey}"
username = "${artifactory_user}"
password = "${artifactory_password}"
maven = true
}
defaults {
publications('mavenJava2')
}
}
}
artifactoryPublish {
dependsOn jar
}
detekt {
version = detektVersion
profile("main") {
input = "$projectDir/src"
config = "$projectDir/detekt.yml"
filters = ".*/resources/.*,.*/tmp/.*"
output = "$project.buildDir/reports/detekt"
}
}
check.dependsOn detektCheck
Good news: there is a new kotlinx-kover Gradle plugin, compatible with JaCoCo and IntelliJ.
plugins {
id("org.jetbrains.kotlinx.kover") version "0.5.0"
}
Once applied, the plugin can be used out of the box without additional configuration.
Watch its YouTube announcement video and also track its roadmap from this youtrack issue.
As said in the video, it solves the problem with inline functions and more.

Resources