xetra11
xetra11

Reputation: 8885

compile() error while trying to pass dependencies to subprojects in gradle.build file

I'm trying to generalize a dependency to all subprojects in my gradle project. Reading this question: https://discuss.gradle.org/t/inheriting-common-dependencies-from-parent-project-in-child-projects/5493/2 I tried it out:

subprojects{
    dependencies {
        compile group: 'com.xetra11.toolbox', name: 'toolbox-commons', version: "0.0.1"
    }
}

I failed with the following error:

 1. Error:(60, 0) Could not find method compile() for arguments
    [{group=com.xetra11.toolbox, name=toolbox-commons, version=0.0.1}] on
    object of type
    org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
    <a
    href="openFile:C:\Development\Testzone\toolbox-backend\build.gradle">Open
    File</a>

Also using allprojects closure did not succeeded. Did I understood the answers of this above mentioned question wrong or what is the issue here?

Upvotes: 0

Views: 235

Answers (1)

Martin Linha
Martin Linha

Reputation: 1019

That's because your subprojects do not have compile configuration yet. In other words, you have to first apply Java plugin and then declare the dependencies. Three ways how you can achieve that:

1) change the code to apply the plugin from root project

subprojects{
    apply plugin: 'java'
    dependencies {
        compile group: 'com.xetra11.toolbox', name: 'toolbox-commons', version: "0.0.1"
    }
}

2) call the configuration after buildscript evaluation of each project is done

subprojects{
    afterEvaluate {
      dependencies {
          compile group: 'com.xetra11.toolbox', name: 'toolbox-commons', version: "0.0.1"
        }
    }
}

3) or, add the configuration as soon as the Java plugin is added in subprojects

subprojects{
    plugins.whenPluginAdded { plugin ->
      if (plugin instanceof JavaPlugin) {
        dependencies {
          compile group: 'com.xetra11.toolbox', name: 'toolbox-commons', version: "0.0.1"
          }
        }
    }
} 

Upvotes: 1

Related Questions