0

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 ?

Pawel
  • 15,548
  • 3
  • 36
  • 36
Emile Achadde
  • 1,715
  • 3
  • 10
  • 22

1 Answers1

1

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())
}
Cisco
  • 20,972
  • 5
  • 38
  • 60
  • My code is not java but kotlin. Sorry I forgot to specify it. – Emile Achadde Feb 15 '20 at 08:54
  • I use gradle 6.1.1. The gradle build task gives no error but provides no class, makin the run fail. I do not know how to tell gradle to compile the kotlin code from the usual src/main/kotlin directory. – Emile Achadde Feb 15 '20 at 09:02
  • This was missing to make gradle compile the code ```plugins { kotlin("jvm") version "${extra["kotlinVersion"]}" }``` – Emile Achadde Feb 16 '20 at 12:24