2

I have a multi-project gradle build, using kotlin as build script language.

I can build a "fat" jar containing all project dependencies (.class files) I am not sure how it works. But it goes through all dependencies and exclude duplicate packages.

tasks.jar {
    //manifest.attributes["Main-Class"] = "com.example.MyMainClass"
    val dependencies = configurations
        .runtimeClasspath
        .get()
        .map(::zipTree) // OR .map { zipTree(it) }
    from(dependencies)
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

Is there some equivalent way to create a sources jar. Including the source code of all dependencies?

Side question: Can you declare sources like dependencies?

dependencies {
    implementation(project(":common"))
    implementation(files("../libs/somejar1.jar"))
    implementation(files("../libs/somejar2.jar"))
}

sources {
    ?
}

Edit

I have written a library. The library has code I have written (multiple modules). The modules have dependencies. (Could be other modules within the project, and/or other libraries) I have the other libraries' as jar files stored locally together with the source code of those libraries.

I am able to pack all the code I have written together with my codes dependencies into a single fat jar.

It would be nice to also be able to pack all source files (including the source files of the dependencies) into a "fat sources jar".

That way, when I want to use my library in other projects. I can have 2 jars. One with all the .class files and one sources jar with all the .java files.

Dahl
  • 149
  • 1
  • 10
  • It's possible to create a 'far sources' jar, but it could be difficult. Are you sure that's really want you want, or is this an [X-Y problem](https://meta.stackexchange.com/q/66377/262203)? Do you only want to include local project sources, or also sources from remote dependencies? It suspect it would be much easier to [publish the projects](https://docs.gradle.org/7.6/userguide/publishing_maven.html) and use [`withSourcesJar()`](https://docs.gradle.org/7.6/userguide/java_plugin.html#sec:java-extension). – aSemy Jan 06 '23 at 10:04
  • @aSemy I will check out the anwer to my side question thanks. In this case i have all sources stored locally. Including all the sources of dependencies. (No remote dependencies). I have tried to publish projects some time ago. But i had some issues, [link](https://stackoverflow.com/questions/72409742/gradle-task-signmavenjavapublication-failed-because-it-has-no-configured-signat) – Dahl Jan 06 '23 at 10:23
  • @aSemy I want to pack all sources into one jar for simplicity. I have a library (my multi-project build) And the library has dependencies (all stored locally) Now i want to use the library in a new project. So i need the sources. – Dahl Jan 06 '23 at 10:27
  • @aSemy so for my side question: [sourceSet](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSet.html) is what i am looking for? I will read more into it. Thanks – Dahl Jan 06 '23 at 10:35
  • @aSemy i could ask it differently. If i have a sources jar file in a directory. Could i add that to the source files that i generate when running the "sourcesJar" task. – Dahl Jan 06 '23 at 13:31
  • Sorry, I misunderstood your side question. Perhaps you should ask it in another question. – aSemy Jan 06 '23 at 13:36
  • _Now i want to use the library in a new project. So i need the sources._ Sorry, I don't understand, you shouldn't need the sources to be able to use a project as a dependency. – aSemy Jan 06 '23 at 13:37
  • @aSemy Maybe i am misunderstanding how it works. The reason why i want this, is to be able to see .java classes in the IDE. So when i select "Choose sources..." in the IDE i can select a single .jar with all the source files. – Dahl Jan 06 '23 at 13:46
  • instead of selecting every single source .jar individually – Dahl Jan 06 '23 at 13:48
  • I create a fat jar of the library including all dependencies. So when i use this library i want a single source .jar of that library. The code i have written. And the the code of the library dependencies – Dahl Jan 06 '23 at 13:50
  • I will try to update the question to be more specific :) – Dahl Jan 06 '23 at 14:03
  • Try taking a look at this example for aggregating JaCoCo reports: https://docs.gradle.org/7.3/samples/sample_jvm_multi_project_with_code_coverage.html. You should focus on the `transitiveSourcesElements` and `sourcesPath` Configurations. Use `java.withSourcesJar()`, so the Java Plugin will create a `sourcesJar` task - add the `sourcesPath` Configuration to this task the same way you added the `runtimeClasspath` to the `jar` task. – aSemy Jan 07 '23 at 11:14

0 Answers0