Enuviel
Enuviel

Reputation: 316

Gradle: Create single JAR with all dependencies

I have project1 and project2.

project1 depend on project2:

dependencies {
    api project(':project2')
}

Is there any way to get one jar file that contains classes from project1 and project2?

Upvotes: 2

Views: 96

Answers (2)

Daniel Taub
Daniel Taub

Reputation: 5389

Yes of course :

// Make your project 2 api configuration
configurations {
    api
    compile.extendsFrom(api)

}

dependencies {
    api project(':project2')
}

task packAllToJar (type : Jar){
    // pack yours main module
    from sourceSets.main.output

    // pack your other "api" module dependencies
    from {
        configurations.api.collect { it.isDirectory() ? it : zipTree(it) }
    }

    // pack all dependencies in the root project
    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }

    // Set name and version
    version = "1.0"
    baseName = "customJarName"
}

Upvotes: 0

Cao Minh Vu
Cao Minh Vu

Reputation: 1948

Try this task:

task jar(type: Jar) {
 baseName="project"
 from 'src/main/java'
}
task create(type: Jar) {
  baseName = "project1"
  from {
    configurations.compile.collect {
        it.isDirectory() ? it : zipTree(it)
    }
  }
  with jar
}

configurations {
  jarConfig
}

artifacts {
  jarConfig jar
}

Upvotes: 1

Related Questions