Emile Achadde
Emile Achadde

Reputation: 1815

Gradle : How to translate some groovy code to kotlin

The groovy code below is working fine in a script build.gradle :

task sourcesJar(type: Jar, dependsOn: classes) {
    from sourceSets.main.allSource
    classifier = 'sources'
}

artifacts {
    archives sourcesJar
}

I can't succeed in translating its syntax to kotlin build.gradle.kts. Could someone give me the correct translation ?

Upvotes: 0

Views: 449

Answers (1)

Cisco
Cisco

Reputation: 22952

If you are on Gradle 6, then this is trivial with the java plugin:

plugins {
    java
}

java {
    withSourcesJar()
}

If you are on an older version of Gradle or unable to upgrade, then you'll need to define the task as you have above:

plugins {
    java
}

val sourcesjar by tasks.registering(Jar::class) {
    from(sourceSets[SourceSet.MAIN_SOURCE_SET_NAME].allSource)
    // Use archiveClassifier on Gradle 5.1+ otherwise use classifier
    archiveClassifier.set("sources")
}

artifacts {
    archives(sourcesjar.get())
}

Upvotes: 1

Related Questions