0

I'm using gradle 2.12 and I have a multi-module java gradle project structured in the following manner -

parentProject  
|__module1  
   |__src  
   |__build.gradle  
|__module2  
   |_src  
   |__build.gradle  
|__build.gradle

The settings.gradle is defined as -

include "module1"
project(":module1").projectDir = file("path/to/module1")

include "module2"
project(":module2").projectDir = file("path/to/module2")

module2 depends on module1 and the dependency is required in the buildscript. I followed this example which shows a workaround as gradle doesn't allow classpath dependencies of projects in the buildscript. What I did in module2's build.gradle is -

buildscript {
    repositories {
        mavenLocal()
    }

    project.getTasks().add([name: "module1", type: GradleBuild]) {
        buildFile = '../module1/build.gradle'
        tasks = ['pluginJar']
    }.execute()
}

apply plugin: java

dependencies {
    // project specific dependencies
}

I started getting the following error -

Error:(13, 0) Could not find method add() for arguments [{name=module1, type=class org.gradle.api.tasks.GradleBuild}, build_c3cjs9wfepa1xdklituv2tk9i$_run_closure1$_closure3@51bcefbf] on task set.

I searched for similar errors and found this solution, but it didn't help me.

This is how my project structure is, and I cannot change it to make module1 an independent build.

Please advice. Appreciate your help!

Thanks

Opal
  • 81,889
  • 28
  • 189
  • 210
Adee J
  • 75
  • 1
  • 5

2 Answers2

0

The method you try to invoke would be defined in TaskContainer. As you can see it's not.

Try:

project.getTasks().create(name: "module1", type: GradleBuild) {
    buildFile = '../module1/build.gradle'
    tasks = ['pluginJar']
}

As you can see I've omitted call to execute - you should never call a task directly.

Opal
  • 81,889
  • 28
  • 189
  • 210
  • Thanks Opal for replying! Here's the thing, the module1 here is a gradle-plugin, and after I try your suggestion it now says during the build that it cannot find the plugin specified. – Adee J Sep 27 '17 at 19:20
  • @AdeeJ and how about calling it directly, I mean invoking `execute()`? – Opal Sep 27 '17 at 19:36
  • The build loops infinitely. Whenever the build.gradle of a module is invoked, gradle evaluates all the modules using their respective build.gradle file. And when module2’s build.gradle gets evaluated, it in turn invokes module1’s build script. – Adee J Sep 27 '17 at 20:06
  • @AdeeJ I doubt if I can help without runnable example. – Opal Sep 27 '17 at 20:51
  • 1
    I've added the example code here - https://github.com/ajuwala/gradle-multimodule-example Please check and let me know if you need more information. – Adee J Sep 28 '17 at 19:28
  • As far as I see this is just not possible, see [here](https://stackoverflow.com/questions/37775023/how-to-use-classes-from-a-neighbouring-subproject-during-configuration-phase) and [here](https://stackoverflow.com/questions/28334174/how-to-add-java-source-onto-gradle-buildscript-classpath) – Opal Sep 29 '17 at 14:21
0

As answered by Opal, this is not possible.

Adee J
  • 75
  • 1
  • 5