Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Gradle jar multiple projects into one jar?

Tags:

java

gradle

build

Can Gradle jar multiple projects into one jar ?

I know you can do it for a single project using a method like this:

task packageTests(type: Jar) {
  from sourceSets.test.classes
}

But how does a person zip up multiple sub-projects into one jar?

I tried this and it doesn't work:

task packageTests(type: Jar) {
  from project(':core').sourceSets.main.classes
  from project(':core:google').sourceSets.test.classes
  from project(':core:bing').sourceSets.test.classes
}
like image 366
djangofan Avatar asked Dec 08 '12 21:12

djangofan


People also ask

What is multi-project build in Gradle?

A multi-project build in Gradle consists of one root project, and one or more subprojects. A basic multi-project build contains a root project and a single subproject. This is a structure of a multi-project build that contains a single subproject called app : Example 1. Basic multi-project build.

How do I create a sub project in Gradle?

Create a Multi-Project. Let's create a project containing its subprojects and build the Gradle project. Co'sider the below project structure in which the root project name is Multi_project, and the subproject name is sub_project. Create a new root directory in which we want to create a multi-project.


1 Answers

Here's my solution, which is a little bit simpler:

// Create a list of subprojects that you wish to include in the jar.  
def mainProjects = [':apps',':core',':gui',':io']
task oneJar( type: Jar , dependsOn: mainProjects.collect{ it+":compileJava"}) {
    baseName = 'name of jar'
    from files(mainProjects.collect{ project(it).sourceSets.main.output })
}

Code has been tested on Gradle 1.12

like image 72
lessthanoptimal Avatar answered Jan 03 '23 17:01

lessthanoptimal