Home > front end >  How can you add dependency source code to sourceJar task output?
How can you add dependency source code to sourceJar task output?

Time:01-07

If you got a multi-project gradle build. And one module depends on another.

How could you add the dependency module source code to the output jar

Now i am using this:

java {
    withSourcesJar()
}

I am new to gradle builds and i don't know any kotlin.

And if you have the source code of a dependency as a .jar file. Could you also add that to the output?

So I have a project module:

dependencies:

  • project module
  • local .jar

What i want:

One .jar of the project (including other modules and dependencies) compiled code:

project-0.5.0.jar

..and one .jar of the source code (including other modules and dependencies)

project-0.5.0-sources.jar

I have all source code of dependencies stored locally as .jar files

Edit

My project conventions (global for all modules):

plugins {
`java-library`
}

java {
    withSourcesJar()
}

How I am currently creating the project "fat".jar with compiled code: (inside the build script)

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
}

I have figured out how to add a project moduleA to another moduleB output sources .jar like so (inside moduleB's build-script):

tasks.sourcesJar {
    from(project(":moduleA").sourceSets.main.get().allSource)
}

Now I need to figure out how to include source code from a dependency .jar

from(file("../path/dependency-1.0.0-sources.jar"))

This packs the .jar as it is. I need it's files.

CodePudding user response:

You can use the jar task in your build script to create a jar file that includes the compiled class files and the source files of your project. To include the source files of your project's dependencies, you can use the from method of the jar task and specify the location of the dependency's source code.

For example, if you have a dependency on another module in your project called moduleA, you can include the source code of moduleA in the jar file like this:

jar {
    from project(':moduleA').sourceSets.main.allSource
}

If you have a local jar file that you want to include in the output jar, you can use the from method of the jar task and specify the location of the jar file. For example:

jar {
    from file('path/to/local.jar')
}

Keep in mind that the jar task is dependent on the compileJava task, so make sure that the compileJava task has been executed before the jar task. You can do this by specifying the dependsOn property of the jar task like this:

jar {
    dependsOn compileJava
    from project(':moduleA').sourceSets.main.allSource
    from file('path/to/local.jar')
}

CodePudding user response:

I figured it out. And it was easier than i thought. Keep in mind i am using Kotlin.

(All code snippets are inside the build.gradle.kts file of the project / module you are creating the sources .jar for)

First off you need to include either the java or java-library plugin:

plugins {
    `java-library`
}

And as far as i know, also this plugin extension:

java {
    withSourcesJar()
}

This makes the sourcesJar task available (task used to create the sources jar), and you can modify it like so:

tasks.sourcesJar {
    from(project(":common").sourceSets.main.get().allSource)
    from(zipTree("../libs/tinylog-2.5.0/tinylog-api-2.5.0-sources.jar"))
}

The first line inside the brackets includes my "common" module source code to the output .jar.

The second line adds the .java files inside the tinylog sources .jar to the output .jar.

  • Related