How to copy sources of a submodule into the build of another module


(niranjan.nanda) #1

I have a multi-module setup for a Java project with following structure.

|--> core-module
|       |--> src
|       |--> build.gradle
|       |-->
|--> lib-module
|       |--> src
|       |--> build.gradle
|       |-->
|--> settings.gradle
|--> build.gradle

Content of mainApp/settings.gradle = 'mainApp'
include 'core-module', 'lib-module'

Intention is to generate core-module.jar and lib-module.jar; however, the core-module should contain the all the classes from lib-module. Basically copy the classes from lib-module subproject in the jar task of core-module. I tried following (in the build.gradle) of core-module, but it didn’t work

jar {
	manifest {
		attributes 'Manifest-Version': '0.1', 
			'Implementation-Title': 'Core Module',
	from project(path: ':lib-module') {
		include 'com/demo/lib/**'

I got following exception

* What went wrong:
Cannot convert the provided notation to a File or URI: project ':lib-module'.
The following types/formats are supported:
  - A String or CharSequence path, for example 'src/main/java' or '/usr/include'.
  - A String or CharSequence URI, for example 'file:/usr/include'.
  - A File instance.
  - A Path instance.
  - A Directory instance.
  - A RegularFile instance.
  - A URI or URL instance.

Can someone help what’s the best way to achieve this?

(Pierre) #2


Should I were you, I’d look at the Java Library Distribution plugin:
The Java library distribution plugin adds support for building a distribution ZIP for a Java library. The distribution contains the JAR file for the library and its dependencies.

It seems to match your requirements.

Should you cannot/do not want to use the aforementioned plugin, then maybe you could try something like that:

jar {
    from project.findProject(':lib-module').projectDir {
        // ...

(niranjan.nanda) #3

Thank you for the reply. I have used distribution plugin before; but in this case, the requirement was to release core-module as a jar with flattened classes for lib-module. I tried following and it worked

// For core-module classes
from (project.sourceSets.main.output.classesDir)

// For lib-module classes.
from project(':lib-module').sourceSets.main.output.classesDir