Adam Arold
Adam Arold

Reputation: 30528

How to build a jar from a multi-module project when using Gradle?

I'm working on a multi-module library project which I build with Gradle. I have a dependency to another module of this project in my dependencies section:

dependencies {
    compile project(':my-other-module')
}

My problem is that I want to build a .jar file which only contains the local modules in the final file, not its transitive dependencies. I tried this:

jar {
    from project(':my-other-module').configurations.compile.collect { zipTree it }
}

but this added all the transitive dependencies as well. I want to create a .jar which only contains my own files, so the users of this library can have their own versions of transitive dependencies. How can I do so?

Further clarification:

I have dependencies declared in my project to external jars like apache-commons. I want these not to be in my resulting .jar file but I want the users of my library to be able to just add my library as a dependency and let Maven/Gradle download the transitive dependencies. I don't want these transitive dependencies to be in the .jar file I deploy to Maven Central. compileOnly is not an option since the dependencies I use like apache-commons are not provided by a framework or a container. They need to be present as compile dependencies. I just want to build and deploy a .jar file which has all the files in my project which has multiple modules.

Upvotes: 3

Views: 3414

Answers (2)

r-rach
r-rach

Reputation: 11

I am not sure it'll help you or not but, you can try this.

In your build.gradle file, customize your jar task as follows:

// This closure will return the full directory path of folder where your classes are built
ext.moduleClassPath = { moduleName ->
    def classOutputDirConst = "/classes/java/main"
    return "${project(":${moduleName}").buildDir}${classOutputDirConst}"
}

// Now jar task will include only the built file of specified project
jar {
    from(moduleClassPath("projectName1"), moduleClassPath("projectName2"))
}

Check the reference for the from(SourcePaths) method here: Reference: https://docs.gradle.org/current/dsl/org.gradle.jvm.tasks.Jar.html#org.gradle.jvm.tasks.Jar:from(java.lang.Object[])

Upvotes: 1

Adrian Baker
Adrian Baker

Reputation: 9989

Gradle has a compile-only dependency concept, similar to Maven's provided scope:

Compile-only dependencies are distinctly different than regular compile dependencies. They are not included on the runtime classpath and they are non-transitive, meaning they are not included in dependent projects.

The dependencies you don't want can be declared in the compileOnly configuration, rather than compile, eg:

dependencies { compileOnly 'javax.servlet:servlet-api:2.5' }

compileOnly is not even visible to unit tests, by default. We change this in a common gradle snippet which we include in each build:

// compileOnly isn't visible to tests by default, add it
plugins.withType(JavaPlugin).whenPluginAdded {
    sourceSets {
        test.compileClasspath += configurations.compileOnly
        test.runtimeClasspath += configurations.compileOnly
    }
}

For the second part, for which I believe you want to create a single "fat" jar, I would suggest creating your jar using the very good Shadow Plugin, rather than manually extending the jar task. By default, the shadow plugin will not include anything in the compileOnly configuration in the resulting jar.

Upvotes: 0

Related Questions