Slackbot
07/06/2022, 8:01 AMNiels Doucet
07/06/2022, 8:41 AMGiuseppe Barbieri
07/06/2022, 8:42 AMNiels Doucet
07/06/2022, 8:43 AMGiuseppe Barbieri
07/06/2022, 8:43 AMsourceSets {
main {
java.srcDir("$buildDir/generated")
}
}Giuseppe Barbieri
07/06/2022, 8:44 AMassemble and got error because the code supposed to be generated wasn't there..Niels Doucet
07/06/2022, 8:46 AMsourceSets.main.kotlin. Not sure how java and kotlin relate to eachother.Niels Doucet
07/06/2022, 8:53 AMcompileKotlin.dependsOn(generateCode), as that’s the task that actually requires the generated codeGiuseppe Barbieri
07/06/2022, 8:58 AMcompileKotlin.dependsOn(generateCode) did do it indeed, thanks
now I get the same for sourcesJar and I'll do the sameNiels Doucet
07/06/2022, 9:01 AMGiuseppe Barbieri
07/06/2022, 9:02 AMGiuseppe Barbieri
07/06/2022, 9:05 AMsourcesJar
> Task :generateCode
> Task :compileKotlin UP-TO-DATE
> Task :compileJava NO-SOURCE
> Task :pluginDescriptors UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :inspectClassesForKotlinIC UP-TO-DATE
> Task :jar UP-TO-DATE
> Task :javadoc NO-SOURCE
> Task :javadocJar UP-TO-DATE
> Task :sourcesJar
Execution optimizations have been disabled for task ':sourcesJar' to ensure correctness due to the following reasons:
- Gradle detected a problem with the following location: '/home/elect/IdeaProjects/gradle-catalog/build/generated'. Reason: Task ':sourcesJar' uses this output of task ':compileJava' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.4.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.But if I try to
getByName("sourcesJar") { dependsOn(generateCode) }
then
Task with name 'sourcesJar' not found in root project 'gradle-catalog'.
Alex Semin
07/06/2022, 9:05 AMbuiltBy function.
See an example in this section:
https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:link_output_dir_to_input_filesNiels Doucet
07/06/2022, 9:11 AMgetByName if you use kotlin dsl, this task has an accessor: tasks.sourcesJarAlex Semin
07/06/2022, 9:14 AMsourcesJar artifact, or to actually compile them and include in the binary artifact. In the latter case, I think, sourcesJar is the wrong path, and the generated sources must be included in the main source set (after which they will automatically be included in the sourcesJar).Giuseppe Barbieri
07/06/2022, 9:31 AMnote also that you don’t need theI can't see itif you use kotlin dsl, this task has an accessor:getByNametasks.sourcesJar
Giuseppe Barbieri
07/06/2022, 9:33 AMmain source set
val SourceSet.kotlin: SourceDirectorySet
get() = project.extensions.getByType<KotlinJvmProjectExtension>().sourceSets.getByName(name).kotlin
sourceSets {
main {
kotlin.srcDir("$buildDir/generated")
}
}Alex Semin
07/06/2022, 9:48 AMmain source set and the output of the task, because they are connected only via the directory name.Giuseppe Barbieri
07/06/2022, 9:49 AMgenerated sources must be included in thehow shall I do it?source set (after which they will automatically be included in themainsourcesJar
Alex Semin
07/06/2022, 9:50 AMgenerateSources task.Giuseppe Barbieri
07/06/2022, 9:51 AMbuiltBy comes into play?Alex Semin
07/06/2022, 9:52 AMGiuseppe Barbieri
07/06/2022, 9:58 AMbuiltBy looks to be available only on `Property`s, how can I use that in my case? I'd need somehow to have the input files for kotlinCompile call builtBy(generateCodeNiels Doucet
07/06/2022, 10:02 AMsourceSets.main.kotlin.srcDir("$buildDir/generated", "builtBy" to "generateSources")
I believe this should workAlex Semin
07/06/2022, 10:02 AMGiuseppe Barbieri
07/06/2022, 10:03 AMAlex Semin
07/06/2022, 10:03 AMbuiltBy was in the right direction, but not available in the scope we want.Niels Doucet
07/06/2022, 10:04 AMsourceSets.main.output.dir("$buildDir/generated", "builtBy" to "generateSources")Alex Semin
07/06/2022, 10:06 AMkotlin extension that you declare, to not get confused:
val SourceSet.kotlin: SourceDirectorySet
After that all you need to do is this:
kotlin {
sourceSets["main"].kotlin.srcDir(generateSources.get().outputs.files)
}
The important difference with all the approaches above is that we have a live instance of generateSources task, and we take the reference to it’s output file collection. This collection tracks dependencies. Because of this the main source set now depends on the generateSources task, and you don’t need to duplicate the "$buildDir/generated" path at all (which is not advised anyways).Alex Semin
07/06/2022, 10:07 AMjar task would execute the generatedSources task automatically:
$ ./gradlew --console plain :lib:jar
> Task :lib:generateSources
> Task :lib:compileKotlin FROM-CACHE
> Task :lib:compileJava NO-SOURCE
> Task :lib:processResources NO-SOURCE
> Task :lib:classes UP-TO-DATE
> Task :lib:inspectClassesForKotlinIC
> Task :lib:jar
(ignore the :lib prefixes, it’s just my local setup)Giuseppe Barbieri
07/06/2022, 10:12 AMtasks {
..
kotlin.sourceSets["main"].kotlin.srcDir(generateCode.get().outputs.files)
}
running assemble, I see generateCode is called properly before compileKotlin (and the generated directory is also properly marked by Idea as src folder) but I do still get the warning on sourcesJar
> Task :generateCode
> Task :compileKotlin UP-TO-DATE
> Task :compileJava NO-SOURCE
> Task :pluginDescriptors UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :inspectClassesForKotlinIC UP-TO-DATE
> Task :jar UP-TO-DATE
> Task :javadoc NO-SOURCE
> Task :javadocJar UP-TO-DATE
> Task :sourcesJar
Execution optimizations have been disabled for task ':sourcesJar' to ensure correctness due to the following reasons:
- Gradle detected a problem with the following location: '/home/elect/IdeaProjects/gradle-catalog/build/generated'. Reason: Task ':sourcesJar' uses this output of task ':compileJava' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.4.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.Ps: I also pushed in case you want to try this yourselves
Alex Semin
07/06/2022, 10:18 AMjava {
withSourcesJar()
}
To the build script. Seems to be working for me:
$ gw --console plain :lib:assemble
> Task :lib:generateSources
> Task :lib:compileKotlin UP-TO-DATE
> Task :lib:compileJava NO-SOURCE
> Task :lib:processResources NO-SOURCE
> Task :lib:classes UP-TO-DATE
> Task :lib:inspectClassesForKotlinIC UP-TO-DATE
> Task :lib:jar UP-TO-DATE
> Task :lib:sourcesJar
> Task :lib:assemble
BUILD SUCCESSFUL in 1sAlex Semin
07/06/2022, 10:20 AM1.0.0 has landed recently)
https://plugins.gradle.org/plugin/com.gradle.plugin-publishGiuseppe Barbieri
07/06/2022, 10:32 AMjava {
withSourcesJar()
}
didn't solve yet the warning about sourcesJar
I'm still puzzled why I get the warning about it but if the moment I try to configure it (getByName("sourcesJar") { .. } ) then I have the following error:
Task with name 'sourcesJar' not found in root project 'gradle-catalog'.
Giuseppe Barbieri
08/03/2022, 1:24 PMGiuseppe Barbieri
10/14/2022, 3:40 AMval generateCode by registering(GenerateCode::class)
kotlin.sourceSets.commonMain { kotlin.srcDir(generateCode.get().outputs.files) }